PHPMD - Avoid using Static Access to class - Using a constant?

2.7k views Asked by At

I am using PHP Mess Detector 1.5.0 and have a number of issues, that I do not believe are issues.

class FOO
{
    const NEW_LINE = "\n";
    const ADD = 1;
    const DELETE = 2;
    ...

    public function __construct($OptionOne = self::ADD)
    {
    }
}

In this code, I then use the constants similar to enumerations in C++ to work with options. Therefore, if I want to ADD something, I pass FOO::ADD in my code to the class, which is the same as 1.

I can test this class without an issue in PHPUnit, as I validate the constants, then I can pass the values and constants to functions.

I am not sure why PHPMD would complain about this, especially since it is a const value being referenced safely in PHP.

1

There are 1 answers

1
Alejandro Salamanca Mazuelo On BEST ANSWER

Because I use a lot of self:: for constants, change phpmd code to accept self:: and parent::.

In the program PHP/PMD/Rule/CleanCode/StaticAccess.php at line 36, change to:

if ($this->isReferenceInParameter($reference)
    || $reference->getImage() === 'self' 
    || $reference->getImage() === 'parent' 
    ) {
    continue;
}