Skip to content

Commit

Permalink
[FinalInternalClassFixer] Fix Entities with Repo (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed May 31, 2022
1 parent 85e2263 commit b229ded
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/FinalInternalClassFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

final class FinalInternalClassFixer extends AbstractFixer
{
private const REGEX = '(?:final|\\\\Doctrine\\\\ORM\\\\Mapping\\\\Entity|ORM\\\\Mapping\\\\Entity|ORM\\\\Entity|Entity)';

public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
Expand Down Expand Up @@ -65,7 +67,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void
private static function isDoctrineEntity(Tokens $tokens, int $classIndex): bool
{
$docToken = $tokens[$tokens->getPrevNonWhitespace($classIndex)];
if ($docToken->isGivenKind(\T_DOC_COMMENT) && false !== \mb_strpos($docToken->getContent(), '@ORM\Entity')) {
if ($docToken->isGivenKind(\T_DOC_COMMENT) && 1 === \preg_match(\sprintf('/@%s/', self::REGEX), $docToken->getContent())) {
return true;
}

Expand All @@ -76,10 +78,7 @@ private static function isDoctrineEntity(Tokens $tokens, int $classIndex): bool
for ($index = $attributeOpenIndex; $index < $classIndex; ++$index) {
$content .= $tokens[$index]->getContent();
}
if (false !== \mb_strpos($content, '#[ORM\Entity]')) {
return true;
}
if (false !== \mb_strpos($content, '#[\Doctrine\ORM\Mapping\Entity')) {
if (1 === \preg_match(\sprintf('/^#\[%s/', self::REGEX), $content)) {
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/FinalInternalClassFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ public function provideCases(): array
[
"<?php\n/**\n * @ORM\\Entity\n */\nclass MyEntity {}",
],
[
"<?php\n/**\n * @final\n */\nclass MyEntity {}",
],
[
"<?php\n#[ORM\\Entity]\nclass MyEntity {}",
],
[
"<?php\n#[ORM\\Entity]\n#[CustomAttribute]\nclass MyEntity {}",
],
[
"<?php\n#[ORM\\Entity(repositoryClass: \\MyClass::class)]\n#[CustomAttribute]\nclass MyEntity {}",
],
[
"<?php\n#[\\Doctrine\\ORM\\Mapping\\Entity(repositoryClass: \\MyClass::class)]\nclass MyEntity {}",
],
Expand Down

0 comments on commit b229ded

Please sign in to comment.