mglaman / phpstan-drupal

Extension for PHPStan to allow analysis of Drupal code.
https://phpstan-drupal.mglaman.dev/
MIT License
192 stars 76 forks source link

Detect when Form API #ajax callback is invoked statically but the method is not static #213

Open mglaman opened 2 years ago

mglaman commented 2 years ago

In PHP 7 calling a non-static method statically only threw a notice or warning. In PHP 8 it will cause EXPLOSIONS 🤯 .

Example code:

class MyForm extends BaseForm {

    public function buildForm() {
        $form['foo']['#ajax']['callback'] = [static::class, 'ajaxCallback'];
    }

    // This will error
    public function ajaxCallback() {

    }

    // This will not.
    public static function ajaxCallback() {

    }

}

Since the callback is nested in a Form API structure, PHPStan will not detect that we're eventually going to call this method. So we need a custom rule to make sure the AJAX callback is static if invoked statically.

mglaman commented 2 years ago

For this we just need to read #ajax.callback and check if isCallable. That will find out if it has been statically called or not once https://github.com/phpstan/phpstan/issues/5782 lands.