[PHP] Detect usages of undefined variables

Summary

Forbid usages of undefined local variables in classes/methods.

Type

Bug

Jira Link

https://jira.sonarsource.com/browse/SONARPHP-262

Code Sample

<?php

class TestUnknownVariables
{
    public function badFunction($a)
    {
        $p = $a + 2;
        $p = $p - $pp; // Usage of undefined variable

        echo $p;
    }

    public function goodFunction($a)
    {
        global $pp;
        $p = $a + 2;
        $p = $p - $pp;

        echo $p;
    }
}

Greetings,

Putting aside an… interesting JIRA ticket (that misspells SonarQube and isn’t associated with an RSPEC), does this (already implemented) rule do what you need?

https://rules.sonarsource.com/php/RSPEC-836

Colin

Ah! Yeah, I wondered about the state of that ticket too…

Thanks for pointing me toward the existing rule; I had searched for just about every other synonym of “uninitialized” I could think of and came up dry, so I’m glad to see that it exists.

I actually started this search because of a pull request scan where SonarQube didn’t find a pretty obvious violation of this rule, so I guess my next steps will be doing some local debugging to figure out why.