-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |