From e99729650019b9c4e4e62c12116c603ecab22a24 Mon Sep 17 00:00:00 2001 From: Leo Feyer <1192057+leofeyer@users.noreply.github.com> Date: Fri, 8 Mar 2024 08:49:10 +0100 Subject: [PATCH] Add the FindByPkFixer --- config/contao.php | 2 + ecs.php | 4 ++ src/Fixer/FindByPkFixer.php | 81 +++++++++++++++++++++++++++++++ tests/Fixer/FindByPkFixerTest.php | 75 ++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 src/Fixer/FindByPkFixer.php create mode 100644 tests/Fixer/FindByPkFixerTest.php diff --git a/config/contao.php b/config/contao.php index 96b70bb..0fcbcd9 100644 --- a/config/contao.php +++ b/config/contao.php @@ -15,6 +15,7 @@ use Contao\EasyCodingStandard\Fixer\ChainedMethodBlockFixer; use Contao\EasyCodingStandard\Fixer\CommentLengthFixer; use Contao\EasyCodingStandard\Fixer\ExpectsWithCallbackFixer; +use Contao\EasyCodingStandard\Fixer\FindByPkFixer; use Contao\EasyCodingStandard\Fixer\FunctionCallWithMultilineArrayFixer; use Contao\EasyCodingStandard\Fixer\InlinePhpdocCommentFixer; use Contao\EasyCodingStandard\Fixer\IsArrayNotEmptyFixer; @@ -120,6 +121,7 @@ DisallowArrayTypeHintSyntaxSniff::class, DisallowDirectMagicInvokeCallSniff::class, ExpectsWithCallbackFixer::class, + FindByPkFixer::class, FunctionCallWithMultilineArrayFixer::class, GitMergeConflictSniff::class, HeredocIndentationFixer::class, diff --git a/ecs.php b/ecs.php index 5bf8aec..61406f4 100644 --- a/ecs.php +++ b/ecs.php @@ -3,6 +3,7 @@ declare(strict_types=1); use Contao\EasyCodingStandard\Fixer\CommentLengthFixer; +use Contao\EasyCodingStandard\Fixer\FindByPkFixer; use PhpCsFixer\Fixer\Comment\HeaderCommentFixer; use SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff; use Symplify\EasyCodingStandard\Config\ECSConfig; @@ -13,6 +14,9 @@ CommentLengthFixer::class => [ 'config/contao.php', ], + FindByPkFixer::class => [ + 'src/Fixer/FindByPkFixer.php', + ], ReferenceUsedNamesOnlySniff::class => [ 'config/contao.php', ], diff --git a/src/Fixer/FindByPkFixer.php b/src/Fixer/FindByPkFixer.php new file mode 100644 index 0000000..81c8660 --- /dev/null +++ b/src/Fixer/FindByPkFixer.php @@ -0,0 +1,81 @@ +isAnyTokenKindsFound([T_STRING, T_CONSTANT_ENCAPSED_STRING]); + } + + protected function applyFix(\SplFileInfo $file, Tokens $tokens): void + { + for ($index = 1, $count = \count($tokens); $index < $count; ++$index) { + if (!$tokens[$index]->isGivenKind([T_STRING, T_CONSTANT_ENCAPSED_STRING])) { + continue; + } + + $content = $tokens[$index]->getContent(); + + if (!\in_array($content, ['findByPk', "'findByPk'", '"findByPk"'], true)) { + continue; + } + + if ($tokens[$index]->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) { + $tokens->offsetSet($index, new Token([T_CONSTANT_ENCAPSED_STRING, "'findById'"])); + } elseif ($this->isMethodCall($index, $tokens)) { + $tokens->offsetSet($index, new Token([T_STRING, 'findById'])); + } + } + } + + private function isMethodCall(int $index, Tokens $tokens): bool + { + if (!$tokens[$index + 1]->equals('(')) { + return false; + } + + if (!$tokens[$index - 1]->isGivenKind([T_OBJECT_OPERATOR, T_PAAMAYIM_NEKUDOTAYIM])) { + return false; + } + + return !$tokens[$index - 2]->equals([T_STRING, 'parent']); + } +} diff --git a/tests/Fixer/FindByPkFixerTest.php b/tests/Fixer/FindByPkFixerTest.php new file mode 100644 index 0000000..57116df --- /dev/null +++ b/tests/Fixer/FindByPkFixerTest.php @@ -0,0 +1,75 @@ +fix($this->createMock(\SplFileInfo::class), $tokens); + + $this->assertSame($expected, $tokens->generateCode()); + } + + public function getCodeSamples(): \Generator + { + yield [ + <<<'EOT' + getAdapter(Model::class)->findByPk($id); + $baz = call_user_func(Model::class, 'findByPk'); + } + + public function findByPk(int $id): void + { + parent::findByPk($id); + } + } + EOT, + <<<'EOT' + getAdapter(Model::class)->findById($id); + $baz = call_user_func(Model::class, 'findById'); + } + + public function findByPk(int $id): void + { + parent::findByPk($id); + } + } + EOT, + ]; + } +}