Skip to content

RemoveEmptyClassMethodRector removes private method __clone() #9337

@shmaltorhbooks

Description

@shmaltorhbooks

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/aab572e1-78b6-475a-94f7-83c08cf5673b

<?php

class Foo
{
    private function __clone()
    {
    }
}

Responsible rules

  • RemoveEmptyClassMethodRector

Expected Behaviour

The refactored php code should keep private/protected function __clone(), since the inability to clone the object is part of its behavior, and such refactoring breaks the object's behavior.

In other words, the refactored code should be like this:

<?php

class Foo
{
    private function __clone()
    {
    }
}

$foo = new Foo();
# expected: PHP Fatal error:  Uncaught Error: Call to private Foo::__clone() from global scope ...
$clone = clone $foo;

Actual Behaviour

Private method __clone deleted, object's behavior is broken

class Foo
{
}

$foo = new Foo();
# No exception here!
$clone = clone $foo;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions