diff --git a/.github/workflows/code_checks.yml b/.github/workflows/code_checks.yml index e96251d7ea0a..8053174cb2b5 100644 --- a/.github/workflows/code_checks.yml +++ b/.github/workflows/code_checks.yml @@ -75,6 +75,17 @@ jobs: coverage: none # disable xdebug, pcov - run: composer install --no-progress - run: composer rector + rector-ci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - + uses: shivammathur/setup-php@v1 + with: + php-version: 7.3 + coverage: none # disable xdebug, pcov + - run: composer install --no-progress + - run: composer rector-ci docs: runs-on: ubuntu-latest diff --git a/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/BlameableTagValueNode.php b/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/BlameableTagValueNode.php index f549d32bae9c..3ac44d5a799f 100644 --- a/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/BlameableTagValueNode.php +++ b/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/BlameableTagValueNode.php @@ -54,11 +54,8 @@ public function __toString(): string } if ($this->field) { - if (is_array($this->field)) { - $contentItems['field'] = $this->printArrayItem($this->field, 'field'); - } else { - $contentItems['field'] = $this->field; - } + $contentItems['field'] = is_array($this->field) ? + $this->printArrayItem($this->field, 'field') : $this->field; } if ($this->value !== null) { diff --git a/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/SoftDeleteableTagValueNode.php b/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/SoftDeleteableTagValueNode.php index b37e451afb60..366bb0050829 100644 --- a/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/SoftDeleteableTagValueNode.php +++ b/packages/BetterPhpDocParser/src/PhpDocNode/Gedmo/SoftDeleteableTagValueNode.php @@ -49,11 +49,11 @@ public function __toString(): string $contentItems['fieldName'] = $this->fieldName; } - if ($this->timeAware !== false) { + if ($this->timeAware) { $contentItems['timeAware'] = sprintf('strict=%s', $this->timeAware ? 'true' : 'false'); } - if ($this->hardDelete !== false) { + if ($this->hardDelete) { $contentItems['hardDelete'] = sprintf('strict=%s', $this->hardDelete ? 'true' : 'false'); } diff --git a/packages/BetterPhpDocParser/src/PhpDocParser/AnnotationContentResolver.php b/packages/BetterPhpDocParser/src/PhpDocParser/AnnotationContentResolver.php index 39dab63a3cd7..3ce643f8879c 100644 --- a/packages/BetterPhpDocParser/src/PhpDocParser/AnnotationContentResolver.php +++ b/packages/BetterPhpDocParser/src/PhpDocParser/AnnotationContentResolver.php @@ -121,14 +121,13 @@ private function tryStartWithKey(string $name, bool $start, TokenIterator $local return true; } - if ($localTokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) { - if ($localTokenIterator->currentTokenValue() === $name) { - // consume "=" as well - $localTokenIterator->next(); - $localTokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL); - - return true; - } + if ($localTokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER) && + $localTokenIterator->currentTokenValue() === $name + ) { + // consume "=" as well + $localTokenIterator->next(); + $localTokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL); + return true; } return false; diff --git a/packages/BetterPhpDocParser/src/Printer/MultilineSpaceFormatPreserver.php b/packages/BetterPhpDocParser/src/Printer/MultilineSpaceFormatPreserver.php index c223fb0c59b2..6d09804abb67 100644 --- a/packages/BetterPhpDocParser/src/Printer/MultilineSpaceFormatPreserver.php +++ b/packages/BetterPhpDocParser/src/Printer/MultilineSpaceFormatPreserver.php @@ -16,10 +16,10 @@ final class MultilineSpaceFormatPreserver { public function resolveCurrentPhpDocNodeText(AttributeAwareNodeInterface $attributeAwareNode): ?string { - if ($attributeAwareNode instanceof PhpDocTagNode) { - if (property_exists($attributeAwareNode->value, 'description')) { - return $attributeAwareNode->value->description; - } + if ($attributeAwareNode instanceof PhpDocTagNode && + property_exists($attributeAwareNode->value, 'description') + ) { + return $attributeAwareNode->value->description; } if ($attributeAwareNode instanceof PhpDocTextNode) { @@ -45,20 +45,19 @@ public function setNewTextToPhpDocNode( AttributeAwareNodeInterface $attributeAwareNode, string $newText ): AttributeAwareNodeInterface { - if ($attributeAwareNode instanceof PhpDocTagNode) { - if (property_exists($attributeAwareNode->value, 'description')) { - $attributeAwareNode->value->description = $newText; - } + if ($attributeAwareNode instanceof PhpDocTagNode && property_exists( + $attributeAwareNode->value, + 'description' + )) { + $attributeAwareNode->value->description = $newText; } if ($attributeAwareNode instanceof PhpDocTextNode) { $attributeAwareNode->text = $newText; } - if ($attributeAwareNode instanceof AttributeAwarePhpDocTagNode) { - if ($attributeAwareNode->value instanceof AttributeAwareGenericTagValueNode) { - $attributeAwareNode->value->value = $newText; - } + if ($attributeAwareNode instanceof AttributeAwarePhpDocTagNode && $attributeAwareNode->value instanceof AttributeAwareGenericTagValueNode) { + $attributeAwareNode->value->value = $newText; } return $attributeAwareNode; diff --git a/packages/BetterPhpDocParser/src/Printer/OriginalSpacingRestorer.php b/packages/BetterPhpDocParser/src/Printer/OriginalSpacingRestorer.php index 8e2d45577279..8138b952dc41 100644 --- a/packages/BetterPhpDocParser/src/Printer/OriginalSpacingRestorer.php +++ b/packages/BetterPhpDocParser/src/Printer/OriginalSpacingRestorer.php @@ -67,15 +67,16 @@ private function detectOldWhitespaces(Node $node, array $tokens, StartEndValueOb if ($tokens[$i][1] === Lexer::TOKEN_HORIZONTAL_WS) { $value = $tokens[$i][0]; - if ($node instanceof DoctrineTagNodeInterface) { - // give back "\s+\*" as well - if ($i - 1 > $start) { // do not overlap to previous node - if (isset($tokens[$i - 1]) && $tokens[$i - 1][1] === Lexer::TOKEN_PHPDOC_EOL) { - $previousTokenValue = $tokens[$i - 1][0]; - if (Strings::match($previousTokenValue, '#\s+\*#m')) { - $value = $previousTokenValue . $value; - } - } + // give back "\s+\*" as well + // do not overlap to previous node + if ($node instanceof DoctrineTagNodeInterface && + $i - 1 > $start && + isset($tokens[$i - 1]) && + $tokens[$i - 1][1] === Lexer::TOKEN_PHPDOC_EOL + ) { + $previousTokenValue = $tokens[$i - 1][0]; + if (Strings::match($previousTokenValue, '#\s+\*#m')) { + $value = $previousTokenValue . $value; } } diff --git a/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php b/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php index bc04acd35709..c3252cc2ac8f 100644 --- a/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php +++ b/packages/BetterPhpDocParser/src/Printer/PhpDocInfoPrinter.php @@ -207,14 +207,12 @@ private function addTokensFromTo( --$from; } - if ($shouldSkipEmptyLinesAbove) { - // skip extra empty lines above if this is the last one - if (Strings::contains($this->tokens[$from][0], PHP_EOL) && Strings::contains( - $this->tokens[$from + 1][0], - PHP_EOL - )) { - ++$from; - } + // skip extra empty lines above if this is the last one + if ($shouldSkipEmptyLinesAbove && + Strings::contains($this->tokens[$from][0], PHP_EOL) && + Strings::contains($this->tokens[$from + 1][0], PHP_EOL) + ) { + ++$from; } return $this->appendToOutput($output, $from, $to, $positionJumpSet); @@ -234,21 +232,15 @@ private function printPhpDocTagNode( $output .= ' '; } - if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES)) { - if (property_exists($phpDocTagNode->value, 'description') && $phpDocTagNode->value->description) { - $quotedDescription = preg_quote($phpDocTagNode->value->description, '#'); - - $pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+'); - - $nodeOutput = Strings::replace( - $nodeOutput, - '#' . $pattern . '#', - $phpDocTagNode->value->description - ); - - if (substr_count($nodeOutput, "\n") !== 0) { - $nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * '); - } + if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES) && (property_exists( + $phpDocTagNode->value, + 'description' + ) && $phpDocTagNode->value->description)) { + $quotedDescription = preg_quote($phpDocTagNode->value->description, '#'); + $pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+'); + $nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', $phpDocTagNode->value->description); + if (substr_count($nodeOutput, "\n") !== 0) { + $nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * '); } } diff --git a/packages/CakePHPToSymfony/src/Rector/AbstractCakePHPRector.php b/packages/CakePHPToSymfony/src/Rector/AbstractCakePHPRector.php index 214341ca800e..6e68f94c660d 100644 --- a/packages/CakePHPToSymfony/src/Rector/AbstractCakePHPRector.php +++ b/packages/CakePHPToSymfony/src/Rector/AbstractCakePHPRector.php @@ -17,10 +17,6 @@ protected function isInCakePHPController(Node $node): bool } $class = $node->getAttribute(AttributeKey::CLASS_NODE); - if ($class !== null) { - return true; - } - - return false; + return $class !== null; } } diff --git a/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerComponentToSymfonyRector.php b/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerComponentToSymfonyRector.php index 9cd93e1d5413..ab57ac2b531e 100644 --- a/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerComponentToSymfonyRector.php +++ b/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerComponentToSymfonyRector.php @@ -148,7 +148,7 @@ public function refactor(Node $node): ?Node */ private function getComponentClasses(): array { - if ($this->componentsClasses) { + if ($this->componentsClasses !== []) { return $this->componentsClasses; } diff --git a/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerHelperToSymfonyRector.php b/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerHelperToSymfonyRector.php index d12cab8bdd53..e0e5cf5d6c22 100644 --- a/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerHelperToSymfonyRector.php +++ b/packages/CakePHPToSymfony/src/Rector/Class_/CakePHPControllerHelperToSymfonyRector.php @@ -132,11 +132,6 @@ private function isThisFlashMethodCall(Node $node): bool if (! $this->isName($node->var->var, 'this')) { return false; } - - if (! $this->isName($node->var->name, 'Flash')) { - return false; - } - - return true; + return $this->isName($node->var->name, 'Flash'); } } diff --git a/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateLinkToTwigRector.php b/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateLinkToTwigRector.php index a3496c49f32a..f146850c18d9 100644 --- a/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateLinkToTwigRector.php +++ b/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateLinkToTwigRector.php @@ -81,12 +81,9 @@ public function refactor(Node $node): ?Node # e.g. |trans https://symfony.com/doc/current/translation/templates.html#using-twig-filters $labelFilters = []; - if ($label instanceof FuncCall) { - if ($this->isName($label, '__')) { - $labelFilters[] = 'trans'; - - $label = $label->args[0]->value; - } + if ($label instanceof FuncCall && $this->isName($label, '__')) { + $labelFilters[] = 'trans'; + $label = $label->args[0]->value; } $parametersValue = $this->getValue($parameters); @@ -112,23 +109,18 @@ private function isThisHtmlPropertyFetch(Expr $expr): bool if (! $this->isName($expr->var, 'this')) { return false; } - - if (! $this->isName($expr->name, 'Html')) { - return false; - } - - return true; + return $this->isName($expr->name, 'Html'); } private function createAHtml(string $routeName, array $labelFilters, string $labelValue): string { $aHtml = Html::el('a'); - $aHtml->href = sprintf('{{ path(\'%s\') }}', $routeName); + $aHtml->href = sprintf("{{ path('%s') }}", $routeName); if ($labelFilters !== []) { $labelFilterAsString = implode('|', $labelFilters); - $labelValue = sprintf('{{ \'%s\'|%s }}', $labelValue, $labelFilterAsString); + $labelValue = sprintf("{{ '%s'|%s }}", $labelValue, $labelFilterAsString); } $aHtml->setText($labelValue); diff --git a/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateTranslateToTwigRector.php b/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateTranslateToTwigRector.php index 88cd2ca87c8d..c6c37b005ae5 100644 --- a/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateTranslateToTwigRector.php +++ b/packages/CakePHPToSymfony/src/Rector/Echo_/CakePHPTemplateTranslateToTwigRector.php @@ -5,6 +5,7 @@ namespace Rector\CakePHPToSymfony\Rector\Echo_; use PhpParser\Node; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Echo_; use PhpParser\Node\Stmt\InlineHTML; use Rector\Rector\AbstractRector; @@ -37,7 +38,7 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $expr = $node->exprs[0]; - if (! $expr instanceof Node\Expr\FuncCall) { + if (! $expr instanceof FuncCall) { return null; } @@ -48,7 +49,7 @@ public function refactor(Node $node): ?Node $translatedValue = $expr->args[0]->value; $translatedValue = $this->getValue($translatedValue); - $html = sprintf('{{ \'%s\'|trans }}', $translatedValue); + $html = sprintf("{{ '%s'|trans }}", $translatedValue); return new InlineHTML($html); } diff --git a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php index 65d8466c0aee..c0ba491665ab 100644 --- a/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php +++ b/packages/CodeQuality/src/Rector/Array_/CallableThisArrayToAnonymousFunctionRector.php @@ -211,10 +211,8 @@ private function createAnonymousFunction(ClassMethod $classMethod, Node $node): $anonymousFunction->stmts[] = new Expression($innerMethodCall); } - if ($node instanceof Variable) { - if (! $this->isName($node, 'this')) { - $anonymousFunction->uses[] = new ClosureUse($node); - } + if ($node instanceof Variable && ! $this->isName($node, 'this')) { + $anonymousFunction->uses[] = new ClosureUse($node); } return $anonymousFunction; diff --git a/packages/CodeQuality/src/Rector/For_/ForRepeatedCountToOwnVariableRector.php b/packages/CodeQuality/src/Rector/For_/ForRepeatedCountToOwnVariableRector.php index 025bb0e94369..f514c85e328d 100644 --- a/packages/CodeQuality/src/Rector/For_/ForRepeatedCountToOwnVariableRector.php +++ b/packages/CodeQuality/src/Rector/For_/ForRepeatedCountToOwnVariableRector.php @@ -107,11 +107,7 @@ public function refactor(Node $node): ?Node protected function createCountedValueName(?string $valueName, ?Scope $scope): string { - if ($valueName === null) { - $countedValueName = self::DEFAULT_VARIABLE_COUNT_NAME; - } else { - $countedValueName = $valueName . 'Count'; - } + $countedValueName = $valueName === null ? self::DEFAULT_VARIABLE_COUNT_NAME : $valueName . 'Count'; return parent::createCountedValueName($countedValueName, $scope); } diff --git a/packages/CodeQuality/src/Rector/For_/ForToForeachRector.php b/packages/CodeQuality/src/Rector/For_/ForToForeachRector.php index 66b02c047206..7d99766f3ad7 100644 --- a/packages/CodeQuality/src/Rector/For_/ForToForeachRector.php +++ b/packages/CodeQuality/src/Rector/For_/ForToForeachRector.php @@ -182,14 +182,11 @@ private function isConditionMatch(array $condExprs): bool } // count($values) - if ($condExprs[0]->right instanceof FuncCall) { - if ($this->isName($condExprs[0]->right, 'count')) { - /** @var FuncCall $countFuncCall */ - $countFuncCall = $condExprs[0]->right; - $this->iteratedExpr = $countFuncCall->args[0]->value; - - return true; - } + if ($condExprs[0]->right instanceof FuncCall && $this->isName($condExprs[0]->right, 'count')) { + /** @var FuncCall $countFuncCall */ + $countFuncCall = $condExprs[0]->right; + $this->iteratedExpr = $countFuncCall->args[0]->value; + return true; } return false; @@ -288,10 +285,8 @@ private function isPartOfAssign(?Node $node): bool $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); while ($parentNode !== null && ! $parentNode instanceof Expression) { - if ($parentNode instanceof Assign) { - if ($this->areNodesEqual($parentNode->var, $previousNode)) { - return true; - } + if ($parentNode instanceof Assign && $this->areNodesEqual($parentNode->var, $previousNode)) { + return true; } $previousNode = $parentNode; diff --git a/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php b/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php index 78a5e549b6c1..0bf8f6bc2df4 100644 --- a/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php +++ b/packages/CodeQuality/src/Rector/Foreach_/SimplifyForeachToArrayFilterRector.php @@ -121,7 +121,7 @@ private function shouldSkip(Foreach_ $foreachNode): bool return true; } - if ($ifNode->elseifs) { + if ($ifNode->elseifs !== []) { return true; } diff --git a/packages/CodeQuality/src/Rector/If_/CombineIfRector.php b/packages/CodeQuality/src/Rector/If_/CombineIfRector.php index 2577d6b2b299..de31edb94096 100644 --- a/packages/CodeQuality/src/Rector/If_/CombineIfRector.php +++ b/packages/CodeQuality/src/Rector/If_/CombineIfRector.php @@ -69,6 +69,9 @@ public function refactor(Node $node): ?Node $subIf = $node->stmts[0]; $node->cond = new BooleanAnd($node->cond, $subIf->cond); $node->stmts = $subIf->stmts; + + $node->setAttribute('comments', array_merge($node->getComments(), $subIf->getComments())); + return $node; } @@ -82,7 +85,7 @@ private function shouldSkip(If_ $node): bool return true; } - if ($node->elseifs) { + if ($node->elseifs !== []) { return true; } @@ -93,10 +96,6 @@ private function shouldSkip(If_ $node): bool if ($node->stmts[0]->else !== null) { return true; } - - if ($node->stmts[0]->elseifs) { - return true; - } - return false; + return (bool) $node->stmts[0]->elseifs; } } diff --git a/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php b/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php index 4133ba0bc12d..fa5bd9aa7263 100644 --- a/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php +++ b/packages/CodeQuality/src/Rector/If_/RemoveAlwaysTrueConditionSetInConstructorRector.php @@ -213,10 +213,8 @@ private function resolveAssignedTypeInStmtsByPropertyName(array $stmts, string $ $this->traverseNodesWithCallable($stmts, function (Node $node) use ($propertyName, &$resolvedTypes): ?int { // skip constructor - if ($node instanceof ClassMethod) { - if ($this->isName($node, '__construct')) { - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } + if ($node instanceof ClassMethod && $this->isName($node, '__construct')) { + return NodeTraverser::DONT_TRAVERSE_CHILDREN; } if (! $this->isPropertyFetchAssignOfPropertyName($node, $propertyName)) { diff --git a/packages/CodeQuality/src/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php b/packages/CodeQuality/src/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php index 5d92df3fffc4..8406a2c860ea 100644 --- a/packages/CodeQuality/src/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php +++ b/packages/CodeQuality/src/Rector/Include_/AbsolutizeRequireAndIncludePathRector.php @@ -82,11 +82,9 @@ public function refactor(Node $node): ?Node } // add preslash to string - if (! Strings::startsWith($includeValue, '/')) { - // keep dots - if (! Strings::startsWith($includeValue, '.')) { - $node->expr->value = '/' . $includeValue; - } + // keep dots + if (! Strings::startsWith($includeValue, '/') && ! Strings::startsWith($includeValue, '.')) { + $node->expr->value = '/' . $includeValue; } $node->expr = new Concat(new Dir(), $node->expr); diff --git a/packages/CodeQuality/src/Rector/Return_/SimplifyUselessVariableRector.php b/packages/CodeQuality/src/Rector/Return_/SimplifyUselessVariableRector.php index 81080983e241..9ef0fabd54bd 100644 --- a/packages/CodeQuality/src/Rector/Return_/SimplifyUselessVariableRector.php +++ b/packages/CodeQuality/src/Rector/Return_/SimplifyUselessVariableRector.php @@ -133,12 +133,8 @@ private function shouldSkip(Return_ $returnNode): bool private function isPreviousExpressionVisuallySimilar(Expression $previousExpression, Node $previousNode): bool { $prePreviousExpression = $previousExpression->getAttribute(AttributeKey::PREVIOUS_STATEMENT); - if ($prePreviousExpression instanceof Expression && $prePreviousExpression->expr instanceof AssignOp) { - if ($this->areNodesEqual($prePreviousExpression->expr->var, $previousNode->var)) { - return true; - } - } - - return false; + return $prePreviousExpression instanceof Expression && + $prePreviousExpression->expr instanceof AssignOp && + $this->areNodesEqual($prePreviousExpression->expr->var, $previousNode->var); } } diff --git a/packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/docblock.php.inc b/packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/docblock.php.inc new file mode 100644 index 000000000000..dfa678de9cbc --- /dev/null +++ b/packages/CodeQuality/tests/Rector/If_/CombineIfRector/Fixture/docblock.php.inc @@ -0,0 +1,49 @@ + +----- + diff --git a/packages/CodingStyle/src/Application/UseAddingCommander.php b/packages/CodingStyle/src/Application/UseAddingCommander.php index 0379b7ece916..bdfdeba2e368 100644 --- a/packages/CodingStyle/src/Application/UseAddingCommander.php +++ b/packages/CodingStyle/src/Application/UseAddingCommander.php @@ -251,10 +251,10 @@ public function canImportBeAdded(Node $node, FullyQualifiedObjectType $fullyQual $useImportTypes = $this->getUseImportTypesByNode($node); foreach ($useImportTypes as $useImportType) { - if (! $useImportType->equals($fullyQualifiedObjectType)) { - if ($useImportType->areShortNamesEqual($fullyQualifiedObjectType)) { - return false; - } + if (! $useImportType->equals($fullyQualifiedObjectType) && + $useImportType->areShortNamesEqual($fullyQualifiedObjectType) + ) { + return false; } if ($useImportType->equals($fullyQualifiedObjectType)) { diff --git a/packages/CodingStyle/src/Imports/ShortNameResolver.php b/packages/CodingStyle/src/Imports/ShortNameResolver.php index eb6067735113..3dfb7046556a 100644 --- a/packages/CodingStyle/src/Imports/ShortNameResolver.php +++ b/packages/CodingStyle/src/Imports/ShortNameResolver.php @@ -115,11 +115,9 @@ private function resolveForNamespace(Namespace_ $node): array &$shortNames ): void { // class name is used! - if ($node instanceof ClassLike) { - if ($node->name instanceof Identifier) { - $shortNames[$node->name->toString()] = $node->name->toString(); - return; - } + if ($node instanceof ClassLike && $node->name instanceof Identifier) { + $shortNames[$node->name->toString()] = $node->name->toString(); + return; } if (! $node instanceof Name) { diff --git a/packages/CodingStyle/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php b/packages/CodingStyle/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php index 62cd51b79659..b5cae4b8c6d5 100644 --- a/packages/CodingStyle/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php +++ b/packages/CodingStyle/src/Rector/Encapsed/EncapsedStringsToSprintfRector.php @@ -96,10 +96,8 @@ public function refactor(Node $node): ?Node private function createSprintfFuncCallOrConcat(string $string, array $argumentVariables): Node { // special case for variable with PHP_EOL - if ($string === '%s') { - if (count($argumentVariables) === 2) { - return new Concat($argumentVariables[0], $argumentVariables[1]); - } + if ($string === '%s' && count($argumentVariables) === 2) { + return new Concat($argumentVariables[0], $argumentVariables[1]); } $arguments = [new Arg(new String_($string))]; diff --git a/packages/CodingStyle/src/Rector/FuncCall/ConsistentImplodeRector.php b/packages/CodingStyle/src/Rector/FuncCall/ConsistentImplodeRector.php index dc8f2a009195..32f0bf6b4e12 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/ConsistentImplodeRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/ConsistentImplodeRector.php @@ -82,10 +82,8 @@ public function refactor(Node $node): ?Node return null; } - if (count($node->args) === 2) { - if ($this->isStringOrUnionStringOnlyType($node->args[1]->value)) { - $node->args = array_reverse($node->args); - } + if (count($node->args) === 2 && $this->isStringOrUnionStringOnlyType($node->args[1]->value)) { + $node->args = array_reverse($node->args); } return $node; diff --git a/packages/CodingStyle/src/Rector/FuncCall/ConsistentPregDelimiterRector.php b/packages/CodingStyle/src/Rector/FuncCall/ConsistentPregDelimiterRector.php index fa359fac72e4..fd512526b762 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/ConsistentPregDelimiterRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/ConsistentPregDelimiterRector.php @@ -149,12 +149,10 @@ private function refactorArgument(Arg $arg): void $arg->value->value = Strings::replace($value, self::INNER_PATTERN_PATTERN, function (array $match): string { $innerPattern = $match['content']; - if (strlen($innerPattern) > 2) { - // change delimiter - if ($innerPattern[0] === $innerPattern[strlen($innerPattern) - 1]) { - $innerPattern[0] = $this->delimiter; - $innerPattern[strlen($innerPattern) - 1] = $this->delimiter; - } + // change delimiter + if (strlen($innerPattern) > 2 && $innerPattern[0] === $innerPattern[strlen($innerPattern) - 1]) { + $innerPattern[0] = $this->delimiter; + $innerPattern[strlen($innerPattern) - 1] = $this->delimiter; } return $innerPattern . $match['close']; diff --git a/packages/CodingStyle/src/Rector/String_/ManualJsonStringToJsonEncodeArrayRector.php b/packages/CodingStyle/src/Rector/String_/ManualJsonStringToJsonEncodeArrayRector.php index b967555c3d8a..57c07d28f964 100644 --- a/packages/CodingStyle/src/Rector/String_/ManualJsonStringToJsonEncodeArrayRector.php +++ b/packages/CodingStyle/src/Rector/String_/ManualJsonStringToJsonEncodeArrayRector.php @@ -353,10 +353,8 @@ private function isImplodeToJson(Node $node): bool } $firstArgumentValue = $node->args[0]->value; - if ($firstArgumentValue instanceof String_) { - if ($firstArgumentValue->value !== '","') { - return false; - } + if ($firstArgumentValue instanceof String_ && $firstArgumentValue->value !== '","') { + return false; } return true; diff --git a/packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php b/packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php index e124916f9eb5..544b55061efd 100644 --- a/packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php +++ b/packages/CodingStyle/src/Rector/Use_/RemoveUnusedAliasRector.php @@ -18,6 +18,8 @@ use PhpParser\Node\Stmt\Use_; use PhpParser\Node\Stmt\UseUse; use PhpParser\NodeVisitor\NameResolver; +use PHPStan\Type\Type; +use PHPStan\Type\UnionType; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\CodingStyle\Imports\ShortNameResolver; use Rector\CodingStyle\Naming\ClassNaming; @@ -203,16 +205,12 @@ private function renameNameNode(array $usedNameNodes, string $lastName): void } if ($parentNode instanceof Class_) { - if ($parentNode->name !== null) { - if ($this->areNamesEqual($parentNode->name, $usedName)) { - $parentNode->name = new Identifier($lastName); - } + if ($parentNode->name !== null && $this->areNamesEqual($parentNode->name, $usedName)) { + $parentNode->name = new Identifier($lastName); } - if ($parentNode->extends !== null) { - if ($this->areNamesEqual($parentNode->extends, $usedName)) { - $parentNode->extends = new Name($lastName); - } + if ($parentNode->extends !== null && $this->areNamesEqual($parentNode->extends, $usedName)) { + $parentNode->extends = new Name($lastName); } foreach ($parentNode->implements as $key => $implementNode) { @@ -225,10 +223,8 @@ private function renameNameNode(array $usedNameNodes, string $lastName): void } if ($parentNode instanceof Param) { - if ($parentNode->type !== null) { - if ($this->areNamesEqual($parentNode->type, $usedName)) { - $parentNode->type = new Name($lastName); - } + if ($parentNode->type !== null && $this->areNamesEqual($parentNode->type, $usedName)) { + $parentNode->type = new Name($lastName); } continue; @@ -243,10 +239,8 @@ private function renameNameNode(array $usedNameNodes, string $lastName): void } if ($parentNode instanceof ClassMethod) { - if ($parentNode->returnType !== null) { - if ($this->areNamesEqual($parentNode->returnType, $usedName)) { - $parentNode->returnType = new Name($lastName); - } + if ($parentNode->returnType !== null && $this->areNamesEqual($parentNode->returnType, $usedName)) { + $parentNode->returnType = new Name($lastName); } continue; @@ -335,21 +329,13 @@ private function resolveDocPossibleAliases(Node $searchNode): array /** @var PhpDocInfo $phpDocInfo */ $phpDocInfo = $this->getPhpDocInfo($node); - if ($phpDocInfo->getVarType()) { - $varType = $phpDocInfo->getVarType(); - if ($varType instanceof AliasedObjectType) { - $possibleDocAliases[] = $varType->getClassName(); - } - $returnType = $phpDocInfo->getReturnType(); - if ($returnType instanceof AliasedObjectType) { - $possibleDocAliases[] = $returnType->getClassName(); - } + if ($phpDocInfo->getVarType()) { + $possibleDocAliases = $this->appendPossibleAliases($phpDocInfo->getVarType(), $possibleDocAliases); + $possibleDocAliases = $this->appendPossibleAliases($phpDocInfo->getReturnType(), $possibleDocAliases); foreach ($phpDocInfo->getParamTypes() as $paramType) { - if ($paramType instanceof AliasedObjectType) { - $possibleDocAliases[] = $paramType->getClassName(); - } + $possibleDocAliases = $this->appendPossibleAliases($paramType, $possibleDocAliases); } } @@ -362,4 +348,20 @@ private function resolveDocPossibleAliases(Node $searchNode): array return array_unique($possibleDocAliases); } + + /** + * @return string[] + */ + private function appendPossibleAliases(Type $varType, array $possibleDocAliases): array + { + if ($varType instanceof AliasedObjectType) { + $possibleDocAliases[] = $varType->getClassName(); + } + if ($varType instanceof UnionType) { + foreach ($varType->getTypes() as $type) { + $possibleDocAliases = $this->appendPossibleAliases($type, $possibleDocAliases); + } + } + return $possibleDocAliases; + } } diff --git a/packages/CodingStyle/tests/Rector/Use_/RemoveUnusedAliasRector/Fixture/doc_block_uniontype.php.inc b/packages/CodingStyle/tests/Rector/Use_/RemoveUnusedAliasRector/Fixture/doc_block_uniontype.php.inc new file mode 100644 index 000000000000..5c29e58c8104 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/Use_/RemoveUnusedAliasRector/Fixture/doc_block_uniontype.php.inc @@ -0,0 +1,19 @@ +getByType(DoctrineRelationTagValueNodeInterface::class); $shouldUpdate = false; - if ($relationTagValueNode instanceof MappedByNodeInterface) { - if ($relationTagValueNode->getMappedBy()) { - $shouldUpdate = true; - $relationTagValueNode->removeMappedBy(); - } + if ($relationTagValueNode instanceof MappedByNodeInterface && $relationTagValueNode->getMappedBy()) { + $shouldUpdate = true; + $relationTagValueNode->removeMappedBy(); } - if ($relationTagValueNode instanceof InversedByNodeInterface) { - if ($relationTagValueNode->getInversedBy()) { - $shouldUpdate = true; - $relationTagValueNode->removeInversedBy(); - } + if ($relationTagValueNode instanceof InversedByNodeInterface && $relationTagValueNode->getInversedBy()) { + $shouldUpdate = true; + $relationTagValueNode->removeInversedBy(); } if (! $shouldUpdate) { diff --git a/packages/DeadCode/src/Rector/ClassMethod/RemoveOverriddenValuesRector.php b/packages/DeadCode/src/Rector/ClassMethod/RemoveOverriddenValuesRector.php index 897485855fc7..1733c2ba78d5 100644 --- a/packages/DeadCode/src/Rector/ClassMethod/RemoveOverriddenValuesRector.php +++ b/packages/DeadCode/src/Rector/ClassMethod/RemoveOverriddenValuesRector.php @@ -159,11 +159,9 @@ private function resolveUsedVariables(Node $node, array $assignedVariables): arr } $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Assign) { - // is the left assign - not use of one - if ($parentNode->var instanceof Variable && $parentNode->var === $node) { - return false; - } + // is the left assign - not use of one + if ($parentNode instanceof Assign && ($parentNode->var instanceof Variable && $parentNode->var === $node)) { + return false; } // simple variable only @@ -270,15 +268,11 @@ private function isAssignNodeUsed( VariableNodeUseInfo $nodeByTypeAndPosition ): bool { // this node was just used, skip to next one - if ($previousNode !== null) { - if ($previousNode->isType(VariableNodeUseInfo::TYPE_ASSIGN) && $nodeByTypeAndPosition->isType( - VariableNodeUseInfo::TYPE_USE - )) { - return true; - } - } - - return false; + return $previousNode !== null && ($previousNode->isType( + VariableNodeUseInfo::TYPE_ASSIGN + ) && $nodeByTypeAndPosition->isType( + VariableNodeUseInfo::TYPE_USE + )); } private function shouldRemoveAssignNode( diff --git a/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php b/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php index 936c76166e27..328f5699ea36 100644 --- a/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php +++ b/packages/DeadCode/src/Rector/MethodCall/RemoveDefaultArgumentValueRector.php @@ -195,10 +195,8 @@ private function resolveKeysToRemove(Node $node, array $defaultValues): array return []; } - if ($keysToKeep !== []) { - if (max($keysToKeep) > max($keysToRemove)) { - return []; - } + if ($keysToKeep !== [] && max($keysToKeep) > max($keysToRemove)) { + return []; } return $keysToRemove; diff --git a/packages/DeadCode/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php b/packages/DeadCode/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php index 919df8b6e5f8..31b61346e506 100644 --- a/packages/DeadCode/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php +++ b/packages/DeadCode/src/Rector/Plus/RemoveDeadZeroAndOneOperationRector.php @@ -148,19 +148,15 @@ private function processBinaryOp(Node $node): ?Expr */ private function processBinaryPlusAndMinus(BinaryOp $binaryOp): ?Expr { - if ($this->isValue($binaryOp->left, 0)) { - if ($this->isNumberType($binaryOp->right)) { - if ($binaryOp instanceof Minus) { - return new UnaryMinus($binaryOp->right); - } - return $binaryOp->right; + if ($this->isValue($binaryOp->left, 0) && $this->isNumberType($binaryOp->right)) { + if ($binaryOp instanceof Minus) { + return new UnaryMinus($binaryOp->right); } + return $binaryOp->right; } - if ($this->isValue($binaryOp->right, 0)) { - if ($this->isNumberType($binaryOp->left)) { - return $binaryOp->left; - } + if ($this->isValue($binaryOp->right, 0) && $this->isNumberType($binaryOp->left)) { + return $binaryOp->left; } return null; @@ -171,18 +167,12 @@ private function processBinaryPlusAndMinus(BinaryOp $binaryOp): ?Expr */ private function processBinaryMulAndDiv(BinaryOp $binaryOp): ?Expr { - if ($binaryOp instanceof Mul) { - if ($this->isValue($binaryOp->left, 1)) { - if ($this->isNumberType($binaryOp->right)) { - return $binaryOp->right; - } - } + if ($binaryOp instanceof Mul && $this->isValue($binaryOp->left, 1) && $this->isNumberType($binaryOp->right)) { + return $binaryOp->right; } - if ($this->isValue($binaryOp->right, 1)) { - if ($this->isNumberType($binaryOp->left)) { - return $binaryOp->left; - } + if ($this->isValue($binaryOp->right, 1) && $this->isNumberType($binaryOp->left)) { + return $binaryOp->left; } return null; diff --git a/packages/DeadCode/src/UnusedNodeResolver/UnusedClassResolver.php b/packages/DeadCode/src/UnusedNodeResolver/UnusedClassResolver.php index af40fa0443f0..61ee32ef2bd2 100644 --- a/packages/DeadCode/src/UnusedNodeResolver/UnusedClassResolver.php +++ b/packages/DeadCode/src/UnusedNodeResolver/UnusedClassResolver.php @@ -45,10 +45,8 @@ public function __construct(NameResolver $nameResolver, ParsedNodesByType $parse */ public function getUsedClassNames(): array { - if (! PHPUnitEnvironment::isPHPUnitRun()) { - if ($this->cachedUsedClassNames !== []) { - return $this->cachedUsedClassNames; - } + if (! PHPUnitEnvironment::isPHPUnitRun() && $this->cachedUsedClassNames !== []) { + return $this->cachedUsedClassNames; } $cachedUsedClassNames = array_merge( @@ -76,12 +74,7 @@ public function isClassWithoutInterfaceAndNotController(Class_ $class): bool if ($this->nameResolver->isNames($class, ['*Controller', '*Presenter'])) { return false; } - - if ($this->nameResolver->isName($class, '*Test')) { - return false; - } - - return true; + return ! $this->nameResolver->isName($class, '*Test'); } public function isClassUsed(Class_ $class): bool diff --git a/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php b/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php index 5df024529793..4ef32680c3fb 100644 --- a/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php +++ b/packages/Doctrine/src/Rector/Class_/AddUuidMirrorForRelationPropertyRector.php @@ -111,14 +111,8 @@ private function shouldSkipProperty(Class_ $class, Property $property): bool } $oneToOneTagValueNode = $propertyPhpDocInfo->getByType(OneToOneTagValueNode::class); - if ($oneToOneTagValueNode !== null) { - // skip mappedBy oneToOne, as the column doesn't really exist - if ($oneToOneTagValueNode->getMappedBy()) { - return true; - } - } - - return false; + // skip mappedBy oneToOne, as the column doesn't really exist + return $oneToOneTagValueNode !== null && $oneToOneTagValueNode->getMappedBy(); } /** diff --git a/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php b/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php index b5a6c3bfc1f9..b8cffdab9acd 100644 --- a/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php +++ b/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php @@ -214,10 +214,8 @@ private function resolveLastEmptyLine(string $prettyPrintContent): string { $tokens = $this->lexer->getTokens(); $lastToken = array_pop($tokens); - if ($lastToken) { - if (Strings::contains($lastToken[1], "\n")) { - $prettyPrintContent = trim($prettyPrintContent) . PHP_EOL; - } + if ($lastToken && Strings::contains($lastToken[1], "\n")) { + $prettyPrintContent = trim($prettyPrintContent) . PHP_EOL; } return $prettyPrintContent; diff --git a/packages/Laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php b/packages/Laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php index 2c62231df088..32ea9edd5c73 100644 --- a/packages/Laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php +++ b/packages/Laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php @@ -114,10 +114,8 @@ private function getTypesToMethods(): array private function processArgumentPosition(Expr $expr, int $argumentPosition): Expr { $oldValue = $expr->args[$argumentPosition]->value; - if (! $oldValue instanceof LNumber) { - if (! $this->getStaticType($oldValue) instanceof ConstantIntegerType) { - return $expr; - } + if (! $oldValue instanceof LNumber && ! $this->getStaticType($oldValue) instanceof ConstantIntegerType) { + return $expr; } $newArgumentValue = new Mul($oldValue, new LNumber(60)); diff --git a/packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php b/packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php index 7f47d7af3e7d..120bf5501385 100644 --- a/packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php +++ b/packages/Legacy/src/NodeAnalyzer/SingletonClassMethodAnalyzer.php @@ -117,10 +117,8 @@ private function matchStaticPropertyFetchInIfCond(Expr $expr): ?StaticPropertyFe } // matching: "! self::$static" - if ($expr instanceof BooleanNot) { - if ($expr->expr instanceof StaticPropertyFetch) { - return $expr->expr; - } + if ($expr instanceof BooleanNot && $expr->expr instanceof StaticPropertyFetch) { + return $expr->expr; } return null; diff --git a/packages/MinimalScope/src/Rector/Class_/ChangeLocalPropertyToVariableRector.php b/packages/MinimalScope/src/Rector/Class_/ChangeLocalPropertyToVariableRector.php index 907d497a8bed..d2970fa12dcc 100644 --- a/packages/MinimalScope/src/Rector/Class_/ChangeLocalPropertyToVariableRector.php +++ b/packages/MinimalScope/src/Rector/Class_/ChangeLocalPropertyToVariableRector.php @@ -169,7 +169,7 @@ private function collectPropertyFetchByMethods(Class_ $class, array $privateProp return (bool) $this->isName($node->name, $privatePropertyName); }); - if ($hasProperty === false) { + if (! $hasProperty) { continue; } @@ -211,10 +211,8 @@ private function isPropertyChangingInMultipleMethodCalls( } $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Assign) { - if ($parentNode->var === $node) { - $isIfFollowedByAssign = true; - } + if ($parentNode instanceof Assign && $parentNode->var === $node) { + $isIfFollowedByAssign = true; } } @@ -259,7 +257,7 @@ private function isPropertyChangingInMultipleMethodCalls( return NodeTraverser::STOP_TRAVERSAL; }); - if ($isPropertyChanging === false) { + if (! $isPropertyChanging) { return null; } diff --git a/packages/Nette/src/TemplatePropertyAssignCollector.php b/packages/Nette/src/TemplatePropertyAssignCollector.php index c80cb52a874c..6ee6097771ad 100644 --- a/packages/Nette/src/TemplatePropertyAssignCollector.php +++ b/packages/Nette/src/TemplatePropertyAssignCollector.php @@ -102,10 +102,8 @@ private function collectVariableFromAssign(Assign $assign): void } // $x = $this->template - if ($assign->var instanceof Variable) { - if ($this->isTemplatePropertyFetch($assign->expr)) { - $this->nodesToRemove[] = $assign; - } + if ($assign->var instanceof Variable && $this->isTemplatePropertyFetch($assign->expr)) { + $this->nodesToRemove[] = $assign; } } diff --git a/packages/NodeTypeResolver/src/NodeTypeResolver.php b/packages/NodeTypeResolver/src/NodeTypeResolver.php index d7f70971278b..e825376a40fa 100644 --- a/packages/NodeTypeResolver/src/NodeTypeResolver.php +++ b/packages/NodeTypeResolver/src/NodeTypeResolver.php @@ -177,10 +177,8 @@ public function isObjectType(Node $node, $requiredType): bool { $this->ensureRequiredTypeIsStringOrObjectType($requiredType, __METHOD__); - if (is_string($requiredType)) { - if (Strings::contains($requiredType, '*')) { - return $this->isFnMatch($node, $requiredType); - } + if (is_string($requiredType) && Strings::contains($requiredType, '*')) { + return $this->isFnMatch($node, $requiredType); } $resolvedType = $this->getObjectType($node); @@ -199,10 +197,12 @@ public function isObjectType(Node $node, $requiredType): bool return true; } - if ($resolvedType instanceof TypeWithClassName) { - if (is_a($resolvedType->getClassName(), $requiredType->getClassName(), true)) { - return true; - } + if ($resolvedType instanceof TypeWithClassName && is_a( + $resolvedType->getClassName(), + $requiredType->getClassName(), + true + )) { + return true; } if ($this->isUnionNullTypeOfRequiredType($requiredType, $resolvedType)) { @@ -319,11 +319,11 @@ public function isArrayType(Node $node): bool return true; } - if ($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch) { - // PHPStan false positive, when variable has type[] docblock, but default array is missing - if (! $this->isPropertyFetchWithArrayDefault($node)) { - return false; - } + // PHPStan false positive, when variable has type[] docblock, but default array is missing + if (($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch) && ! $this->isPropertyFetchWithArrayDefault( + $node + )) { + return false; } if ($nodeStaticType instanceof MixedType) { @@ -355,19 +355,15 @@ public function getStaticType(Node $node): Type /** @var Scope|null $nodeScope */ $nodeScope = $node->getAttribute(AttributeKey::SCOPE); - if ($node instanceof Scalar) { - if ($nodeScope === null) { - if ($node instanceof DNumber) { - return new ConstantFloatType($node->value); - } - - if ($node instanceof String_) { - return new ConstantStringType($node->value); - } - - if ($node instanceof LNumber) { - return new ConstantIntegerType($node->value); - } + if ($node instanceof Scalar && $nodeScope === null) { + if ($node instanceof DNumber) { + return new ConstantFloatType($node->value); + } + if ($node instanceof String_) { + return new ConstantStringType($node->value); + } + if ($node instanceof LNumber) { + return new ConstantIntegerType($node->value); } } @@ -375,19 +371,15 @@ public function getStaticType(Node $node): Type return new MixedType(); } - if ($node instanceof New_) { - if ($this->isAnonymousClass($node->class)) { - return new ObjectWithoutClassType(); - } + if ($node instanceof New_ && $this->isAnonymousClass($node->class)) { + return new ObjectWithoutClassType(); } // false type correction of inherited method - if ($node instanceof MethodCall) { - if ($this->isObjectType($node->var, SplFileInfo::class)) { - $methodName = $this->nameResolver->getName($node->name); - if ($methodName === 'getRealPath') { - return new UnionType([new StringType(), new ConstantBooleanType(false)]); - } + if ($node instanceof MethodCall && $this->isObjectType($node->var, SplFileInfo::class)) { + $methodName = $this->nameResolver->getName($node->name); + if ($methodName === 'getRealPath') { + return new UnionType([new StringType(), new ConstantBooleanType(false)]); } } @@ -601,10 +593,8 @@ private function resolveFirstType(Node $node): Type } // skip anonymous classes, ref https://github.com/rectorphp/rector/issues/1574 - if ($node instanceof New_) { - if ($this->isAnonymousClass($node->class)) { - return new ObjectWithoutClassType(); - } + if ($node instanceof New_ && $this->isAnonymousClass($node->class)) { + return new ObjectWithoutClassType(); } $type = $nodeScope->getType($node); @@ -841,7 +831,7 @@ private function getVendorPropertyFetchType(PropertyFetch $propertyFetch): ?Type return null; } - if ($this->parsedNodesByType->findClass($varObjectType->getClassName())) { + if ($this->parsedNodesByType->findClass($varObjectType->getClassName()) !== null) { return null; } diff --git a/packages/NodeTypeResolver/src/PHPStan/Type/TypeFactory.php b/packages/NodeTypeResolver/src/PHPStan/Type/TypeFactory.php index 6de5ad0a730c..81fbf5827f76 100644 --- a/packages/NodeTypeResolver/src/PHPStan/Type/TypeFactory.php +++ b/packages/NodeTypeResolver/src/PHPStan/Type/TypeFactory.php @@ -92,9 +92,7 @@ private function unwrapUnionedTypes(array $types): array $unwrappedTypes = []; foreach ($types as $key => $type) { if ($type instanceof UnionType) { - foreach ($type->getTypes() as $subUnionedType) { - $unwrappedTypes[] = $subUnionedType; - } + $unwrappedTypes = $type->getTypes(); unset($types[$key]); } diff --git a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php index 124c87baacfe..0a13cbca70df 100644 --- a/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php +++ b/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockManipulator.php @@ -255,12 +255,8 @@ public function changeVarTag(Node $node, Type $newType): void } // prevent existing type override by mixed - if (! $currentVarType instanceof MixedType) { - if ($newType instanceof ConstantArrayType) { - if ($newType->getItemType() instanceof NeverType) { - return; - } - } + if (! $currentVarType instanceof MixedType && $newType instanceof ConstantArrayType && $newType->getItemType() instanceof NeverType) { + return; } if ($this->hasTag($node, '@var')) { @@ -478,11 +474,12 @@ public function hasNodeTypeTags(Node $node): bool // has any type node? foreach ($phpDocInfo->getPhpDocNode()->children as $phpDocChildNode) { - if ($phpDocChildNode instanceof PhpDocTagNode) { - // is custom class, it can contain some type info - if (Strings::startsWith(get_class($phpDocChildNode->value), 'Rector\\')) { - return true; - } + // is custom class, it can contain some type info + if ($phpDocChildNode instanceof PhpDocTagNode && Strings::startsWith( + get_class($phpDocChildNode->value), + 'Rector\\' + )) { + return true; } } @@ -668,18 +665,9 @@ private function areBothSameScalarType(Type $firstType, Type $secondType): bool private function areAliasedObjectMatchingFqnObject(Type $firstType, Type $secondType): bool { - if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType) { - if ($firstType->getFullyQualifiedClass() === $secondType->getClassName()) { - return true; - } - } - - if ($secondType instanceof AliasedObjectType && $firstType instanceof ObjectType) { - if ($secondType->getFullyQualifiedClass() === $firstType->getClassName()) { - return true; - } + if ($firstType instanceof AliasedObjectType && $secondType instanceof ObjectType && $firstType->getFullyQualifiedClass() === $secondType->getClassName()) { + return true; } - - return false; + return $secondType instanceof AliasedObjectType && $firstType instanceof ObjectType && $secondType->getFullyQualifiedClass() === $firstType->getClassName(); } } diff --git a/packages/NodeTypeResolver/src/StaticTypeMapper.php b/packages/NodeTypeResolver/src/StaticTypeMapper.php index 03ffe731600c..e30900f7197a 100644 --- a/packages/NodeTypeResolver/src/StaticTypeMapper.php +++ b/packages/NodeTypeResolver/src/StaticTypeMapper.php @@ -272,12 +272,7 @@ public function mapPHPStanPhpDocTypeNodeToPHPStanType(TypeNode $typeNode, Node $ return $type; } - $customType = $this->customMapPHPStanPhpDocTypeNodeToPHPStanType($typeNode, $node); - if ($type === $customType) { - return $type; - } - - return $customType; + return $this->customMapPHPStanPhpDocTypeNodeToPHPStanType($typeNode, $node); } /** diff --git a/packages/PHPStanStaticTypeMapper/src/TypeAnalyzer/UnionTypeAnalyzer.php b/packages/PHPStanStaticTypeMapper/src/TypeAnalyzer/UnionTypeAnalyzer.php index 0dd5a5fee53a..924cbc0e587a 100644 --- a/packages/PHPStanStaticTypeMapper/src/TypeAnalyzer/UnionTypeAnalyzer.php +++ b/packages/PHPStanStaticTypeMapper/src/TypeAnalyzer/UnionTypeAnalyzer.php @@ -34,11 +34,9 @@ public function analyseForNullableAndIterable(UnionType $unionType): ?UnionTypeA continue; } - if ($unionedType instanceof ObjectType) { - if ($unionedType->getClassName() === Traversable::class) { - $hasIterable = true; - continue; - } + if ($unionedType instanceof ObjectType && $unionedType->getClassName() === Traversable::class) { + $hasIterable = true; + continue; } return null; diff --git a/packages/PHPStanStaticTypeMapper/src/TypeMapper/ObjectTypeMapper.php b/packages/PHPStanStaticTypeMapper/src/TypeMapper/ObjectTypeMapper.php index ba7cd27d29d8..4022d876fee2 100644 --- a/packages/PHPStanStaticTypeMapper/src/TypeMapper/ObjectTypeMapper.php +++ b/packages/PHPStanStaticTypeMapper/src/TypeMapper/ObjectTypeMapper.php @@ -57,10 +57,8 @@ public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node return new FullyQualified($type->getClassName()); } - if ($type instanceof GenericObjectType) { - if ($type->getClassName() === 'object') { - return new Identifier('object'); - } + if ($type instanceof GenericObjectType && $type->getClassName() === 'object') { + return new Identifier('object'); } // fallback diff --git a/packages/PHPUnit/src/Manipulator/OnContainerGetCallManipulator.php b/packages/PHPUnit/src/Manipulator/OnContainerGetCallManipulator.php index 05f6ae748cd1..72eafdf0c884 100644 --- a/packages/PHPUnit/src/Manipulator/OnContainerGetCallManipulator.php +++ b/packages/PHPUnit/src/Manipulator/OnContainerGetCallManipulator.php @@ -114,10 +114,8 @@ public function removeAndCollectFormerAssignedVariables(Class_ $class, bool $ski return null; } - if ($skipSetUpMethod) { - if ($this->kernelTestCaseNodeAnalyzer->isSetUpOrEmptyMethod($node)) { - return null; - } + if ($skipSetUpMethod && $this->kernelTestCaseNodeAnalyzer->isSetUpOrEmptyMethod($node)) { + return null; } if (! $this->kernelTestCaseNodeAnalyzer->isOnContainerGetMethodCall($node)) { diff --git a/packages/PHPUnit/src/Rector/GetMockRector.php b/packages/PHPUnit/src/Rector/GetMockRector.php index e6c4100e41bb..70399367cb75 100644 --- a/packages/PHPUnit/src/Rector/GetMockRector.php +++ b/packages/PHPUnit/src/Rector/GetMockRector.php @@ -48,10 +48,8 @@ public function refactor(Node $node): ?Node return null; } - if ($node instanceof MethodCall) { - if ($node->var instanceof MethodCall) { - return null; - } + if ($node instanceof MethodCall && $node->var instanceof MethodCall) { + return null; } // narrow args to one diff --git a/packages/PHPUnit/src/Rector/MethodCall/WithConsecutiveArgToArrayRector.php b/packages/PHPUnit/src/Rector/MethodCall/WithConsecutiveArgToArrayRector.php index 7ada5bba4917..ad21554a792a 100644 --- a/packages/PHPUnit/src/Rector/MethodCall/WithConsecutiveArgToArrayRector.php +++ b/packages/PHPUnit/src/Rector/MethodCall/WithConsecutiveArgToArrayRector.php @@ -177,12 +177,12 @@ private function inferMockedClassName(MethodCall $methodCall): ?string if ($assignToVariable->expr instanceof MethodCall) { /** @var MethodCall $assignedMethodCall */ $assignedMethodCall = $assignToVariable->expr; - if ($this->isName($assignedMethodCall->var, 'this')) { - if ($this->isName($assignedMethodCall->name, 'createMock')) { - $firstArgumentValue = $assignedMethodCall->args[0]->value; - - return $this->getValue($firstArgumentValue); - } + if ($this->isName($assignedMethodCall->var, 'this') && $this->isName( + $assignedMethodCall->name, + 'createMock' + )) { + $firstArgumentValue = $assignedMethodCall->args[0]->value; + return $this->getValue($firstArgumentValue); } } diff --git a/packages/PHPUnit/src/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php b/packages/PHPUnit/src/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php index f3cbe38ff57e..7bc609cbd8c7 100644 --- a/packages/PHPUnit/src/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php +++ b/packages/PHPUnit/src/Rector/SpecificMethod/AssertTrueFalseToSpecificMethodRector.php @@ -96,16 +96,12 @@ private function renameMethod(MethodCall $methodCall, string $funcName): void [$trueMethodName, $falseMethodName] = $this->oldToNewMethods[$funcName]; - if ($trueMethodName) { - if (in_array($oldMethodName, ['assertTrue', 'assertNotFalse'], true)) { - $methodCall->name = new Identifier($trueMethodName); - } + if ($trueMethodName && in_array($oldMethodName, ['assertTrue', 'assertNotFalse'], true)) { + $methodCall->name = new Identifier($trueMethodName); } - if ($falseMethodName) { - if (in_array($oldMethodName, ['assertFalse', 'assertNotTrue'], true)) { - $methodCall->name = new Identifier($falseMethodName); - } + if ($falseMethodName && in_array($oldMethodName, ['assertFalse', 'assertNotTrue'], true)) { + $methodCall->name = new Identifier($falseMethodName); } } diff --git a/packages/PSR4/src/Rector/Namespace_/NormalizeNamespaceByPSR4ComposerAutoloadRector.php b/packages/PSR4/src/Rector/Namespace_/NormalizeNamespaceByPSR4ComposerAutoloadRector.php index bbeeae0d3626..d29f72710d29 100644 --- a/packages/PSR4/src/Rector/Namespace_/NormalizeNamespaceByPSR4ComposerAutoloadRector.php +++ b/packages/PSR4/src/Rector/Namespace_/NormalizeNamespaceByPSR4ComposerAutoloadRector.php @@ -84,11 +84,11 @@ public function refactor(Node $node): ?Node /** @var Name|null $originalName */ $originalName = $node->getAttribute('originalName'); - if ($originalName instanceof Name) { - if ($currentNamespace . '\\' . $originalName->toString() === $this->getName($node)) { - // this needs to be imported - $newUseImports[] = $this->getName($node); - } + if ($originalName instanceof Name && + $currentNamespace . '\\' . $originalName->toString() === $this->getName($node) + ) { + // this needs to be imported + $newUseImports[] = $this->getName($node); } }); diff --git a/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php b/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php index 0a70035319b3..387a658f2050 100644 --- a/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php +++ b/packages/Php52/src/Rector/Switch_/ContinueToBreakInSwitchRector.php @@ -100,12 +100,8 @@ private function processVariableNum(Continue_ $node, Variable $numVariable): Nod { $staticType = $this->getStaticType($numVariable); - if ($staticType instanceof ConstantType) { - if ($staticType instanceof ConstantIntegerType) { - if ($staticType->getValue() <= 1) { - return new Break_(); - } - } + if ($staticType instanceof ConstantType && $staticType instanceof ConstantIntegerType && $staticType->getValue() <= 1) { + return new Break_(); } return $node; diff --git a/packages/Php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php b/packages/Php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php index d770292a4ed9..20eea569ef66 100644 --- a/packages/Php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php +++ b/packages/Php56/src/Rector/FunctionLike/AddDefaultValueForUndefinedVariableRector.php @@ -136,12 +136,10 @@ private function shouldSkipVariable(Variable $variable): bool return true; } - if ($parentNode instanceof Node) { - if ($parentNode instanceof Assign || $parentNode instanceof AssignRef || $this->isStaticVariable( - $parentNode - )) { - return true; - } + if ($parentNode instanceof Node && + ($parentNode instanceof Assign || $parentNode instanceof AssignRef || $this->isStaticVariable($parentNode) + )) { + return true; } if ($parentNode instanceof Unset_ || $parentNode instanceof Node\Expr\Cast\Unset_) { diff --git a/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php b/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php index 54c9c08d0ee2..7270803d47ed 100644 --- a/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php +++ b/packages/Php70/src/Rector/If_/IfToSpaceshipRector.php @@ -133,10 +133,8 @@ public function refactor(Node $node): ?Node } } else { $nextNode = $node->getAttribute(AttributeKey::NEXT_NODE); - if ($nextNode instanceof Return_) { - if ($nextNode->expr instanceof Ternary) { - $this->processTernary($nextNode->expr); - } + if ($nextNode instanceof Return_ && $nextNode->expr instanceof Ternary) { + $this->processTernary($nextNode->expr); } } diff --git a/packages/Php70/src/Rector/Ternary/TernaryToSpaceshipRector.php b/packages/Php70/src/Rector/Ternary/TernaryToSpaceshipRector.php index 83cb66f804f3..f724fc5137d9 100644 --- a/packages/Php70/src/Rector/Ternary/TernaryToSpaceshipRector.php +++ b/packages/Php70/src/Rector/Ternary/TernaryToSpaceshipRector.php @@ -97,10 +97,11 @@ private function shouldSkip(Ternary $ternary): bool */ private function processSmallerThanTernary(Ternary $node, Ternary $nestedTernary): ?Spaceship { - if ($node->cond instanceof Smaller && $nestedTernary->cond instanceof Greater) { - if ($this->areValues([$node->if, $nestedTernary->if, $nestedTernary->else], [-1, 1, 0])) { - return new Spaceship($node->cond->left, $node->cond->right); - } + if ($node->cond instanceof Smaller && $nestedTernary->cond instanceof Greater && $this->areValues( + [$node->if, $nestedTernary->if, $nestedTernary->else], + [-1, 1, 0] + )) { + return new Spaceship($node->cond->left, $node->cond->right); } return null; @@ -111,10 +112,11 @@ private function processSmallerThanTernary(Ternary $node, Ternary $nestedTernary */ private function processGreaterThanTernary(Ternary $node, Ternary $nestedTernary): ?Spaceship { - if ($node->cond instanceof Greater && $nestedTernary->cond instanceof Smaller) { - if ($this->areValues([$node->if, $nestedTernary->if, $nestedTernary->else], [-1, 1, 0])) { - return new Spaceship($node->cond->right, $node->cond->left); - } + if ($node->cond instanceof Greater && $nestedTernary->cond instanceof Smaller && $this->areValues( + [$node->if, $nestedTernary->if, $nestedTernary->else], + [-1, 1, 0] + )) { + return new Spaceship($node->cond->right, $node->cond->left); } return null; diff --git a/packages/Php71/src/Rector/Assign/AssignArrayToStringRector.php b/packages/Php71/src/Rector/Assign/AssignArrayToStringRector.php index c08e9b2282e4..a296dc952bd6 100644 --- a/packages/Php71/src/Rector/Assign/AssignArrayToStringRector.php +++ b/packages/Php71/src/Rector/Assign/AssignArrayToStringRector.php @@ -77,10 +77,10 @@ public function refactor(Node $node): ?Node $variableNode = $arrayDimFetchNode->var; // set default value to property - if ($variableNode instanceof PropertyFetch || $variableNode instanceof StaticPropertyFetch) { - if ($this->processProperty($variableNode)) { - return $node; - } + if (($variableNode instanceof PropertyFetch || $variableNode instanceof StaticPropertyFetch) && + $this->processProperty($variableNode) + ) { + return $node; } // fallback to variable, property or static property = '' set diff --git a/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php index a2d17179288a..4ba6cf7f3b23 100644 --- a/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php +++ b/packages/Php71/src/Rector/List_/ListToArrayDestructRector.php @@ -77,16 +77,12 @@ public function refactor(Node $node): ?Node $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Assign) { - if ($parentNode->var === $node) { - return new Array_((array) $node->items); - } + if ($parentNode instanceof Assign && $parentNode->var === $node) { + return new Array_((array) $node->items); } - if ($parentNode instanceof Foreach_) { - if ($parentNode->valueVar === $node) { - return new Array_((array) $node->items); - } + if ($parentNode instanceof Foreach_ && $parentNode->valueVar === $node) { + return new Array_((array) $node->items); } return null; diff --git a/packages/Php72/src/Rector/FuncCall/GetClassOnNullRector.php b/packages/Php72/src/Rector/FuncCall/GetClassOnNullRector.php index e763eb02c509..04a50ca585ed 100644 --- a/packages/Php72/src/Rector/FuncCall/GetClassOnNullRector.php +++ b/packages/Php72/src/Rector/FuncCall/GetClassOnNullRector.php @@ -75,10 +75,8 @@ public function refactor(Node $node): ?Node // only relevant inside the class /** @var Scope|null $nodeScope */ $nodeScope = $node->getAttribute(AttributeKey::SCOPE); - if ($nodeScope instanceof Scope) { - if (! $nodeScope->isInClass()) { - return null; - } + if ($nodeScope instanceof Scope && ! $nodeScope->isInClass()) { + return null; } // possibly already changed @@ -129,19 +127,14 @@ private function isIdenticalToNotNull(FuncCall $funcCall, Ternary $ternary): boo return false; } - if ($this->areNodesEqual($ternary->cond->left, $funcCall->args[0]->value)) { - if (! $this->isNull($ternary->cond->right)) { - return true; - } - } - - if ($this->areNodesEqual($ternary->cond->right, $funcCall->args[0]->value)) { - if (! $this->isNull($ternary->cond->left)) { - return true; - } + if ($this->areNodesEqual($ternary->cond->left, $funcCall->args[0]->value) && ! $this->isNull( + $ternary->cond->right + )) { + return true; } - - return false; + return $this->areNodesEqual($ternary->cond->right, $funcCall->args[0]->value) && ! $this->isNull( + $ternary->cond->left + ); } /** @@ -153,18 +146,13 @@ private function isNotIdenticalToNull(FuncCall $funcCall, Ternary $ternary): boo return false; } - if ($this->areNodesEqual($ternary->cond->left, $funcCall->args[0]->value)) { - if ($this->isNull($ternary->cond->right)) { - return true; - } - } - - if ($this->areNodesEqual($ternary->cond->right, $funcCall->args[0]->value)) { - if ($this->isNull($ternary->cond->left)) { - return true; - } + if ($this->areNodesEqual($ternary->cond->left, $funcCall->args[0]->value) && $this->isNull( + $ternary->cond->right + )) { + return true; } - - return false; + return $this->areNodesEqual($ternary->cond->right, $funcCall->args[0]->value) && $this->isNull( + $ternary->cond->left + ); } } diff --git a/packages/Php72/src/Rector/FuncCall/ParseStrWithResultArgumentRector.php b/packages/Php72/src/Rector/FuncCall/ParseStrWithResultArgumentRector.php index ef1a0ad72516..db9b817aa4bb 100644 --- a/packages/Php72/src/Rector/FuncCall/ParseStrWithResultArgumentRector.php +++ b/packages/Php72/src/Rector/FuncCall/ParseStrWithResultArgumentRector.php @@ -74,10 +74,8 @@ public function refactor(Node $node): ?Node } $this->traverseNodesWithCallable($nextExpression, function (Node $node) use ($resultVariable): ?Variable { - if ($node instanceof FuncCall) { - if ($this->isName($node, 'get_defined_vars')) { - return $resultVariable; - } + if ($node instanceof FuncCall && $this->isName($node, 'get_defined_vars')) { + return $resultVariable; } return null; diff --git a/packages/Php73/src/Rector/FuncCall/RemoveMissingCompactVariableRector.php b/packages/Php73/src/Rector/FuncCall/RemoveMissingCompactVariableRector.php index 3b571ab28875..35540a68edf8 100644 --- a/packages/Php73/src/Rector/FuncCall/RemoveMissingCompactVariableRector.php +++ b/packages/Php73/src/Rector/FuncCall/RemoveMissingCompactVariableRector.php @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node unset($node->args[$key]); } - if (! $node->args) { + if ($node->args === []) { return new Array_(); } diff --git a/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index 0c3a611c2232..c5a8f3982305 100644 --- a/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/packages/Php74/src/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -158,10 +158,8 @@ private function resolveValue(Expr $expr): Expr return $expr; } - if ($expr->if instanceof Variable) { - if ($this->isIteratorToArrayFuncCall($expr->else)) { - return $expr->if; - } + if ($expr->if instanceof Variable && $this->isIteratorToArrayFuncCall($expr->else)) { + return $expr->if; } return $expr; diff --git a/packages/Php74/src/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php b/packages/Php74/src/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php index d4c87e2fb604..dbd74a056a2f 100644 --- a/packages/Php74/src/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php +++ b/packages/Php74/src/Rector/MethodCall/ChangeReflectionTypeToStringToGetNameRector.php @@ -98,10 +98,8 @@ public function refactor(Node $node): ?Node return $this->refactorIfHasReturnTypeWasCalled($node->expr); } - if ($node->expr instanceof Variable) { - if ($this->isObjectType($node->expr, 'ReflectionType')) { - return $this->createMethodCall($node->expr, 'getName'); - } + if ($node->expr instanceof Variable && $this->isObjectType($node->expr, 'ReflectionType')) { + return $this->createMethodCall($node->expr, 'getName'); } } diff --git a/packages/PhpSpecToPHPUnit/src/Rector/ClassMethod/PhpSpecMethodToPHPUnitMethodRector.php b/packages/PhpSpecToPHPUnit/src/Rector/ClassMethod/PhpSpecMethodToPHPUnitMethodRector.php index 8e177cebe622..cd666ec336b3 100644 --- a/packages/PhpSpecToPHPUnit/src/Rector/ClassMethod/PhpSpecMethodToPHPUnitMethodRector.php +++ b/packages/PhpSpecToPHPUnit/src/Rector/ClassMethod/PhpSpecMethodToPHPUnitMethodRector.php @@ -80,11 +80,12 @@ private function processTestMethod(ClassMethod $classMethod): void // reorder instantiation + expected exception $previousStmt = null; foreach ((array) $classMethod->stmts as $key => $stmt) { - if ($previousStmt && Strings::contains($this->print($stmt), 'duringInstantiation')) { - if (Strings::contains($this->print($previousStmt), 'beConstructedThrough')) { - $classMethod->stmts[$key - 1] = $stmt; - $classMethod->stmts[$key] = $previousStmt; - } + if ($previousStmt && + Strings::contains($this->print($stmt), 'duringInstantiation') && + Strings::contains($this->print($previousStmt), 'beConstructedThrough') + ) { + $classMethod->stmts[$key - 1] = $stmt; + $classMethod->stmts[$key] = $previousStmt; } $previousStmt = $stmt; diff --git a/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php b/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php index 64c786f3ce02..63375fc514f2 100644 --- a/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php +++ b/packages/Polyfill/src/Rector/If_/UnwrapFutureCompatibleIfFunctionExistsRector.php @@ -82,7 +82,7 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $match = $this->ifManipulator->isIfOrIfElseWithFunctionCondition($node, 'function_exists'); - if ($match === false) { + if (! $match) { return null; } diff --git a/packages/Renaming/src/Rector/Class_/RenameClassRector.php b/packages/Renaming/src/Rector/Class_/RenameClassRector.php index 24cdc319d628..74221a3128bf 100644 --- a/packages/Renaming/src/Rector/Class_/RenameClassRector.php +++ b/packages/Renaming/src/Rector/Class_/RenameClassRector.php @@ -186,10 +186,8 @@ private function refactorName(Name $name): ?Name $parentNode = $name->getAttribute(AttributeKey::PARENT_NODE); // no need to preslash "use \SomeNamespace" of imported namespace - if ($parentNode instanceof UseUse) { - if ($parentNode->type === Use_::TYPE_NORMAL || $parentNode->type === Use_::TYPE_UNKNOWN) { - return new Name($newName); - } + if ($parentNode instanceof UseUse && ($parentNode->type === Use_::TYPE_NORMAL || $parentNode->type === Use_::TYPE_UNKNOWN)) { + return new Name($newName); } return new FullyQualified($newName); diff --git a/packages/SOLID/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php b/packages/SOLID/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php index 535cff8a2e1f..1aaa924c5da3 100644 --- a/packages/SOLID/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php +++ b/packages/SOLID/src/Rector/If_/ChangeNestedIfsToEarlyReturnRector.php @@ -152,18 +152,14 @@ private function addStandaloneIfsWithReturn(If_ $nestedIfWithOnlyReturn, If_ $if $invertedCondition = $this->createInvertedCondition($nestedIfWithOnlyReturn->cond); // special case - if ($invertedCondition instanceof BooleanNot) { - if ($invertedCondition->expr instanceof BooleanAnd) { - $booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->left)); - $booleanNotPartIf->stmts = [clone $return]; - $this->addNodeAfterNode($booleanNotPartIf, $if); - - $booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->right)); - $booleanNotPartIf->stmts = [clone $return]; - $this->addNodeAfterNode($booleanNotPartIf, $if); - - return; - } + if ($invertedCondition instanceof BooleanNot && $invertedCondition->expr instanceof BooleanAnd) { + $booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->left)); + $booleanNotPartIf->stmts = [clone $return]; + $this->addNodeAfterNode($booleanNotPartIf, $if); + $booleanNotPartIf = new If_(new BooleanNot($invertedCondition->expr->right)); + $booleanNotPartIf->stmts = [clone $return]; + $this->addNodeAfterNode($booleanNotPartIf, $if); + return; } $nestedIfWithOnlyReturn->cond = $invertedCondition; diff --git a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php index a314038c6276..051a8d6ffe6b 100644 --- a/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php +++ b/packages/Sensio/src/Rector/FrameworkExtraBundle/TemplateAnnotationRector.php @@ -153,10 +153,8 @@ private function updateReturnType(ClassMethod $classMethod): void if ($classMethod->returnType !== null) { $returnTypeName = $this->getName($classMethod->returnType); - if ($returnTypeName !== null) { - if (is_a($returnTypeName, Response::class, true)) { - return; - } + if ($returnTypeName !== null && is_a($returnTypeName, Response::class, true)) { + return; } } @@ -237,15 +235,13 @@ private function refactorClassMethod( /** @var Return_|null $returnNode */ $returnNode = $this->betterNodeFinder->findLastInstanceOf((array) $classMethod->stmts, Return_::class); - if ($returnNode !== null) { - if ($returnNode->expr instanceof MethodCall) { - // go inside called method - $innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr); - if ($innerClassMethod !== null) { - $this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode); + if ($returnNode !== null && $returnNode->expr instanceof MethodCall) { + // go inside called method + $innerClassMethod = $this->parsedNodesByType->findClassMethodByMethodCall($returnNode->expr); + if ($innerClassMethod !== null) { + $this->refactorClassMethod($innerClassMethod, $sensioTemplateTagValueNode); - return; - } + return; } } diff --git a/packages/Symfony/src/Rector/Console/ConsoleExceptionToErrorEventConstantRector.php b/packages/Symfony/src/Rector/Console/ConsoleExceptionToErrorEventConstantRector.php index 516224e1be66..4529c8fcdc75 100644 --- a/packages/Symfony/src/Rector/Console/ConsoleExceptionToErrorEventConstantRector.php +++ b/packages/Symfony/src/Rector/Console/ConsoleExceptionToErrorEventConstantRector.php @@ -48,10 +48,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node instanceof ClassConstFetch) { - if ($this->isObjectType($node, self::CONSOLE_EVENTS_CLASS) && $this->isName($node->name, 'EXCEPTION')) { - return $this->createClassConstant(self::CONSOLE_EVENTS_CLASS, 'ERROR'); - } + if ($node instanceof ClassConstFetch && ( + $this->isObjectType($node, self::CONSOLE_EVENTS_CLASS) && + $this->isName($node->name, 'EXCEPTION')) + ) { + return $this->createClassConstant(self::CONSOLE_EVENTS_CLASS, 'ERROR'); } if ($node instanceof String_ && $node->value === 'console.exception') { diff --git a/packages/Symfony/src/Rector/Console/ConsoleExecuteReturnIntRector.php b/packages/Symfony/src/Rector/Console/ConsoleExecuteReturnIntRector.php index bc21e6b51b69..2b113d433c93 100644 --- a/packages/Symfony/src/Rector/Console/ConsoleExecuteReturnIntRector.php +++ b/packages/Symfony/src/Rector/Console/ConsoleExecuteReturnIntRector.php @@ -89,11 +89,9 @@ public function refactor(Node $node): ?Node private function refactorReturnTypeDeclaration(ClassMethod $classMethod): void { - if ($classMethod->returnType !== null) { - // already set - if ($this->isName($classMethod->returnType, 'int')) { - return; - } + // already set + if ($classMethod->returnType !== null && $this->isName($classMethod->returnType, 'int')) { + return; } $classMethod->returnType = new Identifier('int'); diff --git a/packages/Symfony/src/Rector/MethodCall/FormTypeInstanceToClassConstRector.php b/packages/Symfony/src/Rector/MethodCall/FormTypeInstanceToClassConstRector.php index 53ad77453dbf..d31520d56c01 100644 --- a/packages/Symfony/src/Rector/MethodCall/FormTypeInstanceToClassConstRector.php +++ b/packages/Symfony/src/Rector/MethodCall/FormTypeInstanceToClassConstRector.php @@ -166,10 +166,8 @@ private function moveArgumentsToOptions( $namesToArgs = $this->resolveNamesToArgs($className, $argNodes); // set default data in between - if ($position + 1 !== $optionsPosition) { - if (! isset($methodCall->args[$position + 1])) { - $methodCall->args[$position + 1] = new Arg($this->createNull()); - } + if ($position + 1 !== $optionsPosition && ! isset($methodCall->args[$position + 1])) { + $methodCall->args[$position + 1] = new Arg($this->createNull()); } // @todo extend current options - array analyzer diff --git a/packages/Symfony/src/Rector/Yaml/ParseFileRector.php b/packages/Symfony/src/Rector/Yaml/ParseFileRector.php index 49811905bc30..4a87248051db 100644 --- a/packages/Symfony/src/Rector/Yaml/ParseFileRector.php +++ b/packages/Symfony/src/Rector/Yaml/ParseFileRector.php @@ -83,13 +83,6 @@ private function isArgumentYamlFile(StaticCall $staticCall): bool } $nodeType = $nodeScope->getType($possibleFileNode); - - if ($nodeType instanceof ConstantStringType) { - if (Strings::match($nodeType->getValue(), '#\.(yml|yaml)$#')) { - return true; - } - } - - return false; + return $nodeType instanceof ConstantStringType && Strings::match($nodeType->getValue(), '#\.(yml|yaml)$#'); } } diff --git a/packages/SymfonyPHPUnit/src/SelfContainerMethodCallCollector.php b/packages/SymfonyPHPUnit/src/SelfContainerMethodCallCollector.php index cc3cea00c848..8d60ef276f24 100644 --- a/packages/SymfonyPHPUnit/src/SelfContainerMethodCallCollector.php +++ b/packages/SymfonyPHPUnit/src/SelfContainerMethodCallCollector.php @@ -54,10 +54,8 @@ public function collectContainerGetServiceTypes(Class_ $class, bool $skipSetUpMe return null; } - if ($skipSetUpMethod) { - if ($this->kernelTestCaseNodeAnalyzer->isSetUpOrEmptyMethod($node)) { - return null; - } + if ($skipSetUpMethod && $this->kernelTestCaseNodeAnalyzer->isSetUpOrEmptyMethod($node)) { + return null; } /** @var MethodCall $node */ diff --git a/packages/TypeDeclaration/src/Rector/ClassMethod/AddArrayReturnDocTypeRector.php b/packages/TypeDeclaration/src/Rector/ClassMethod/AddArrayReturnDocTypeRector.php index 68bdec9c0de2..d76aeb1dd7e6 100644 --- a/packages/TypeDeclaration/src/Rector/ClassMethod/AddArrayReturnDocTypeRector.php +++ b/packages/TypeDeclaration/src/Rector/ClassMethod/AddArrayReturnDocTypeRector.php @@ -141,25 +141,14 @@ private function shouldSkip(ClassMethod $classMethod): bool private function shouldSkipType(Type $newType, ClassMethod $classMethod): bool { - if ($newType instanceof ArrayType) { - if ($this->shouldSkipArrayType($newType, $classMethod)) { - return true; - } - } - - if ($newType instanceof UnionType) { - if ($this->shouldSkipUnionType($newType)) { - return true; - } + if ($newType instanceof ArrayType && $this->shouldSkipArrayType($newType, $classMethod)) { + return true; } - if ($newType instanceof ConstantArrayType) { - if (count($newType->getValueTypes()) > self::MAX_NUMBER_OF_TYPES) { - return true; - } + if ($newType instanceof UnionType && $this->shouldSkipUnionType($newType)) { + return true; } - - return false; + return $newType instanceof ConstantArrayType && count($newType->getValueTypes()) > self::MAX_NUMBER_OF_TYPES; } private function shouldSkipArrayType(ArrayType $arrayType, ClassMethod $classMethod): bool @@ -208,10 +197,6 @@ private function isMixedOfSpecificOverride(ArrayType $arrayType, ClassMethod $cl private function shouldSkipUnionType(UnionType $unionType): bool { - if (count($unionType->getTypes()) > self::MAX_NUMBER_OF_TYPES) { - return true; - } - - return false; + return count($unionType->getTypes()) > self::MAX_NUMBER_OF_TYPES; } } diff --git a/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php b/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php index 44d5faa96293..2f7f07c13c66 100644 --- a/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php +++ b/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php @@ -124,7 +124,7 @@ private function shouldSkip(ClassMethod $classMethod): bool // skip class without parents/interfaces if ($class instanceof Class_) { - if ($class->implements) { + if ($class->implements !== []) { return false; } diff --git a/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php b/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php index 19a260c46388..e10526422a12 100644 --- a/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php +++ b/packages/TypeDeclaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php @@ -143,10 +143,8 @@ public function refactor(Node $node): ?Node $currentType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->returnType); - if ($node instanceof ClassMethod) { - if ($this->vendorLockResolver->isReturnChangeVendorLockedIn($node)) { - return null; - } + if ($node instanceof ClassMethod && $this->vendorLockResolver->isReturnChangeVendorLockedIn($node)) { + return null; } if ($this->isCurrentObjectTypeSubType($currentType, $inferedType)) { @@ -183,10 +181,8 @@ private function shouldSkip(Node $node): bool return true; } - if (! $this->overrideExistingReturnTypes) { - if ($node->returnType !== null) { - return true; - } + if (! $this->overrideExistingReturnTypes && $node->returnType !== null) { + return true; } if (! $node instanceof ClassMethod) { @@ -307,10 +303,8 @@ private function isStaticTypeIterable(Type $type): bool return true; } - if ($type instanceof ObjectType) { - if ($type->getClassName() === Iterator::class) { - return true; - } + if ($type instanceof ObjectType && $type->getClassName() === Iterator::class) { + return true; } if ($type instanceof UnionType || $type instanceof IntersectionType) { diff --git a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php index b5ff421f6ec8..7b5626307f28 100644 --- a/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/PropertyTypeInferer/ConstructorPropertyTypeInferer.php @@ -6,7 +6,6 @@ use Nette\Utils\Strings; use PhpParser\Node; -use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; @@ -91,53 +90,6 @@ public function getPriority(): int return 800; } - /** - * In case the property name is different to param name: - * - * E.g.: - * (SomeType $anotherValue) - * $this->value = $anotherValue; - * ↓ - * $anotherValue param - */ - private function resolveParamForPropertyFetch(ClassMethod $classMethod, string $propertyName): ?Param - { - $assignedParamName = null; - - $this->callableNodeTraverser->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use ( - $propertyName, - &$assignedParamName - ): ?int { - if (! $node instanceof Assign) { - return null; - } - - if (! $this->nameResolver->isName($node->var, $propertyName)) { - return null; - } - - $assignedParamName = $this->nameResolver->getName($node->expr); - - return NodeTraverser::STOP_TRAVERSAL; - }); - - /** @var string|null $assignedParamName */ - if ($assignedParamName === null) { - return null; - } - - /** @var Param $param */ - foreach ((array) $classMethod->params as $param) { - if (! $this->nameResolver->isName($param, $assignedParamName)) { - continue; - } - - return $param; - } - - return null; - } - private function resolveParamTypeToPHPStanType(Param $param): Type { if ($param->type === null) { diff --git a/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInferer.php b/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInferer.php index 8800a54ed0bc..1a19cc4292fb 100644 --- a/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInferer.php +++ b/packages/TypeDeclaration/src/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInferer.php @@ -30,10 +30,8 @@ public function inferFunctionLike(FunctionLike $functionLike): Type { /** @var Class_|Trait_|Interface_|null $classLike */ $classLike = $functionLike->getAttribute(AttributeKey::CLASS_NODE); - if ($functionLike instanceof ClassMethod) { - if ($classLike instanceof Interface_) { - return new MixedType(); - } + if ($functionLike instanceof ClassMethod && $classLike instanceof Interface_) { + return new MixedType(); } $localReturnNodes = $this->collectReturns($functionLike); diff --git a/packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php b/packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php index 85fe216ed888..6a51997e1df3 100644 --- a/packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php +++ b/packages/TypeDeclaration/src/VendorLock/VendorLockResolver.php @@ -91,12 +91,10 @@ public function isParameterChangeVendorLockedIn(ClassMethod $classMethod, int $p $interfaceNames = $this->classManipulator->getClassLikeNodeParentInterfaceNames($classNode); foreach ($interfaceNames as $interfaceName) { $interface = $this->parsedNodesByType->findInterface($interfaceName); - if ($interface !== null) { - // parent class method in local scope → it's ok - // @todo validate type is conflicting - if ($interface->getMethod($methodName) !== null) { - return false; - } + // parent class method in local scope → it's ok + // @todo validate type is conflicting + if ($interface !== null && $interface->getMethod($methodName) !== null) { + return false; } if (method_exists($interfaceName, $methodName)) { @@ -156,12 +154,10 @@ public function isReturnChangeVendorLockedIn(ClassMethod $classMethod): bool $interfaceNames = $this->classManipulator->getClassLikeNodeParentInterfaceNames($classNode); foreach ($interfaceNames as $interfaceName) { $interface = $this->parsedNodesByType->findInterface($interfaceName); - if ($interface !== null) { - // parent class method in local scope → it's ok - // @todo validate type is conflicting - if ($interface->getMethod($methodName) !== null) { - return false; - } + // parent class method in local scope → it's ok + // @todo validate type is conflicting + if ($interface !== null && $interface->getMethod($methodName) !== null) { + return false; } if (method_exists($interfaceName, $methodName)) { @@ -234,10 +230,8 @@ private function getProperty(ClassLike $classLike, string $name) private function hasParentClassOrImplementsInterface(Node $classNode): bool { - if ($classNode instanceof Class_ || $classNode instanceof Interface_) { - if ($classNode->extends) { - return true; - } + if (($classNode instanceof Class_ || $classNode instanceof Interface_) && $classNode->extends) { + return true; } if ($classNode instanceof Class_) { diff --git a/src/Console/Command/ScreenFileCommand.php b/src/Console/Command/ScreenFileCommand.php index a8578430f61d..5776818061eb 100644 --- a/src/Console/Command/ScreenFileCommand.php +++ b/src/Console/Command/ScreenFileCommand.php @@ -180,16 +180,12 @@ private function decorateNodeData(Node $node, array $data = []): array $data = $this->decorateAssign($node, $data); } - if ($node instanceof Namespace_) { - if ($node->name !== null) { - $data['name'] = $this->nameResolver->getName($node->name); - } + if ($node instanceof Namespace_ && $node->name !== null) { + $data['name'] = $this->nameResolver->getName($node->name); } - if ($node instanceof FuncCall) { - if ($node->name !== null) { - $data['name'] = $this->nameResolver->getName($node->name); - } + if ($node instanceof FuncCall && $node->name !== null) { + $data['name'] = $this->nameResolver->getName($node->name); } if ($node instanceof Variable) { diff --git a/src/PHPStan/Reflection/CallReflectionResolver.php b/src/PHPStan/Reflection/CallReflectionResolver.php index 0437a7cc1272..f52d4018f51a 100644 --- a/src/PHPStan/Reflection/CallReflectionResolver.php +++ b/src/PHPStan/Reflection/CallReflectionResolver.php @@ -296,11 +296,6 @@ private function areKeyTypesValid(ConstantArrayType $constantArrayType): bool if ($keyTypes[0]->isSuperTypeOf(new ConstantIntegerType(0))->no()) { return false; } - - if ($keyTypes[1]->isSuperTypeOf(new ConstantIntegerType(1))->no()) { - return false; - } - - return true; + return ! $keyTypes[1]->isSuperTypeOf(new ConstantIntegerType(1))->no(); } } diff --git a/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php b/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php index 508e196e4acd..54deed4a2a99 100644 --- a/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php +++ b/src/PhpParser/Node/Manipulator/ChildAndParentClassManipulator.php @@ -58,12 +58,10 @@ public function completeParentConstructor(Class_ $classNode, ClassMethod $classM } // complete parent call for __construct() - if ($parentClassName !== '') { - if (method_exists($parentClassName, '__construct')) { - $parentConstructCallNode = $this->nodeFactory->createParentConstructWithParams([]); - $classMethod->stmts[] = new Expression($parentConstructCallNode); - return; - } + if ($parentClassName !== '' && method_exists($parentClassName, '__construct')) { + $parentConstructCallNode = $this->nodeFactory->createParentConstructWithParams([]); + $classMethod->stmts[] = new Expression($parentConstructCallNode); + return; } } diff --git a/src/PhpParser/Node/Manipulator/ClassManipulator.php b/src/PhpParser/Node/Manipulator/ClassManipulator.php index d59b90093dda..99182e312f92 100644 --- a/src/PhpParser/Node/Manipulator/ClassManipulator.php +++ b/src/PhpParser/Node/Manipulator/ClassManipulator.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\PropertyFetch; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Identifier; use PhpParser\Node\Name; @@ -474,7 +475,7 @@ public function renamePropertyFetches(Class_ $class, array $oldToNewPropertyName $this->callableNodeTraverser->traverseNodesWithCallable($class, function (Node $node) use ( $oldToNewPropertyNames ) { - if (! $node instanceof Node\Expr\PropertyFetch) { + if (! $node instanceof PropertyFetch) { return null; } diff --git a/src/PhpParser/Node/Manipulator/IfManipulator.php b/src/PhpParser/Node/Manipulator/IfManipulator.php index a7e9b78e6396..3738f88cbf2e 100644 --- a/src/PhpParser/Node/Manipulator/IfManipulator.php +++ b/src/PhpParser/Node/Manipulator/IfManipulator.php @@ -195,12 +195,7 @@ public function isIfAndElseWithSameVariableAssignAsLastStmts(If_ $if, Expr $desi if (! $this->betterStandardPrinter->areNodesEqual($lastIfStmt->var, $lastElseStmt->var)) { return false; } - - if (! $this->betterStandardPrinter->areNodesEqual($desiredExpr, $lastElseStmt->var)) { - return false; - } - - return true; + return $this->betterStandardPrinter->areNodesEqual($desiredExpr, $lastElseStmt->var); } /** @@ -218,20 +213,18 @@ public function isIfOrIfElseWithFunctionCondition(If_ $if, string $functionName) if (! $if->cond instanceof FuncCall) { return false; } - - if (! $this->nameResolver->isName($if->cond, $functionName)) { - return false; - } - - return true; + return $this->nameResolver->isName($if->cond, $functionName); } private function matchComparedAndReturnedNode(NotIdentical $notIdentical, Return_ $returnNode): ?Expr { - if ($this->betterStandardPrinter->areNodesEqual($notIdentical->left, $returnNode->expr)) { - if ($this->constFetchManipulator->isNull($notIdentical->right)) { - return $notIdentical->left; - } + if ($this->betterStandardPrinter->areNodesEqual( + $notIdentical->left, + $returnNode->expr + ) && $this->constFetchManipulator->isNull( + $notIdentical->right + )) { + return $notIdentical->left; } if (! $this->betterStandardPrinter->areNodesEqual($notIdentical->right, $returnNode->expr)) { diff --git a/src/PhpParser/Node/Resolver/NameResolver.php b/src/PhpParser/Node/Resolver/NameResolver.php index 81e1d71f89ee..9a4fa67cd578 100644 --- a/src/PhpParser/Node/Resolver/NameResolver.php +++ b/src/PhpParser/Node/Resolver/NameResolver.php @@ -163,17 +163,13 @@ public function getName(Node $node): ?string } // skip $some->$dynamicMethodName() - if ($parentNode instanceof MethodCall) { - if ($node === $parentNode->name) { - return null; - } + if ($parentNode instanceof MethodCall && $node === $parentNode->name) { + return null; } // skip $some->$dynamicPropertyName - if ($parentNode instanceof PropertyFetch) { - if ($node === $parentNode->name) { - return null; - } + if ($parentNode instanceof PropertyFetch && $node === $parentNode->name) { + return null; } } diff --git a/src/PhpParser/Printer/BetterStandardPrinter.php b/src/PhpParser/Printer/BetterStandardPrinter.php index da47dca33c91..8bc74bc2e047 100644 --- a/src/PhpParser/Printer/BetterStandardPrinter.php +++ b/src/PhpParser/Printer/BetterStandardPrinter.php @@ -270,12 +270,10 @@ protected function pStmt_Class(Class_ $class): string $shouldReindex = false; foreach ($class->stmts as $key => $stmt) { - if ($stmt instanceof TraitUse) { - // remove empty ones - if (count($stmt->traits) === 0) { - unset($class->stmts[$key]); - $shouldReindex = true; - } + // remove empty ones + if ($stmt instanceof TraitUse && count($stmt->traits) === 0) { + unset($class->stmts[$key]); + $shouldReindex = true; } } diff --git a/src/Rector/MagicDisclosure/GetAndSetToMethodCallRector.php b/src/Rector/MagicDisclosure/GetAndSetToMethodCallRector.php index 12855e4922a6..5e9da410f4f0 100644 --- a/src/Rector/MagicDisclosure/GetAndSetToMethodCallRector.php +++ b/src/Rector/MagicDisclosure/GetAndSetToMethodCallRector.php @@ -142,10 +142,8 @@ private function processPropertyFetch(PropertyFetch $propertyFetch): ?MethodCall // setter, skip $parentNode = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE); - if ($parentNode instanceof Assign) { - if ($parentNode->var === $propertyFetch) { - continue; - } + if ($parentNode instanceof Assign && $parentNode->var === $propertyFetch) { + continue; } return $this->createMethodCallNodeFromPropertyFetchNode($propertyFetch, $transformation['get']);