diff --git a/packages/set/src/ValueObject/DowngradeSetList.php b/packages/set/src/ValueObject/DowngradeSetList.php index 3f7724ac3a4..3a26747ad11 100644 --- a/packages/set/src/ValueObject/DowngradeSetList.php +++ b/packages/set/src/ValueObject/DowngradeSetList.php @@ -9,25 +9,25 @@ final class DowngradeSetList /** * @var string */ - public const DOWNGRADE_PHP71 = __DIR__ . '/../../../../config/set/downgrade-php71.php'; + public const PHP_71 = __DIR__ . '/../../../../config/set/downgrade-php71.php'; /** * @var string */ - public const DOWNGRADE_PHP72 = __DIR__ . '/../../../../config/set/downgrade-php72.php'; + public const PHP_72 = __DIR__ . '/../../../../config/set/downgrade-php72.php'; /** * @var string */ - public const DOWNGRADE_PHP73 = __DIR__ . '/../../../../config/set/downgrade-php73.php'; + public const PHP_73 = __DIR__ . '/../../../../config/set/downgrade-php73.php'; /** * @var string */ - public const DOWNGRADE_PHP74 = __DIR__ . '/../../../../config/set/downgrade-php74.php'; + public const PHP_74 = __DIR__ . '/../../../../config/set/downgrade-php74.php'; /** * @var string */ - public const DOWNGRADE_PHP80 = __DIR__ . '/../../../../config/set/downgrade-php80.php'; + public const PHP_80 = __DIR__ . '/../../../../config/set/downgrade-php80.php'; } diff --git a/phpstan.neon b/phpstan.neon index 83ca84111f2..463a1bed7cd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -21,6 +21,14 @@ includes: - vendor/symplify/phpstan-rules/config/symplify-rules/string-to-constant-rules.neon services: + - + class: Symplify\PHPStanRules\Rules\RequireClassTypeInClassMethodByTypeRule + tags: [phpstan.rules.rule] + arguments: + requiredTypeInMethodByClass: + Rector\Core\Contract\Rector\PhpRectorInterface: + getNodeTypes: 'PhpParser\Node' + - class: Symplify\PHPStanRules\CognitiveComplexity\Rules\FunctionLikeCognitiveComplexityRule tags: [phpstan.rules.rule] @@ -32,6 +40,10 @@ services: tags: [phpstan.rules.rule] arguments: maxClassCognitiveComplexity: 50 + limitsByTypes: + Rector\Core\Rector\AbstractRector: 40 + Symfony\Component\Console\Command\Command: 40 + PHPStan\Rule\Rule: 30 # require constant in argument position - @@ -707,11 +719,16 @@ parameters: - '#Parameter \#1 \$expr of class PhpParser\\Node\\Stmt\\Expression constructor expects PhpParser\\Node\\Expr, PhpParser\\Node\\Expr\|PhpParser\\Node\\Stmt given#' - - message: '#Class cognitive complexity is \d+, keep it under 50#' + message: '#Class cognitive complexity is \d+, keep it under \d+#' paths: - rules/php70/src/EregToPcreTransformer.php - packages/node-collector/src/NodeCollector/NodeRepository.php - packages/node-type-resolver/src/NodeTypeResolver.php + - rules/solid/src/Rector/Variable/MoveVariableDeclarationNearReferenceRector.php + - src/Rector/AbstractRector.php + - rules/php80/src/Rector/If_/NullsafeOperatorRector.php + - rules/code-quality/src/Rector/For_/ForToForeachRector.php + - rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php # symplify 9 - '#Use decoupled factory service to create "(.*?)" object#' diff --git a/rules/coding-style/src/Naming/NameRenamer.php b/rules/coding-style/src/Naming/NameRenamer.php new file mode 100644 index 00000000000..6bb07e533b6 --- /dev/null +++ b/rules/coding-style/src/Naming/NameRenamer.php @@ -0,0 +1,163 @@ +nodeNameResolver = $nodeNameResolver; + } + + /** + * @param NameAndParent[] $usedNameNodes + */ + public function renameNameNode(array $usedNameNodes, string $lastName): void + { + foreach ($usedNameNodes as $nameAndParent) { + $parentNode = $nameAndParent->getParentNode(); + $usedName = $nameAndParent->getNameNode(); + + if ($parentNode instanceof TraitUse) { + $this->renameTraitUse($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof Class_) { + $this->renameClass($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof Param) { + $this->renameParam($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof New_) { + $this->renameNew($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof ClassMethod) { + $this->renameClassMethod($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof Interface_) { + $this->renameInterface($lastName, $parentNode, $usedName); + } + + if ($parentNode instanceof StaticCall) { + $this->renameStaticCall($lastName, $parentNode); + } + } + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameTraitUse(string $lastName, TraitUse $traitUse, Node $usedNameNode): void + { + foreach ($traitUse->traits as $key => $traitName) { + if (! $this->nodeNameResolver->areNamesEqual($traitName, $usedNameNode)) { + continue; + } + + $traitUse->traits[$key] = new Name($lastName); + } + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameClass(string $lastName, Class_ $class, Node $usedNameNode): void + { + if ($class->name !== null && $this->nodeNameResolver->areNamesEqual($class->name, $usedNameNode)) { + $class->name = new Identifier($lastName); + } + + if ($class->extends !== null && $this->nodeNameResolver->areNamesEqual($class->extends, $usedNameNode)) { + $class->extends = new Name($lastName); + } + + foreach ($class->implements as $key => $implementNode) { + if ($this->nodeNameResolver->areNamesEqual($implementNode, $usedNameNode)) { + $class->implements[$key] = new Name($lastName); + } + } + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameParam(string $lastName, Node $parentNode, Node $usedNameNode): void + { + if ($parentNode->type === null) { + return; + } + if (! $this->nodeNameResolver->areNamesEqual($parentNode->type, $usedNameNode)) { + return; + } + $parentNode->type = new Name($lastName); + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameNew(string $lastName, Node $parentNode, Node $usedNameNode): void + { + if ($this->nodeNameResolver->areNamesEqual($parentNode->class, $usedNameNode)) { + $parentNode->class = new Name($lastName); + } + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameClassMethod(string $lastName, ClassMethod $classMethod, Node $usedNameNode): void + { + if ($classMethod->returnType === null) { + return; + } + + if (! $this->nodeNameResolver->areNamesEqual($classMethod->returnType, $usedNameNode)) { + return; + } + + $classMethod->returnType = new Name($lastName); + } + + /** + * @param Name|Identifier $usedNameNode + */ + private function renameInterface(string $lastName, Interface_ $interface, Node $usedNameNode): void + { + foreach ($interface->extends as $key => $extendInterfaceName) { + if (! $this->nodeNameResolver->areNamesEqual($extendInterfaceName, $usedNameNode)) { + continue; + } + + $interface->extends[$key] = new Name($lastName); + } + } + + private function renameStaticCall(string $lastName, StaticCall $staticCall): void + { + $staticCall->class = new Name($lastName); + } +} diff --git a/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php b/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php index 4080c23b8b6..85525c278dc 100644 --- a/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php +++ b/rules/coding-style/src/Rector/Use_/RemoveUnusedAliasRector.php @@ -5,17 +5,11 @@ declare(strict_types=1); namespace Rector\CodingStyle\Rector\Use_; use PhpParser\Node; -use PhpParser\Node\Expr\New_; -use PhpParser\Node\Expr\StaticCall; -use PhpParser\Node\Identifier; use PhpParser\Node\Name; use PhpParser\Node\Param; -use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Interface_; -use PhpParser\Node\Stmt\TraitUse; use PhpParser\Node\Stmt\Use_; use PhpParser\Node\Stmt\UseUse; +use Rector\CodingStyle\Naming\NameRenamer; use Rector\CodingStyle\Node\DocAliasResolver; use Rector\CodingStyle\Node\UseManipulator; use Rector\CodingStyle\Node\UseNameAliasToNameResolver; @@ -60,14 +54,21 @@ final class RemoveUnusedAliasRector extends AbstractRector */ private $useManipulator; + /** + * @var NameRenamer + */ + private $nameRenamer; + public function __construct( DocAliasResolver $docAliasResolver, UseManipulator $useManipulator, - UseNameAliasToNameResolver $useNameAliasToNameResolver + UseNameAliasToNameResolver $useNameAliasToNameResolver, + NameRenamer $nameRenamer ) { $this->docAliasResolver = $docAliasResolver; $this->useNameAliasToNameResolver = $useNameAliasToNameResolver; $this->useManipulator = $useManipulator; + $this->nameRenamer = $nameRenamer; } public function getRuleDefinition(): RuleDefinition @@ -217,7 +218,7 @@ CODE_SAMPLE return; } - $this->renameNameNode($this->resolvedNodeNames[$lowerAliasName], $lastName); + $this->nameRenamer->renameNameNode($this->resolvedNodeNames[$lowerAliasName], $lastName); $useUse->alias = null; } @@ -231,136 +232,4 @@ CODE_SAMPLE return false; } - - /** - * @param NameAndParent[] $usedNameNodes - */ - private function renameNameNode(array $usedNameNodes, string $lastName): void - { - foreach ($usedNameNodes as $nameAndParent) { - $parentNode = $nameAndParent->getParentNode(); - $usedName = $nameAndParent->getNameNode(); - - if ($parentNode instanceof TraitUse) { - $this->renameTraitUse($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Class_) { - $this->renameClass($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Param) { - $this->renameParam($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof New_) { - $this->renameNew($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof ClassMethod) { - $this->renameClassMethod($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof Interface_) { - $this->renameInterface($lastName, $parentNode, $usedName); - } - - if ($parentNode instanceof StaticCall) { - $this->renameStaticCall($lastName, $parentNode); - } - } - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameTraitUse(string $lastName, TraitUse $traitUse, Node $usedNameNode): void - { - foreach ($traitUse->traits as $key => $traitName) { - if (! $this->areNamesEqual($traitName, $usedNameNode)) { - continue; - } - - $traitUse->traits[$key] = new Name($lastName); - } - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameClass(string $lastName, Class_ $class, Node $usedNameNode): void - { - if ($class->name !== null && $this->areNamesEqual($class->name, $usedNameNode)) { - $class->name = new Identifier($lastName); - } - - if ($class->extends !== null && $this->areNamesEqual($class->extends, $usedNameNode)) { - $class->extends = new Name($lastName); - } - - foreach ($class->implements as $key => $implementNode) { - if ($this->areNamesEqual($implementNode, $usedNameNode)) { - $class->implements[$key] = new Name($lastName); - } - } - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameParam(string $lastName, Node $parentNode, Node $usedNameNode): void - { - if ($parentNode->type === null) { - return; - } - if (! $this->areNamesEqual($parentNode->type, $usedNameNode)) { - return; - } - $parentNode->type = new Name($lastName); - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameNew(string $lastName, Node $parentNode, Node $usedNameNode): void - { - if ($this->areNamesEqual($parentNode->class, $usedNameNode)) { - $parentNode->class = new Name($lastName); - } - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameClassMethod(string $lastName, ClassMethod $classMethod, Node $usedNameNode): void - { - if ($classMethod->returnType === null) { - return; - } - - if (! $this->areNamesEqual($classMethod->returnType, $usedNameNode)) { - return; - } - - $classMethod->returnType = new Name($lastName); - } - - /** - * @param Name|Identifier $usedNameNode - */ - private function renameInterface(string $lastName, Interface_ $interface, Node $usedNameNode): void - { - foreach ($interface->extends as $key => $extendInterfaceName) { - if (! $this->areNamesEqual($extendInterfaceName, $usedNameNode)) { - continue; - } - - $interface->extends[$key] = new Name($lastName); - } - } - - private function renameStaticCall(string $lastName, StaticCall $staticCall): void - { - $staticCall->class = new Name($lastName); - } } diff --git a/rules/php80/src/Rector/If_/NullsafeOperatorRector.php b/rules/php80/src/Rector/If_/NullsafeOperatorRector.php index a887933cf19..7efca4d3cd8 100644 --- a/rules/php80/src/Rector/If_/NullsafeOperatorRector.php +++ b/rules/php80/src/Rector/If_/NullsafeOperatorRector.php @@ -57,14 +57,14 @@ final class NullsafeOperatorRector extends AbstractRector <<<'CODE_SAMPLE' class SomeClass { - public function f($o) + public function run($someObject) { - $o2 = $o->mayFail1(); - if ($o2 === null) { + $someObject2 = $someObject->mayFail1(); + if ($someObject2 === null) { return null; } - return $o2->mayFail2(); + return $someObject2->mayFail2(); } } CODE_SAMPLE @@ -72,9 +72,9 @@ CODE_SAMPLE <<<'CODE_SAMPLE' class SomeClass { - public function f($o) + public function run($someObject) { - return $o->mayFail1()?->mayFail2(); + return $someObject->mayFail1()?->mayFail2(); } } CODE_SAMPLE diff --git a/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php b/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php index cdc503c63eb..b3d20b0e1a6 100644 --- a/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php +++ b/rules/solid/src/Rector/If_/ChangeIfElseValueAssignToEarlyReturnRector.php @@ -115,7 +115,7 @@ CODE_SAMPLE $lastElseStmtKey = array_key_last($node->else->stmts); $elseStmts = $node->else->stmts; - + $return = new Return_($assign->expr); $this->copyCommentIfExists($assign, $return); $elseStmts[$lastElseStmtKey] = $return;