Skip to content

Commit

Permalink
Add rector + fix psalm (#87)
Browse files Browse the repository at this point in the history
* Add rector files yiisoft/yii-dev-tool#232

* Add rector/rector dependecy

* [rector] Apply fixes

* Apply fixes from StyleCI

* Use predefined rector action

* fix

* fix psalm

---------

Co-authored-by: rector-bot <rector@yiiframework.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
  • Loading branch information
4 people authored Feb 7, 2023
1 parent d342fde commit 80eeb8c
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 140 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.15.13",
"roave/infection-static-analysis-plugin": "^1.18",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.22"
Expand Down
3 changes: 2 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
resolveFromConfigFile="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
27 changes: 27 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
ClosureToArrowFunctionRector::class,
]);
};
18 changes: 5 additions & 13 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function count;
use function debug_backtrace;
use function gettype;
use function get_class;
use function implode;
use function in_array;
use function is_string;
Expand Down Expand Up @@ -148,8 +147,6 @@ public function getTargets(): array

/**
* @param string $level
* @param string|Stringable $message
* @param array $context
* @psalm-param LogMessageContext $context
* @psalm-suppress MoreSpecificImplementedParamType
*/
Expand Down Expand Up @@ -187,8 +184,6 @@ public function flush(bool $final = false): void
*
* @param int $flushInterval The number of messages to accumulate before flushing.
*
* @return self
*
* @see Logger::$flushInterval
*/
public function setFlushInterval(int $flushInterval): self
Expand All @@ -202,8 +197,6 @@ public function setFlushInterval(int $flushInterval): self
*
* @param int $traceLevel The number of call stack information.
*
* @return self
*
* @see Logger::$traceLevel
*/
public function setTraceLevel(int $traceLevel): self
Expand All @@ -219,8 +212,6 @@ public function setTraceLevel(int $traceLevel): self
*
* @throws InvalidArgumentException for non-string values.
*
* @return self
*
* @see Logger::$excludedTracePaths
*/
public function setExcludedTracePaths(array $excludedTracePaths): self
Expand Down Expand Up @@ -276,7 +267,7 @@ private function dispatch(array $messages, bool $final): void
$target->disable();
$targetErrors[] = new Message(
LogLevel::WARNING,
'Unable to send log via ' . get_class($target) . ': ' . get_class($e) . ': ' . $e->getMessage(),
'Unable to send log via ' . $target::class . ': ' . $e::class . ': ' . $e->getMessage(),
['time' => microtime(true), 'exception' => $e],
);
}
Expand Down Expand Up @@ -306,9 +297,10 @@ private function collectTrace(array $backtrace): array

foreach ($backtrace as $trace) {
if (isset($trace['file'], $trace['line'])) {
$excludedMatch = array_filter($this->excludedTracePaths, static function ($path) use ($trace) {
return str_contains($trace['file'], $path);
});
$excludedMatch = array_filter(
$this->excludedTracePaths,
static fn ($path) => str_contains($trace['file'], $path)
);

if (empty($excludedMatch)) {
unset($trace['object'], $trace['args']);
Expand Down
3 changes: 1 addition & 2 deletions src/Message/CategoryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function gettype;
use function is_string;
use function rtrim;
use function substr_compare;
use function sprintf;

/**
Expand Down Expand Up @@ -95,7 +94,7 @@ public function isExcluded(string $category): bool
$category === $include
|| (
!empty($include)
&& substr_compare($include, '*', -1, 1) === 0
&& str_ends_with($include, '*')
&& str_starts_with($category, rtrim($include, '*'))
)
) {
Expand Down
10 changes: 2 additions & 8 deletions src/PsrTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@
*/
final class PsrTarget extends Target
{
/**
* @var LoggerInterface The logger instance to be used for messages processing.
*/
private LoggerInterface $logger;

/**
* Sets the PSR-3 logger used to save messages of this target.
*
* @param LoggerInterface $logger The logger instance.
* @param LoggerInterface $logger The logger instance to be used for messages processing.
*/
public function __construct(LoggerInterface $logger)
public function __construct(private LoggerInterface $logger)
{
$this->logger = $logger;
parent::__construct();
}

Expand Down
11 changes: 2 additions & 9 deletions src/StreamTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@
*/
final class StreamTarget extends Target
{
/**
* @var resource|string A string stream identifier or a stream resource.
*
* @psalm-var mixed
*/
private $stream;

/**
* @param resource|string $stream A string stream identifier or a stream resource.
*/
public function __construct($stream = 'php://stdout')
public function __construct(private $stream = 'php://stdout')
{
$this->stream = $stream;
parent::__construct();
}

Expand Down Expand Up @@ -83,6 +75,7 @@ private function createStream()
}
}

/** @psalm-suppress DocblockTypeContradiction */
if (!is_resource($stream) || get_resource_type($stream) !== 'stream') {
throw new InvalidArgumentException(sprintf(
'Invalid stream provided. It must be a string stream identifier or a stream resource, "%s" received.',
Expand Down
21 changes: 4 additions & 17 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public function __toString(): string
* @dataProvider messageProvider
*
* @param $message
* @param string $expected
*/
public function testPsrLogInterfaceMethods($message, string $expected): void
{
Expand Down Expand Up @@ -163,10 +162,8 @@ public function invalidExcludedTracePathsProvider(): array

/**
* @dataProvider invalidExcludedTracePathsProvider
*
* @param mixed $list
*/
public function testSetExcludedTracePathsThrowExceptionForNonStringList($list): void
public function testSetExcludedTracePathsThrowExceptionForNonStringList(mixed $list): void
{
$this->expectException(InvalidArgumentException::class);
$this->logger->setExcludedTracePaths($list);
Expand Down Expand Up @@ -199,10 +196,8 @@ public function invalidMessageLevelProvider(): array

/**
* @dataProvider invalidMessageLevelProvider
*
* @param mixed $level
*/
public function testGetLevelNameThrowExceptionForInvalidMessageLevel($level): void
public function testGetLevelNameThrowExceptionForInvalidMessageLevel(mixed $level): void
{
$this->expectException(\Psr\Log\InvalidArgumentException::class);
Logger::validateLevel($level);
Expand Down Expand Up @@ -235,8 +230,6 @@ public function invalidListTargetProvider(): array

/**
* @dataProvider invalidListTargetProvider
*
* @param array $targetList
*/
public function testConstructorThrowExceptionForNonInstanceTarget(array $targetList): void
{
Expand Down Expand Up @@ -267,10 +260,6 @@ public function parseMessageProvider(): array

/**
* @dataProvider parseMessageProvider
*
* @param string $message
* @param array $context
* @param string $expected
*/
public function testParseMessage(string $message, array $context, string $expected): void
{
Expand Down Expand Up @@ -384,8 +373,8 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
[
$this->callback(function ($messages) use ($target1, $exception) {
$message = $messages[0] ?? null;
$text = 'Unable to send log via ' . get_class($target1) . ': RuntimeException: some error';
return (count($messages) === 1 && $message instanceof Message)
$text = 'Unable to send log via ' . $target1::class . ': RuntimeException: some error';
return ((is_countable($messages) ? count($messages) : 0) === 1 && $message instanceof Message)
&& $message->level() === LogLevel::WARNING
&& $message->message() === $text
&& is_float($message->context('time'))
Expand Down Expand Up @@ -416,7 +405,6 @@ public function testDispatchWithFakeTarget2ThrowExceptionWhenCollect(): void
/**
* Sets an inaccessible object property to a designated value.
*
* @param Logger $logger
* @param Message[] $messages
* @param bool $revoke whether to make property inaccessible after setting.
*/
Expand All @@ -435,7 +423,6 @@ private function setInaccessibleMessages(Logger $logger, array $messages, bool $
/**
* Gets an inaccessible object property.
*
* @param Logger $logger
* @param bool $revoke whether to make property inaccessible after getting.
*
* @return Message[]
Expand Down
4 changes: 0 additions & 4 deletions tests/Message/CategoryFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function invalidCategoryMessageStructureProvider(): array

/**
* @dataProvider invalidCategoryMessageStructureProvider
*
* @param array $categories
*/
public function testIncludeThrowExceptionForInvalidCategoryMessageStructure(array $categories): void
{
Expand All @@ -43,8 +41,6 @@ public function testIncludeThrowExceptionForInvalidCategoryMessageStructure(arra

/**
* @dataProvider invalidCategoryMessageStructureProvider
*
* @param array $categories
*/
public function testExcludeThrowExceptionForInvalidCategoryMessageStructure(array $categories): void
{
Expand Down
Loading

0 comments on commit 80eeb8c

Please sign in to comment.