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 @@ +