From 9f70dfd85261ced3af72dc0e5e75380559d7cfd4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 20 Feb 2022 22:09:18 +0000 Subject: [PATCH] [PHPStanRules] Remove ForbiddenNestedForeachWithEmptyStatementRule, as not very helpful in pratise (#3945) --- .../phpstan-rules/config/static-rules.neon | 4 - packages/phpstan-rules/docs/rules_overview.md | 32 ------ ...denNestedForeachWithEmptyStatementRule.php | 104 ------------------ .../NestedForeachWithEmptyStatement.php | 12 -- .../Fixture/SkipArray.php | 17 --- ...mptyStatementWithDifferentVariableLoop.php | 18 --- ...SkipNestedForeachWithNonEmptyStatement.php | 18 --- .../Fixture/SkipNotNestedForeach.php | 15 --- ...estedForeachWithEmptyStatementRuleTest.php | 46 -------- .../config/configured_rule.neon | 7 -- phpstan.neon | 11 +- 11 files changed, 2 insertions(+), 282 deletions(-) delete mode 100644 packages/phpstan-rules/src/Rules/ForbiddenNestedForeachWithEmptyStatementRule.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/NestedForeachWithEmptyStatement.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/SkipArray.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/SkipNestedForeachWithEmptyStatementWithDifferentVariableLoop.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/SkipNestedForeachWithNonEmptyStatement.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/SkipNotNestedForeach.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/ForbiddenNestedForeachWithEmptyStatementRuleTest.php delete mode 100644 packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/config/configured_rule.neon diff --git a/packages/phpstan-rules/config/static-rules.neon b/packages/phpstan-rules/config/static-rules.neon index 9277a95cd0..4fc9f540da 100644 --- a/packages/phpstan-rules/config/static-rules.neon +++ b/packages/phpstan-rules/config/static-rules.neon @@ -132,10 +132,6 @@ services: class: Symplify\PHPStanRules\Rules\RequireThisCallOnLocalMethodRule tags: [phpstan.rules.rule] - - - class: Symplify\PHPStanRules\Rules\ForbiddenNestedForeachWithEmptyStatementRule - tags: [phpstan.rules.rule] - - class: Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule tags: [phpstan.rules.rule] diff --git a/packages/phpstan-rules/docs/rules_overview.md b/packages/phpstan-rules/docs/rules_overview.md index b91462dc90..e717423260 100644 --- a/packages/phpstan-rules/docs/rules_overview.md +++ b/packages/phpstan-rules/docs/rules_overview.md @@ -1304,38 +1304,6 @@ final class SomeClass extends TestCase
-## ForbiddenNestedForeachWithEmptyStatementRule - -Nested foreach with empty statement is not allowed - -- class: [`Symplify\PHPStanRules\Rules\ForbiddenNestedForeachWithEmptyStatementRule`](../src/Rules/ForbiddenNestedForeachWithEmptyStatementRule.php) - -```php -$collectedFileErrors = []; - -foreach ($errors as $fileErrors) { - foreach ($fileErrors as $fileError) { - $collectedFileErrors[] = $fileError; - } -} -``` - -:x: - -
- -```php -$collectedFileErrors = []; - -foreach ($fileErrors as $fileError) { - $collectedFileErrors[] = $fileError; -} -``` - -:+1: - -
- ## ForbiddenNodeRule "%s" is forbidden to use diff --git a/packages/phpstan-rules/src/Rules/ForbiddenNestedForeachWithEmptyStatementRule.php b/packages/phpstan-rules/src/Rules/ForbiddenNestedForeachWithEmptyStatementRule.php deleted file mode 100644 index abb7c14db2..0000000000 --- a/packages/phpstan-rules/src/Rules/ForbiddenNestedForeachWithEmptyStatementRule.php +++ /dev/null @@ -1,104 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [Foreach_::class]; - } - - /** - * @param Foreach_ $node - * @return string[] - */ - public function process(Node $node, Scope $scope): array - { - if (! $this->isNextForeachWithEmptyStatement($node)) { - return []; - } - - return [self::ERROR_MESSAGE]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new CodeSample( - <<<'CODE_SAMPLE' -$collectedFileErrors = []; - -foreach ($errors as $fileErrors) { - foreach ($fileErrors as $fileError) { - $collectedFileErrors[] = $fileError; - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -$collectedFileErrors = []; - -foreach ($fileErrors as $fileError) { - $collectedFileErrors[] = $fileError; -} -CODE_SAMPLE - ), - ]); - } - - private function isNextForeachWithEmptyStatement(Foreach_ $foreach): bool - { - $stmts = $this->nodeFinder->findInstanceOf($foreach->stmts, Stmt::class); - if (! isset($stmts[0])) { - return false; - } - - $nestedForeach = $stmts[0]; - if (! $nestedForeach instanceof Foreach_) { - return false; - } - - $foreachVariable = $foreach->expr->getAttribute(AttributeKey::NEXT); - if (! $foreachVariable instanceof Variable) { - return false; - } - - $nextForeachVariable = $nestedForeach->expr; - if (! $nextForeachVariable instanceof Variable) { - return false; - } - - return $this->simpleNameResolver->areNamesEqual($foreachVariable, $nextForeachVariable); - } -} diff --git a/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/NestedForeachWithEmptyStatement.php b/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/NestedForeachWithEmptyStatement.php deleted file mode 100644 index 2dd8381ba9..0000000000 --- a/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/Fixture/NestedForeachWithEmptyStatement.php +++ /dev/null @@ -1,12 +0,0 @@ - - */ -final class ForbiddenNestedForeachWithEmptyStatementRuleTest extends AbstractServiceAwareRuleTestCase -{ - /** - * @dataProvider provideData() - * @param array $expectedErrorMessagesWithLines - */ - public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void - { - $this->analyse([$filePath], $expectedErrorMessagesWithLines); - } - - public function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/SkipArray.php', []]; - yield [__DIR__ . '/Fixture/SkipNotNestedForeach.php', []]; - yield [__DIR__ . '/Fixture/SkipNestedForeachWithNonEmptyStatement.php', []]; - yield [__DIR__ . '/Fixture/SkipNestedForeachWithEmptyStatementWithDifferentVariableLoop.php', []]; - - yield [ - __DIR__ . '/Fixture/NestedForeachWithEmptyStatement.php', - [[ForbiddenNestedForeachWithEmptyStatementRule::ERROR_MESSAGE, 7]], - ]; - } - - protected function getRule(): Rule - { - return $this->getRuleFromConfig( - ForbiddenNestedForeachWithEmptyStatementRule::class, - __DIR__ . '/config/configured_rule.neon' - ); - } -} diff --git a/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/config/configured_rule.neon b/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/config/configured_rule.neon deleted file mode 100644 index bf37e8f0df..0000000000 --- a/packages/phpstan-rules/tests/Rules/ForbiddenNestedForeachWithEmptyStatementRule/config/configured_rule.neon +++ /dev/null @@ -1,7 +0,0 @@ -includes: - - ../../../config/included_services.neon - -services: - - - class: Symplify\PHPStanRules\Rules\ForbiddenNestedForeachWithEmptyStatementRule - tags: [phpstan.rules.rule] diff --git a/phpstan.neon b/phpstan.neon index 4bd2c155dd..76ac5093cb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -354,6 +354,8 @@ parameters: # known types - '#Method Symplify\\Astral\\NodeFinder\\SimpleNodeFinder\:\:findByType\(\) should return array but returns array#' - '#Method Symplify\\Astral\\NodeFinder\\SimpleNodeFinder\:\:findFirstParentByType\(\) should return T of PhpParser\\Node\|null but returns class\-string\|T of PhpParser\\Node#' + # node finder generics meshup + - '#Method Symplify\\Astral\\NodeFinder\\SimpleNodeFinder\:\:findFirstByType\(\) should return T of PhpParser\\Node\|null but returns PhpParser\\Node\|null#' - message: '#Use void instead of modify and return self object#' @@ -391,11 +393,6 @@ parameters: message: '#Removing parent param type is forbidden#' path: *FileLoader.php - # this class is used by scoper, so better use less external deps - - - message: '#Nested foreach with empty statement is not allowed#' - path: packages/php-config-printer/src/PhpParser/NodeFactory/ConfiguratorClosureNodeFactory.php - - '#Method "processTokensByFixer\(\)" returns bool type, so the name should start with is/has/was#' - @@ -523,7 +520,6 @@ parameters: # parallel - '#popen(.*?)" is forbidden to use#' - - '#PHPDoc tag @param for parameter \$postFileCallback with type \(Closure\)\|null is not subtype of native type Closure#' # json return - @@ -567,9 +563,6 @@ parameters: - packages/symfony-static-dumper/src/Routing/RoutesProvider.php - packages/autowire-array-parameter/src/DependencyInjection/DefinitionFinder.php - # node finder generics meshup - - '#Method Symplify\\Astral\\NodeFinder\\SimpleNodeFinder\:\:findFirstByType\(\) should return T of PhpParser\\Node\|null but returns PhpParser\\Node\|null#' - # false positive, due to scoped autoload of rector.php - '#Call to an undefined method Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\ServiceConfigurator\:\:configure\(\)#'