Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"phpunit/phpunit": "^13.0",
"symfony/framework-bundle": "^6.2",
"illuminate/container": "^11.0",
"phpecs/phpecs": "^2.2",
"symplify/easy-coding-standard": "^13.1",
"tomasvotruba/class-leak": "^2.1",
"rector/rector": "^2.4",
"phpstan/extension-installer": "^1.4",
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
StringClassNameToClassConstantRector::class => [
__DIR__ . '/src/Enum',
__DIR__ . '/src/Testing/PHPUnitTestAnalyser.php',
__DIR__ . '/src/Rules/NoEntityOutsideEntityNamespaceRule.php',
__DIR__ . '/tests/Naming/ClassToSuffixResolverTest.php',
__DIR__ . '/src/Doctrine/DoctrineEntityDocumentAnalyser.php',
],
]);
27 changes: 27 additions & 0 deletions src/Doctrine/DoctrineEntityDocumentAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,40 @@
*/
private const array ENTITY_DOCBLOCK_MARKERS = ['@Document', '@ORM\\Document', '@Entity', '@ORM\\Entity'];

/**
* @var string[]
*/
private const array ENTITY_ATTRIBUTES = [
'Doctrine\\ORM\\Mapping\\Entity',
'Doctrine\\ODM\\MongoDB\\Mapping\\Annotations\\Document',
];

public static function isEntityClass(ClassReflection $classReflection): bool
{
if (self::hasEntityAttribute($classReflection)) {
return true;
}

$resolvedPhpDocBlock = $classReflection->getResolvedPhpDoc();
if (! $resolvedPhpDocBlock instanceof ResolvedPhpDocBlock) {
return false;
}

return array_any(self::ENTITY_DOCBLOCK_MARKERS, fn (string $entityDocBlockMarker): bool => str_contains($resolvedPhpDocBlock->getPhpDocString(), $entityDocBlockMarker));
}

private static function hasEntityAttribute(ClassReflection $classReflection): bool
{
$attributeReflections = $classReflection->getNativeReflection()
->getAttributes();

return array_any(
$attributeReflections,
static fn ($reflectionAttribute): bool => in_array(
$reflectionAttribute->getName(),
self::ENTITY_ATTRIBUTES,
true
)
);
}
}
12 changes: 12 additions & 0 deletions stubs/Doctrine/ORM/Mapping/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Doctrine\ORM\Mapping;

if (class_exists('Doctrine\ORM\Mapping\Entity')) {
return;
}

#[\Attribute(\Attribute::TARGET_CLASS)]
final class Entity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoEntityMockingRule\Fixture;

use PHPUnit\Framework\TestCase;
use Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoEntityMockingRule\Source\SomeAttributeEntity;

final class MockingAttributeEntity extends TestCase
{
public function test(): void
{
$someEntityMock = $this->createMock(SomeAttributeEntity::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public static function provideData(): Iterator
{
yield [__DIR__ . '/Fixture/MockingEntity.php', [[NoEntityMockingRule::ERROR_MESSAGE, 12]]];

yield [__DIR__ . '/Fixture/MockingAttributeEntity.php', [[NoEntityMockingRule::ERROR_MESSAGE, 12]]];

yield [__DIR__ . '/Fixture/MockingDocument.php', [[NoEntityMockingRule::ERROR_MESSAGE, 12]]];

yield [__DIR__ . '/Fixture/SkipMockingOtherObject.php', []];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoEntityMockingRule\Source;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
class SomeAttributeEntity
{
}
Loading