Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Php81] Skip param variable used in next stmt after coalesce on NewInInitializerRector #5971

Merged
merged 2 commits into from
Jun 14, 2024
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,15 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;

final class SkipVariableUsedNextStmt
{
private \DateTime $time;
private bool $timeWasSet;

public function __construct(?\DateTime $time = null)
{
$this->time = $time ?? new \DateTime();
$this->timeWasSet = null !== $time;
}
}
11 changes: 11 additions & 0 deletions rules/Php81/Rector/ClassMethod/NewInInitializerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php81\NodeAnalyzer\CoalesePropertyAssignMatcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
Expand All @@ -33,6 +34,7 @@ public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly ClassChildAnalyzer $classChildAnalyzer,
private readonly CoalesePropertyAssignMatcher $coalesePropertyAssignMatcher,
private readonly StmtsManipulator $stmtsManipulator
) {
}

Expand Down Expand Up @@ -100,6 +102,11 @@ public function refactor(Node $node): ?Node

$hasChanged = false;

// stmts variable defined to avoid unset overlap when used via array_slice() on
// StmtsManipulator::isVariableUsedInNextStmt()
// @see https://github.com/rectorphp/rector-src/pull/5968
// @see https://3v4l.org/eojhk
$stmts = (array) $constructClassMethod->stmts;
foreach ((array) $constructClassMethod->stmts as $key => $stmt) {
foreach ($params as $param) {
$paramName = $this->getName($param);
Expand All @@ -112,6 +119,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $key + 1, $paramName)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with rector code, so what does nextstmt means ? @samsonasik

Does it also fix the bug for code like

public function __construct(?\DateTime $time = null)
    {
        $this->timeWasSet = null !== $time; // Here the statement is before 
        $this->time = $time ?? new \DateTime();
    }

or

public function __construct(?\DateTime $time = null)
    {
        $this->time = $time ?? new \DateTime();

        // Some code...

        $a = random_int(0, 1);
        if ($a === 1) {
            $this->timeWasSet = null !== $time; // Here it's not the direct next statement
        }
    }

Copy link
Member Author

@samsonasik samsonasik Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That mean variable used by next stmt start by key + 1 until end of stmts.

Yes, that should cover that, feel free to try latest dev-main

continue;
}

/** @var NullableType $currentParamType */
$currentParamType = $param->type;

Expand Down