Skip to content

Commit

Permalink
Add rector and apply suggestions (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
3 people committed Aug 11, 2023
1 parent d96894d commit ddfa0ee
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 109 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rector.yml
@@ -0,0 +1,23 @@
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
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -28,6 +28,7 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.17",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.3"
Expand Down
39 changes: 39 additions & 0 deletions rector.php
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php55\Rector\Class_\ClassConstantToSelfClassRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
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,
AddDefaultValueForUndefinedVariableRector::class,
JsonThrowOnErrorRector::class,
StringClassNameToClassConstantRector::class => [
__DIR__ . '/tests/UseStatementParserTest.php',
],
ClassConstantToSelfClassRector::class => [
__DIR__ . '/tests/TestAsset/DummyIteratorAggregateWithClosure.php',
],
]);
};
5 changes: 2 additions & 3 deletions src/ClosureExporter.php
Expand Up @@ -20,7 +20,6 @@
use function is_string;
use function mb_strlen;
use function mb_substr;
use function strpos;
use function token_get_all;
use function trim;

Expand Down Expand Up @@ -100,7 +99,7 @@ public function export(Closure $closure, int $level = 0): string
continue;
}
if (!empty($bufferUse)) {
if ($bufferUse !== $readableToken && strpos($readableToken, $bufferUse) === false) {
if ($bufferUse !== $readableToken && !str_contains($readableToken, $bufferUse)) {
$readableToken = $bufferUse . $readableToken;
}
$bufferUse = '';
Expand Down Expand Up @@ -201,7 +200,7 @@ private function processFullUse(string $use, array $uses): string
*/
private function isUseConsistingOfMultipleParts(string $use): bool
{
return $use !== '\\' && strpos($use, '\\') !== false;
return $use !== '\\' && str_contains($use, '\\');
}

/**
Expand Down
51 changes: 18 additions & 33 deletions src/VarDumper.php
Expand Up @@ -16,7 +16,6 @@
use Yiisoft\Arrays\ArrayableInterface;

use function array_keys;
use function get_class;
use function gettype;
use function highlight_string;
use function method_exists;
Expand Down Expand Up @@ -48,11 +47,6 @@ final class VarDumper
public const VAR_TYPE_ARRAY = 'array';
public const VAR_TYPE_OBJECT = 'object';
public const VAR_TYPE_RESOURCE = 'resource';

/**
* @var mixed Variable to dump.
*/
private $variable;
/**
* @var string[] Variables using in closure scope.
*/
Expand All @@ -67,17 +61,16 @@ final class VarDumper
/**
* @param mixed $variable Variable to dump.
*/
private function __construct($variable)
private function __construct(private mixed $variable)
{
$this->variable = $variable;
}

/**
* @param mixed $variable Variable to dump.
*
* @return static An instance containing variable to dump.
*/
public static function create($variable): self
public static function create(mixed $variable): self
{
return new self($variable);
}
Expand All @@ -94,7 +87,7 @@ public static function create($variable): self
*
* @throws ReflectionException
*/
public static function dump($variable, int $depth = 10, bool $highlight = true): void
public static function dump(mixed $variable, int $depth = 10, bool $highlight = true): void
{
echo self::create($variable)->asString($depth, $highlight);
}
Expand Down Expand Up @@ -216,7 +209,7 @@ public function export(bool $format = true, array $useVariables = [], bool $seri
*
* @return string
*/
private function dumpInternal($var, bool $format, int $depth, int $level): string
private function dumpInternal(mixed $var, bool $format, int $depth, int $level): string
{
switch (gettype($var)) {
case 'resource':
Expand Down Expand Up @@ -287,7 +280,7 @@ private function dumpInternal($var, bool $format, int $depth, int $level): strin
*
* @return string
*/
private function exportInternal($variable, bool $format, int $level): string
private function exportInternal(mixed $variable, bool $format, int $level): string
{
$spaces = str_repeat($this->offset, $level);
switch (gettype($variable)) {
Expand Down Expand Up @@ -335,12 +328,12 @@ private function exportInternal($variable, bool $format, int $level): string
}

return $this->exportObject($variable, $reflectionObject, $format, $level);
} catch (Exception $e) {
} catch (Exception) {
// Serialize may fail, for example: if object contains a `\Closure` instance so we use a fallback.
if ($this->serializeObjects && !$reflectionObject->isInternal() && !$reflectionObject->isAnonymous()) {
try {
return $this->exportObject($variable, $reflectionObject, $format, $level);
} catch (Exception $e) {
} catch (Exception) {
return $this->exportObjectFallback($variable, $format, $level);
}
}
Expand All @@ -353,10 +346,6 @@ private function exportInternal($variable, bool $format, int $level): string
}

/**
* @param mixed $var
* @param int $depth
* @param int $level
*
* @throws ReflectionException
*
* @return mixed
Expand Down Expand Up @@ -392,7 +381,7 @@ private function exportPrimitives(mixed $var, int $depth, int $level): mixed
return $this->exportClosure($var);
}

$objectClass = get_class($var);
$objectClass = $var::class;
$objectId = $this->getObjectId($var);
if ($depth <= $level) {
return [
Expand Down Expand Up @@ -443,10 +432,6 @@ private function getPropertyName(string|int $property): string
}

/**
* @param object $variable
* @param bool $format
* @param int $level
*
* @throws ReflectionException
*
* @return string
Expand All @@ -466,7 +451,7 @@ private function exportObjectFallback(object $variable, bool $format, int $level
}

/** @psalm-suppress RedundantCondition */
if ('__PHP_Incomplete_Class' !== get_class($variable) && method_exists($variable, '__toString')) {
if ('__PHP_Incomplete_Class' !== $variable::class && method_exists($variable, '__toString')) {
return $this->exportVariable($variable->__toString());
}

Expand All @@ -477,20 +462,22 @@ private function exportObject(object $variable, ReflectionObject $reflectionObje
{
$spaces = str_repeat($this->offset, $level);
$objectProperties = $this->getObjectProperties($variable);
$class = get_class($variable);
$class = $variable::class;
$use = $this->useVarInClosures === [] ? '' : ' use (' . implode(', ', $this->useVarInClosures) . ')';
$lines = ['(static function ()' . $use . ' {',];
if ($reflectionObject->getConstructor() === null) {
$lines = array_merge($lines, [
$lines = [
...$lines,
$this->offset . '$object = new ' . $class . '();',
$this->offset . '(function ()' . $use . ' {',
]);
];
} else {
$lines = array_merge($lines, [
$lines = [
...$lines,
$this->offset . '$class = new \ReflectionClass(\'' . $class . '\');',
$this->offset . '$object = $class->newInstanceWithoutConstructor();',
$this->offset . '(function ()' . $use . ' {',
]);
];
}
$endLines = [
$this->offset . '})->bindTo($object, \'' . $class . '\')();',
Expand Down Expand Up @@ -531,18 +518,16 @@ private function exportClosure(Closure $closure, int $level = 0): string
}

/**
* @param mixed $variable
*
* @return string
*/
private function exportVariable($variable): string
private function exportVariable(mixed $variable): string
{
return var_export($variable, true);
}

private function getObjectDescription(object $object): string
{
return get_class($object) . '#' . $this->getObjectId($object);
return $object::class . '#' . $this->getObjectId($object);
}

private function getObjectId(object $object): string
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Expand Up @@ -14,7 +14,7 @@
*
* @psalm-suppress MixedAssignment
*/
function d(...$variables): void
function d(mixed ...$variables): void
{
$highlight = PHP_SAPI !== 'cli';

Expand All @@ -35,7 +35,7 @@ function d(...$variables): void
*
* @psalm-suppress MixedAssignment
*/
function dd(...$variables): void
function dd(mixed ...$variables): void
{
d(...$variables);

Expand Down
2 changes: 1 addition & 1 deletion tests/TestAsset/DummyArrayableWithClosure.php
Expand Up @@ -13,7 +13,7 @@ final class DummyArrayableWithClosure implements ArrayableInterface

public function __construct()
{
$this->closure = static fn (): string => __CLASS__;
$this->closure = static fn (): string => self::class;
}

public function fields(): array
Expand Down
2 changes: 1 addition & 1 deletion tests/TestAsset/DummyJsonSerializableWithClosure.php
Expand Up @@ -13,7 +13,7 @@ final class DummyJsonSerializableWithClosure implements JsonSerializable

public function __construct()
{
$this->closure = static fn (): string => __CLASS__;
$this->closure = static fn (): string => self::class;
}

public function jsonSerialize(): array
Expand Down
4 changes: 2 additions & 2 deletions tests/TestAsset/DummyStringableWithClosure.php
Expand Up @@ -6,13 +6,13 @@

use Closure;

final class DummyStringableWithClosure
final class DummyStringableWithClosure implements \Stringable
{
private Closure $closure;

public function __construct()
{
$this->closure = static fn (): string => __CLASS__;
$this->closure = static fn (): string => self::class;
}

public function __toString(): string
Expand Down
5 changes: 0 additions & 5 deletions tests/UseStatementParserTest.php
Expand Up @@ -25,8 +25,6 @@ public function incorrectFileProvider(): array

/**
* @dataProvider incorrectFileProvider
*
* @param string $file
*/
public function testIncorrectFile(string $file): void
{
Expand Down Expand Up @@ -90,9 +88,6 @@ public function usesProvider(): array

/**
* @dataProvider usesProvider
*
* @param string $file
* @param array $expectedUses
*/
public function testFromFile(string $file, array $expectedUses): void
{
Expand Down

0 comments on commit ddfa0ee

Please sign in to comment.