phpstan-exception-rules icon indicating copy to clipboard operation
phpstan-exception-rules copied to clipboard

Warn about misconfiguration

Open Stadly opened this issue 7 years ago • 3 comments

It would be easier to configure PHPStan exception rules correctly if warnings were emitted for potential misconfigurantions. Consider the following configuration file:

parameters:
    exceptionRules:
        checkedExceptions: # or uncheckedExceptions
            - FooException
        methodThrowTypeDeclarations:
            Bar:
                barMethod:
                    - BarException
        functionThrowTypeDeclarations:
            bazFunction:
                - BazException

includes:
    - vendor/pepakriz/phpstan-exception-rules/extension.neon

A warning should be shown when

  • FooException does not exist.
  • FooException does not implement Throwable.
  • Bar does not exist.
  • Bar::barMethod does not exist.
  • BarException does not exist.
  • BarException does not implement Throwable.
  • bazFunction does not exist.
  • BazException does not exist.
  • BazException does not implement Throwable.

Stadly avatar Oct 03 '18 07:10 Stadly

Your suggestion leads to make a CompilerExtension https://doc.nette.org/en/2.4/di-extensions which can validate the configuration before DI compilation. But it is not a common solution in phpstan ecosystem. I'd like to know current opinion of @ondrejMirtes, because I've talked with him about this some time ago and he didn't like the idea to depend on concrete DI implementation (Nette DI).

pepakriz avatar Oct 03 '18 17:10 pepakriz

See: https://github.com/phpstan/phpstan/issues/1459

BTW feel free to write your own validation. I just think that CompilerExtension is not especially suitable because you wouldn't have available all the functionality PHPStan can offer (like the Broker instance) during DIC compilation.

ondrejmirtes avatar Oct 08 '18 13:10 ondrejmirtes

I think the best hack how to solve this is to define a service that accepts %exceptionRules% and does just this validation. I'm not sure how to make it instantiated every time though.

ondrejmirtes avatar May 20 '19 13:05 ondrejmirtes