Skip to content

[DeadCode] Keep clone statement in RemoveDeadStmtRector - #9044

Merged
TomasVotruba merged 1 commit into
mainfrom
remove-dead-stmt-keep-clone
Nov 23, 2026
Merged

TomasVotruba merged 1 commit into
mainfrom
remove-dead-stmt-keep-clone

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

RemoveDeadStmtRector removed a bare clone $object; statement unless the resolved class itself declared __clone(). Cloning runs __clone(), which can have side effects (counter, logging, deep-copy registry), so removing it changes behavior - for an untyped $object, or a non-final class whose subclass declares __clone().

function run($object)
{
    clone $object;
}

Before, the statement was removed:

 function run($object)
 {
-    clone $object;
 }

After, it is kept.

The fix keeps clone always. Proving it safe to remove would need the class (and all parents) to be final with no __clone(), which is rare in practice and not worth the reflection logic. The existing hasCloneMagicMethod() check is now redundant, so it is removed. The clone.php.inc fixture is turned into skip_clone.php.inc.

@TomasVotruba
TomasVotruba merged commit 13e688b into main Nov 23, 2026
51 checks passed
@TomasVotruba
TomasVotruba deleted the remove-dead-stmt-keep-clone branch November 23, 2026 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant