Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPStan throws undefined static method when using custom rules in Respect/Validation

I'm using Respect/Validation class and I have custom rule CustomRule() which works fine:

use Respect\Validation\Validator as v;

// ...

'email' => v::CustomRule()->email()->setName('email');

But this causes PHPStan to throw an error:

Call to an undefined static method Respect\Validation\Validator::CustomRule().

But if I move it after a built-in rule (e.g., email()), PHPStan works fine, no errors:

'email' => v::email()->CustomRule()->setName('email');

To be clear, both code works but PHPStan thinks the first code is invalid.

Any workaround so that PHPStan will accept it even if CustomRule() was set first?

Update:

I've found that if I edit the doc block of Respect\Validation\Validator class and append my custom rule to the list of its built-in rules, it works!

/**
* ...
* @method static Validator CustomRule()
*/
class Validator extends AllOf
...

Of course it's a bad idea to directly modify the doc block from the main class. That said, my question still remains the same. Or, maybe is there a way for PHPStan to honor my own doc block from my CustomRule class?

like image 499
IMB Avatar asked Oct 19 '25 14:10

IMB


1 Answers

Ran into the same issue. What I did was just to add the custom Validators to phpstan config file (phpstan.neon). It can be done with regex eg:

parameters:
    ignoreErrors:
        - '#Call to an undefined method Respect\\Validation\\Validator::[a-zA-Z0-9\\_]()#'

Docs: https://github.com/phpstan/phpstan#ignore-error-messages-with-regular-expressions

More of a workaround I guess though.

like image 167
echibi Avatar answered Oct 22 '25 03:10

echibi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!