From 63615f41971bbbe795aa7a86070569df9e8ad4d8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 18 Oct 2020 19:14:55 +0700 Subject: [PATCH] [PHPStan] Fixes #4433 Add CheckCodeSampleBeforeAfterAlwaysDifferentRule (#4439) --- .../config/phpstan-extensions.neon | 4 ++ ...deSampleBeforeAfterAlwaysDifferentRule.php | 58 +++++++++++++++++++ ...mpleBeforeAfterAlwaysDifferentRuleTest.php | 39 +++++++++++++ .../Fixture/BeforeAfterDifferent.php | 9 +++ .../Fixture/BeforeAfterSame.php | 9 +++ 5 files changed, 119 insertions(+) create mode 100644 utils/phpstan-extensions/src/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule.php create mode 100644 utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest.php create mode 100644 utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/Fixture/BeforeAfterDifferent.php create mode 100644 utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/Fixture/BeforeAfterSame.php diff --git a/utils/phpstan-extensions/config/phpstan-extensions.neon b/utils/phpstan-extensions/config/phpstan-extensions.neon index 7e58451a05e..d95b298dbd4 100644 --- a/utils/phpstan-extensions/config/phpstan-extensions.neon +++ b/utils/phpstan-extensions/config/phpstan-extensions.neon @@ -23,6 +23,10 @@ services: class: Rector\PHPStanExtensions\Rule\RectorRuleAndValueObjectHaveSameStartsRule tags: [phpstan.rules.rule] + - + class: Rector\PHPStanExtensions\Rule\CheckCodeSampleBeforeAfterAlwaysDifferentRule + tags: [phpstan.rules.rule] + - Rector\PHPStanExtensions\Utils\PHPStanValueResolver # $node->getAttribute($1) => Type|null by $1 diff --git a/utils/phpstan-extensions/src/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule.php b/utils/phpstan-extensions/src/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule.php new file mode 100644 index 00000000000..d8154f0340a --- /dev/null +++ b/utils/phpstan-extensions/src/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule.php @@ -0,0 +1,58 @@ +class; + if (! $class instanceof FullyQualified) { + return []; + } + + $className = $class->toString(); + if ($className !== CodeSample::class) { + return []; + } + + $args = $node->args; + /** @var String_ $firstParameter */ + $firstParameter = $args[0]->value; + /** @var String_ $secondParameter */ + $secondParameter = $args[1]->value; + + if ($firstParameter->value !== $secondParameter->value) { + return []; + } + + return [self::ERROR_MESSAGE]; + } +} diff --git a/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest.php b/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest.php new file mode 100644 index 00000000000..f054d7f1735 --- /dev/null +++ b/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/CheckCodeSampleBeforeAfterAlwaysDifferentRuleTest.php @@ -0,0 +1,39 @@ + $expectedErrorsWithLines + */ + public function testRule(string $filePath, array $expectedErrorsWithLines): void + { + $this->analyse([$filePath], $expectedErrorsWithLines); + } + + public function provideData(): Iterator + { + yield [__DIR__ . '/Fixture/BeforeAfterDifferent.php', []]; + yield [ + __DIR__ . '/Fixture/BeforeAfterSame.php', + [[CheckCodeSampleBeforeAfterAlwaysDifferentRule::ERROR_MESSAGE, 9]], + ]; + } + + protected function getRule(): Rule + { + return $this->getRuleFromConfig( + CheckCodeSampleBeforeAfterAlwaysDifferentRule::class, + __DIR__ . '/../../../config/phpstan-extensions.neon' + ); + } +} diff --git a/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/Fixture/BeforeAfterDifferent.php b/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/Fixture/BeforeAfterDifferent.php new file mode 100644 index 00000000000..f19967e5a0a --- /dev/null +++ b/utils/phpstan-extensions/tests/Rule/CheckCodeSampleBeforeAfterAlwaysDifferentRule/Fixture/BeforeAfterDifferent.php @@ -0,0 +1,9 @@ +