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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions src/NodeAnalyzer/DoctrineEntityDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\NodeAnalyzer\DoctrineEntityDetector;

use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Doctrine\NodeAnalyzer\DoctrineEntityDetector;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class DoctrineEntityDetectorTest extends AbstractLazyTestCase
{
private DoctrineEntityDetector $doctrineEntityDetector;

protected function setUp(): void
{
parent::setUp();

$this->doctrineEntityDetector = $this->make(DoctrineEntityDetector::class);
}

public function testDetectStaticFunctionMapping(): void
{
$class = new Class_('SomeEntity');
$class->stmts[] = new ClassMethod('loadMetadata');

$this->assertTrue($this->doctrineEntityDetector->detect($class));
}
}
Loading