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

Unused private field false positive when called on other objects #230

Closed
jdufresne opened this issue Nov 23, 2014 · 2 comments
Closed

Unused private field false positive when called on other objects #230

jdufresne opened this issue Nov 23, 2014 · 2 comments
Labels
Milestone

Comments

@jdufresne
Copy link

Observe the following test case:

<?php

class foo {
    private $bar;

    public static function create() {
        $foo = new foo();
        $foo->bar = 'baz';
    }
}
$ phpmd test.php text unusedcode

/home/jon/test.php:4    Avoid unused private fields such as '$bar'.

The member $bar is not unused as it is used in the foo::create() method. I think the confusion is due to the fact that it is called on an object other than $this. This is legal PHP as stated in the documentation: http://php.net/manual/en/language.oop5.visibility.php#language.oop5.visibility-other-objects

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

$ phpmd --version
PHPMD 1.5.0 by Manuel Pichler
@ravage84 ravage84 modified the milestone: 2.3.3 Nov 7, 2015
@ravage84 ravage84 modified the milestones: 2.3.3, 2.4.2 Mar 8, 2016
@manuelpichler manuelpichler modified the milestones: 2.4.2, 2.4.3 Mar 10, 2016
@ravage84 ravage84 modified the milestones: 2.4.3, 2.4.4 Apr 25, 2016
@mikebronner
Copy link

I'm seeing this issue as well in version 2.4.3 in the following instance:

class Test
{
    private $test;

    public function test()
    {
        $this->test = 5;
    }
}

@ravage84
Copy link
Member

Duplicate of or at least related to #252

@ravage84 ravage84 modified the milestones: 2.5.1, 2.6.0 Jan 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

4 participants