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

False positive for UnusedPrivateMethod when method is called on clone #937

Open
sveneld opened this issue Jan 24, 2022 · 4 comments · May be fixed by #1069
Open

False positive for UnusedPrivateMethod when method is called on clone #937

sveneld opened this issue Jan 24, 2022 · 4 comments · May be fixed by #1069
Assignees
Milestone

Comments

@sveneld
Copy link

sveneld commented Jan 24, 2022

  • PHPMD version: 2.11.0
  • PHP Version: 7.4.3
  • Installation type: composer
  • Operating System / Distribution & Version: Ubuntu 20.04.3 LTS

Current Behavior

<?php

class Test
{

    private $keywords = [];

    public function testClone()
    {
        $clone = clone $this;
        $clone->prepareKeywords();

        return $clone;
    }

    private function prepareKeywords()
    {
        $this->keywords = ['a', 'b'];
    }
}

phpmd: Avoid unused private methods such as 'prepareKeywords'.

Expected Behavior

No error

Steps To Reproduce:

Run phpmd inspection on provided code

@tvbeek
Copy link
Member

tvbeek commented Jan 24, 2022

@sveneld thanks for your report, this is an interesting case. If this function is only used after the clone I should personal use the __ clone function. See: https://www.php.net/clone
I'm not sure if we can check this correct, do you have the possibility to create a PR with a failing test? That can be a nice starting point.

ps. I have edited your post to have the code highlighting working.

@tvbeek tvbeek added the Bug label Jan 24, 2022
@sveneld
Copy link
Author

sveneld commented Jan 24, 2022

Ok, I will try to create PR with failing test

@sveneld
Copy link
Author

sveneld commented Feb 6, 2022

@tvbeek #940 here it is

@cs278
Copy link

cs278 commented Apr 1, 2022

Basically the problem (using 2.12.0), by creating a new instance in a static method:

Avoid unused private methods such as 'doThing'

final class Test
{
    public static function create(): self
    {
        $obj = new self();
        $obj->doThing();

        return $obj;
    }

    private function doThing(): void
    {
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

4 participants