Skip to content

Commit

Permalink
Found a bug while going through the d11 run on project analysis (#274)
Browse files Browse the repository at this point in the history
* Found a bug while going through the d11 run on project analysis

* Be more defensive while refactoring entitymanager

* Fix codestyle

* Fix mistake with depth

* Extra isntance check for phpstan

* Simplify second if in findInstanceByNameInAssign

* Remove disbled old rule

* Is has been implemented

* We need to check for ->name. We cannot use getName(node) for some weird reason

* Fix codestyle, and add phpstan hint to make it happy
  • Loading branch information
bbrala authored Dec 5, 2023
1 parent a08a47d commit eab4674
Show file tree
Hide file tree
Showing 5 changed files with 574 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Drupal8/Rector/Deprecation/EntityManagerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,16 @@ public function refactor(Node $node): ?Node
return null;
}

// The expressino of the assign is a method call. Therefor we need to check if there is
// The expression of the assign is a method call. Therefor we need to check if there is
// entityManager somewhere up the call.
if ($isAssignedMethodCall || $isAssignedStaticCall) {
$expr = $this->findInstanceByNameInAssign($node->expr, Node\Expr\CallLike::class, 'entityManager');
if (!is_null($expr)) {
$expr = $this->refactorExpression($expr, $node);
$node->expr = $this->replaceInstanceByNameInAssign($node->expr, $expr, Node\Expr\CallLike::class, 'entityManager');
$exprRefactored = $this->refactorExpression($expr, $node);
if (is_null($exprRefactored)) {
return null;
}
$node->expr = $this->replaceInstanceByNameInAssign($node->expr, $exprRefactored, Node\Expr\CallLike::class, 'entityManager');

return $node;
}
Expand All @@ -133,6 +136,8 @@ public function refactor(Node $node): ?Node
*
* @see DrupalRector\Rector\Deprecation\Base\DBBase
*
* @phpstan-param class-string<Node> $class
*
* @param Node\Expr\Assign $assign
* @param string $class
* @param string $name
Expand All @@ -145,17 +150,18 @@ public function findInstanceByNameInAssign(Node\Expr\Assign $assign, string $cla
$depth = 0;

// Should the expression be the class we are looking for and the name is the one we are looking for, we can return early.
if ($node instanceof $class && $this->getName($node->name) === $name) {
if ($node instanceof Node && $node instanceof $class && $this->getName($node->name) === $name) {
$node->setAttribute(self::class, $depth);

return $node;
}

// Find the relevant class with name in the chain.
while (isset($node->var) && !($node->var instanceof $class && $this->getName($node->var->name) !== $name)) {
while (isset($node->var) && !($node->var instanceof $class && isset($node->var->name) && $this->getName($node->var->name) !== $name)) {
$node = $node->var;
++$depth;
if ($node instanceof $class && $this->getName($node->name)) {

if ($node instanceof $class && isset($node->name) && $this->getName($node->name) === $name) {
$node->setAttribute(self::class, $depth);

return $node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Drupal8\Rector\Deprecation\EntityManagerRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

class EntityManagerRectorTest extends AbstractRectorTestCase
{
/**
* @covers ::refactor
*
* @dataProvider provideData
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<<string>>
*/
public static function provideData(): \Iterator
{
return self::yieldFilesFromDirectory(__DIR__.'/fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__.'/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use DrupalRector\Drupal8\Rector\Deprecation\EntityManagerRector;
use DrupalRector\Tests\Rector\Deprecation\DeprecationBase;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
DeprecationBase::addClass(EntityManagerRector::class, $rectorConfig);
};
Loading

0 comments on commit eab4674

Please sign in to comment.