phpmd: Undefined Variable Rule does not understand static class variables.

  • PHPMD version: 2.8.0
  • PHP Version: 7.2.25
  • Installation type: composer
  • Operating System / Distribution & Version: Debian 9.6

Current Behavior

The new feature “Undefined Variable Rule” introduced in https://github.com/phpmd/phpmd/pull/497 is not working with static class variables, such as

class CmsBlock
{
    protected static $cmsBlockBuffer = [];

    protected function getCmsBlockById(string $cmsBlockKey): array
    {
        if (isset(static::$cmsBlockBuffer[$cmsBlockKey])) {
            return static::$cmsBlockBuffer[$cmsBlockKey];
        }
     
        return static::$cmsBlockBuffer[$cmsBlockKey] = $this->findCmsBlockByKey($cmsBlockKey);
    }
}

PhpMd triggers error message Avoid using undefined variables such as '$cmsBlockBuffer' which will lead to PHP notices.

Expected Behavior

No error messages.

Steps To Reproduce:

Explain all the steps you did to create this bug so we can reproduce it.

Checks before submitting

  • [+] Be sure that there isn’t already an issue about this. See: Issues list
  • [+] Be sure that there isn’t already a pull request about this. See: Pull requests
  • [+] I have added every step to reproduce the bug.
  • [+] If possible I added relevant code examples.
  • [+] This issue is about 1 bug and nothing more.
  • [+] The issue has a descriptive title. For example: “JSON rendering failed on Windows for filenames with space”.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 21
  • Comments: 22 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I ran some tests. All is fine as long as the static variable is not an array. So the probable cause is PDepend evaluate the $array[$key] before static::$array, so it looks for a local variable named $array instead of the static property static::$array. The fact that the error is not raised when wrapping the property in parentheses (static::$cmsBlockBuffer) tends to confirm this hypothesis.

If this feature is important to you, you could also hire a freelancer to work on it

I totally understand, maintaining an opensource package is not an easy task and very time-consuming too, we are all thankful for this great opensource project and we are wishing for it to progress further.

on the other hand, what I don’t understand is the upset from someone telling the users not to expect bugs to be fixed, I believe that everybody has the right to know about the status of an opensource project and whether it is maintained or not, people should understand that they need to hire freelancers for this project if they want it to be maintained before starting using it.

@mshamaseen the beauty of opensource is that everyone can contribute 😃 Feel free to create a PR with a fix and test.

It is 2022 and everyone is welcome to contribute and improve the tools that are provided for free. I love that because the maintainers doesn’t have an unlimited amount of time, energy and motivation so they need to prioritize.

2022 and this issue is still not fixed in version 2.11.1

It’s not. I see the difference with the initial issue, return self::$test[0]; does not trigger the error while return self::$test[0]->foo(); triggers it because the ->foo() is parsed before the self::.

I’m on it.

I have a wide refactorization in progress because the new rules about variables revealed problems in the way we detect variables then detect if they are related to a read or write operation that are actually in PDepend for a very long time.

This should fix this error but it will take time.