diff --git a/rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php b/rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php index 3fa370bc..2ce8a83e 100644 --- a/rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php +++ b/rules/Orm30/Rector/MethodCall/CastDoctrineExprToStringRector.php @@ -69,12 +69,11 @@ public function getNodeTypes(): array return [MethodCall::class]; } + /** + * @param MethodCall $node + */ public function refactor(Node $node): ?Node { - if (! $node instanceof MethodCall) { - return null; - } - if (! $this->isObjectType($node->var, new ObjectType(DoctrineClass::QUERY_EXPR))) { return null; } diff --git a/src/NodeAnalyzer/DoctrineEntityDetector.php b/src/NodeAnalyzer/DoctrineEntityDetector.php index 17cd9dc9..05ed978e 100644 --- a/src/NodeAnalyzer/DoctrineEntityDetector.php +++ b/src/NodeAnalyzer/DoctrineEntityDetector.php @@ -6,6 +6,7 @@ use PhpParser\Node\Name; use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Stmt\ClassMethod; use PHPStan\Reflection\ReflectionProvider; use Rector\NodeAnalyzer\DoctrineEntityAnalyzer; @@ -22,7 +23,13 @@ public function __construct( public function detect(Class_ $class): bool { - // A. check annotations + // A. check static function mapping, fields are mapped in loadMetadata() method + // @see https://www.doctrine-project.org/projects/doctrine-orm/en/3.6/reference/php-mapping.html#static-function + if ($class->getMethod('loadMetadata') instanceof ClassMethod) { + return true; + } + + // B. check annotations if ($this->doctrineEntityAnalyzer->hasClassAnnotation($class)) { return true; } @@ -33,7 +40,7 @@ public function detect(Class_ $class): bool $className = $class->namespacedName->toString(); - // B. check attributes + // C. check attributes $classReflection = $this->reflectionProvider->getClass($className); return $this->doctrineEntityAnalyzer->hasClassReflectionAttribute($classReflection); diff --git a/tests/NodeAnalyzer/DoctrineEntityDetector/DoctrineEntityDetectorTest.php b/tests/NodeAnalyzer/DoctrineEntityDetector/DoctrineEntityDetectorTest.php new file mode 100644 index 00000000..493ecb35 --- /dev/null +++ b/tests/NodeAnalyzer/DoctrineEntityDetector/DoctrineEntityDetectorTest.php @@ -0,0 +1,30 @@ +doctrineEntityDetector = $this->make(DoctrineEntityDetector::class); + } + + public function testDetectStaticFunctionMapping(): void + { + $class = new Class_('SomeEntity'); + $class->stmts[] = new ClassMethod('loadMetadata'); + + $this->assertTrue($this->doctrineEntityDetector->detect($class)); + } +}