From 86bd631388ba84c624524ee87eee8026f8981c6d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 9 Feb 2021 12:04:13 +0100 Subject: [PATCH] [PHPStanRules] Remove CheckTraitMethodOnlyDelegateOtherClassRule, not much value (#2927) --- .../src/CaseConverter/ParameterConverter.php | 6 +- .../phpstan-rules/config/static-rules.neon | 4 - ...kTraitMethodOnlyDelegateOtherClassRule.php | 133 ------------------ ...itMethodOnlyDelegateOtherClassRuleTest.php | 41 ------ .../Fixture/CallThisType.php | 15 -- .../Fixture/HasInstanceofCheck.php | 17 --- .../Fixture/SkipDelegate.php | 22 --- 7 files changed, 2 insertions(+), 236 deletions(-) delete mode 100644 packages/phpstan-rules/src/Rules/CheckTraitMethodOnlyDelegateOtherClassRule.php delete mode 100644 packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/CheckTraitMethodOnlyDelegateOtherClassRuleTest.php delete mode 100644 packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/CallThisType.php delete mode 100644 packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/HasInstanceofCheck.php delete mode 100644 packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/SkipDelegate.php diff --git a/packages/phpstan-php-config/src/CaseConverter/ParameterConverter.php b/packages/phpstan-php-config/src/CaseConverter/ParameterConverter.php index afea0944c2..5656027f0d 100644 --- a/packages/phpstan-php-config/src/CaseConverter/ParameterConverter.php +++ b/packages/phpstan-php-config/src/CaseConverter/ParameterConverter.php @@ -39,10 +39,8 @@ public function convertParameterBag(ParameterBagInterface $parameterBag): array ); } - if ($parameterBag->has(OPTION::IGNORE_ERRORS)) { - $neonParameters[OPTION::IGNORE_ERRORS] = (array) $parameterBag->get( - OPTION::IGNORE_ERRORS - ); + if ($parameterBag->has(Option::IGNORE_ERRORS)) { + $neonParameters[Option::IGNORE_ERRORS] = (array) $parameterBag->get(Option::IGNORE_ERRORS); } return $neonParameters; diff --git a/packages/phpstan-rules/config/static-rules.neon b/packages/phpstan-rules/config/static-rules.neon index c8cd7eb627..edd9e8a175 100644 --- a/packages/phpstan-rules/config/static-rules.neon +++ b/packages/phpstan-rules/config/static-rules.neon @@ -146,10 +146,6 @@ services: class: Symplify\PHPStanRules\Rules\CheckParentChildMethodParameterTypeCompatibleRule tags: [phpstan.rules.rule] - - - class: Symplify\PHPStanRules\Rules\CheckTraitMethodOnlyDelegateOtherClassRule - tags: [phpstan.rules.rule] - - class: Symplify\PHPStanRules\Rules\ForbiddenMethodCallOnNewRule tags: [phpstan.rules.rule] diff --git a/packages/phpstan-rules/src/Rules/CheckTraitMethodOnlyDelegateOtherClassRule.php b/packages/phpstan-rules/src/Rules/CheckTraitMethodOnlyDelegateOtherClassRule.php deleted file mode 100644 index 2b3a82775d..0000000000 --- a/packages/phpstan-rules/src/Rules/CheckTraitMethodOnlyDelegateOtherClassRule.php +++ /dev/null @@ -1,133 +0,0 @@ -nodeFinder = $nodeFinder; - $this->simpleNameResolver = $simpleNameResolver; - } - - /** - * @return string[] - */ - public function getNodeTypes(): array - { - return [Trait_::class]; - } - - /** - * @param Trait_ $node - * @return string[] - */ - public function process(Node $node, Scope $scope): array - { - /** @var ClassMethod[] $classMethods */ - $classMethods = $this->nodeFinder->findInstanceOf($node, ClassMethod::class); - - foreach ($classMethods as $classMethod) { - $classMethodName = $this->simpleNameResolver->getName($classMethod); - - if ($this->hasMethodCallFromThis($classMethod)) { - return [sprintf(self::ERROR_MESSAGE, $classMethodName)]; - } - - if ($this->hasInstanceOfExpression($classMethod)) { - return [sprintf(self::ERROR_MESSAGE, $classMethodName)]; - } - } - - return []; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new CodeSample( - <<<'CODE_SAMPLE' -trait SomeTrait -{ - public function someComplexLogic() - { - if (...) { - } else { - // ... - } - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -trait SomeTrait -{ - /** - * @required - */ - public $someDependency; - - public function someDelegateCall() - { - $this->someDependency->singleDelegateCall(); - } -} -CODE_SAMPLE - ), - ]); - } - - private function hasMethodCallFromThis(ClassMethod $classMethod): bool - { - /** @var MethodCall[] $methodCalls */ - $methodCalls = $this->nodeFinder->findInstanceOf($classMethod, MethodCall::class); - - foreach ($methodCalls as $methodCall) { - $methodCallVar = $methodCall->var; - if (! $methodCallVar instanceof PropertyFetch) { - return true; - } - } - - return false; - } - - private function hasInstanceOfExpression(ClassMethod $classMethod): bool - { - return (bool) $this->nodeFinder->findFirst($classMethod, function (Node $node): bool { - return $node instanceof Instanceof_; - }); - } -} diff --git a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/CheckTraitMethodOnlyDelegateOtherClassRuleTest.php b/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/CheckTraitMethodOnlyDelegateOtherClassRuleTest.php deleted file mode 100644 index 22c6e8b46f..0000000000 --- a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/CheckTraitMethodOnlyDelegateOtherClassRuleTest.php +++ /dev/null @@ -1,41 +0,0 @@ -analyse([$filePath], $expectedErrorMessagesWithLines); - } - - public function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/SkipDelegate.php', []]; - - yield [ - __DIR__ . '/Fixture/CallThisType.php', - [[sprintf(CheckTraitMethodOnlyDelegateOtherClassRule::ERROR_MESSAGE, 'run'), 9]], ]; - yield [ - __DIR__ . '/Fixture/HasInstanceofCheck.php', - [[sprintf(CheckTraitMethodOnlyDelegateOtherClassRule::ERROR_MESSAGE, 'run'), 9]], ]; - } - - protected function getRule(): Rule - { - return $this->getRuleFromConfig( - CheckTraitMethodOnlyDelegateOtherClassRule::class, - __DIR__ . '/../../../config/symplify-rules.neon' - ); - } -} diff --git a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/CallThisType.php b/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/CallThisType.php deleted file mode 100644 index b441cb0ca3..0000000000 --- a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/CallThisType.php +++ /dev/null @@ -1,15 +0,0 @@ -isName('test'); - } -} diff --git a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/HasInstanceofCheck.php b/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/HasInstanceofCheck.php deleted file mode 100644 index 103a50f2ec..0000000000 --- a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/HasInstanceofCheck.php +++ /dev/null @@ -1,17 +0,0 @@ -d instanceof DateTime) { - $this->d->format('Y-m-d'); - } - } -} diff --git a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/SkipDelegate.php b/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/SkipDelegate.php deleted file mode 100644 index 31f42e0616..0000000000 --- a/packages/phpstan-rules/tests/Rules/CheckTraitMethodOnlyDelegateOtherClassRule/Fixture/SkipDelegate.php +++ /dev/null @@ -1,22 +0,0 @@ -d = $d; - } - - public function run() - { - return $this->d->format('Y-m-d H:i:s'); - } -}