kahlan icon indicating copy to clipboard operation
kahlan copied to clipboard

PHPStan configuration

Open ezzatron opened this issue 8 years ago • 7 comments

It would be nice to have a drop-in config for Kahlan for PHPStan. Currently, when run over a Kahlan project, PHPStan warns about:

  • The DSL functions not existing.
  • The $this keyword used in kahlan-config.php outside of a class context.

Here's my current config for handling the first point:

parameters:
  ignoreErrors:
    - /^Function context not found.$/
    - /^Function describe not found.$/
    - /^Function expect not found.$/
    - /^Function it not found.$/

But there's probably a better way to fix the problem that doesn't involve "ignoring" these errors. If PHPStan can be configured to understand the functions it could actually pick up on real errors when using them. Are the DSL functions dynamically defined? That would explain why PHPStan complains.

PHPStan uses Neon config files which allow you to include a definition from a vendor, which means that if you created a kahlan/phpstan-kahlan repo, users could require it, then add something like this to their PHPStan configuration:

includes:
  - vendor/kahlan/phpstan-kahlan/kahlan.neon

I just wrote a PHPStan integration for Phony, which is probably more complex than what's needed for Kahlan, but you can check it out here anyway.

ezzatron avatar Nov 16 '17 01:11 ezzatron

Would definitely be a must have :+1: Since the DSL functions can't be loaded through the composer files entry (to avoid collision), they are dynamically created right there and unfortunately there's nothing we can do about that.

jails avatar Nov 17 '17 21:11 jails

@ondrejmirtes Do you have any advice for handling conditionally defined DSL functions in PHPStan? It's a pretty common scenario for describe/it style test runners and matcher libraries like Hamcrest.

ezzatron avatar Nov 18 '17 02:11 ezzatron

@ezzatron Hi, you can take advantage of the fact that PHPStan environment should match the environment of your application - including definitions of class aliases, constants, autoloaders and defined functions 😊 So you can use the bootstrap key in phpstan.neon to provide a file that will load everything necessary for you. Looks like it should be safe to require_once this Kahlan.php file in the bootstrap.

ondrejmirtes avatar Nov 18 '17 03:11 ondrejmirtes

Hello @ezzatron, were you able to find some compatible configuration with Kahlan on this topic ?

jails avatar Mar 10 '18 00:03 jails

Hi @jails, no, I'm sorry, but I haven't had the time to do much of anything PHP-related lately. From the sounds of it though, the bootstrap option mentioned above might solve at least the issue that PHPStan has with Kahlan's DSL functions.

ezzatron avatar Mar 11 '18 11:03 ezzatron

How about run phpstan to src folder for start ?

samsonasik avatar Mar 11 '18 11:03 samsonasik

@samsonasik This issue is about creating a shared config for projects that use Kahlan, not about running PHPStan over Kahlan's own codebase.

ezzatron avatar Mar 11 '18 11:03 ezzatron