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
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

use Doctrine\ORM\Mapping\ClassMetadata;

final class SkipStaticFunctionMapping
{
private $name;

public static function loadMetadata(ClassMetadata $metadata): void
{
$metadata->mapField([
'fieldName' => 'name',
'type' => 'string',
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture\Doctrine;

use Doctrine\ORM\Mapping\ClassMetadata;

final class SkipStaticFunctionMapping
{
private $name;

public function run()
{
$this->name = 'string';
}

public static function loadMetadata(ClassMetadata $metadata): void
{
$metadata->mapField([
'fieldName' => 'name',
'type' => 'string',
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Return_;
Expand Down Expand Up @@ -144,6 +145,12 @@ private function shouldRemoveProperty(Class_ $class, Property $property): bool

private function shouldSkipClass(Class_ $class): bool
{
// skip Doctrine static function mapping, properties 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;
}

foreach ($class->stmts as $stmt) {
// unclear what property can be used there
if ($stmt instanceof TraitUse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\UnionType as NodeUnionType;
use PHPStan\Reflection\ClassReflection;
Expand Down Expand Up @@ -129,6 +130,12 @@ public function provideMinPhpVersion(): int
*/
public function refactor(Node $node): ?Node
{
// skip Doctrine static function mapping, properties are mapped in loadMetadata() method
// @see https://www.doctrine-project.org/projects/doctrine-orm/en/3.6/reference/php-mapping.html#static-function
if ($node->getMethod('loadMetadata') instanceof ClassMethod) {
return null;
}

$hasChanged = false;
$classReflection = null;

Expand Down
Loading