1

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?

IMB
  • 12,083
  • 16
  • 59
  • 118

1 Answers1

0

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.

echibi
  • 1