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
13 changes: 12 additions & 1 deletion src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,18 @@ public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingSc
throw new ShouldNotHappenException($errorMessage);
}

NodeAttributeReIndexer::reIndexNodeAttributes($node);
/**
* The reindex is needed to:
* - be used by PHPStan processNodes() that relies on indexed arrays start from 0
* - use traverser to avoid issues when multiples rules apply, and higher node remove deep node,
* which the next rule use deep node, for example:
* - first rule: - Class_ → ClassMethod → remove stmt with index 0
* - second rule: - ClassMethod → here fetch the index 0 that no longer exists
*/
SimpleCallableNodeTraverser::traverse(
$node,
fn (Node $subNode): ?Node => NodeAttributeReIndexer::reIndexNodeAttributes($subNode)
);

$stmts = $this->resolveStmts($node);
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent\Fixture;

use Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent\Source\SomeParentWithEmptyConstruct;

class Fixture extends SomeParentWithEmptyConstruct
{
private string $prop;

public function __construct(string $prop)
{
$this->prop = $prop;
parent::__construct();
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent\Fixture;

use Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent\Source\SomeParentWithEmptyConstruct;

class Fixture extends SomeParentWithEmptyConstruct
{
public function __construct(private string $prop)
{
parent::__construct();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class IssuePropertyPromoRemoveDelegatingParentTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

declare(strict_types=1);

namespace Rector\Tests\Issues\IssuePropertyPromoRemoveDelegatingParent\Source;

abstract class SomeParentWithEmptyConstruct
{
public function __construct()
{
$this->init();
}

private function init(): void
{
echo 'A init';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveParentDelegatingConstructorRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;

return RectorConfig::configure()
->withRules([
ClassPropertyAssignToConstructorPromotionRector::class,
RemoveParentDelegatingConstructorRector::class,
]);