Skip to content

Commit

Permalink
Add the FindByPkFixer
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Mar 8, 2024
1 parent 43ef31d commit e997296
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/contao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,6 +121,7 @@
DisallowArrayTypeHintSyntaxSniff::class,
DisallowDirectMagicInvokeCallSniff::class,
ExpectsWithCallbackFixer::class,
FindByPkFixer::class,
FunctionCallWithMultilineArrayFixer::class,
GitMergeConflictSniff::class,
HeredocIndentationFixer::class,
Expand Down
4 changes: 4 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,9 @@
CommentLengthFixer::class => [
'config/contao.php',
],
FindByPkFixer::class => [
'src/Fixer/FindByPkFixer.php',
],
ReferenceUsedNamesOnlySniff::class => [
'config/contao.php',
],
Expand Down
81 changes: 81 additions & 0 deletions src/Fixer/FindByPkFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\EasyCodingStandard\Fixer;

use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class FindByPkFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'When searching models by ID, findById() should be used instead of findByPk().',
[
new CodeSample(
<<<'EOT'
<?php
public function findModel(int $id): void
{
return Model::findById($id);
}
EOT,
),
],
);
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->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']);
}
}
75 changes: 75 additions & 0 deletions tests/Fixer/FindByPkFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\EasyCodingStandard\Tests\Fixer;

use Contao\EasyCodingStandard\Fixer\FindByPkFixer;
use PhpCsFixer\Tokenizer\Tokens;
use PHPUnit\Framework\TestCase;

class FindByPkFixerTest extends TestCase
{
/**
* @dataProvider getCodeSamples
*/
public function testFixesTheCode(string $code, string $expected): void
{
$tokens = Tokens::fromCode($code);

$fixer = new FindByPkFixer();
$fixer->fix($this->createMock(\SplFileInfo::class), $tokens);

$this->assertSame($expected, $tokens->generateCode());
}

public function getCodeSamples(): \Generator
{
yield [
<<<'EOT'
<?php
class Foobar
{
public function findModel(int $id): void
{
$foo = Model::findByPk($id);
$bar = $this->getAdapter(Model::class)->findByPk($id);
$baz = call_user_func(Model::class, 'findByPk');
}
public function findByPk(int $id): void
{
parent::findByPk($id);
}
}
EOT,
<<<'EOT'
<?php
class Foobar
{
public function findModel(int $id): void
{
$foo = Model::findById($id);
$bar = $this->getAdapter(Model::class)->findById($id);
$baz = call_user_func(Model::class, 'findById');
}
public function findByPk(int $id): void
{
parent::findByPk($id);
}
}
EOT,
];
}
}

0 comments on commit e997296

Please sign in to comment.