4 ms·
Adding a static analyzer (like Psalm [1]) to the mixture makes PHP's types much more powerful. Psalm can use docblock annotations to attach more complex types t
by duskwuff 6y ago
Adding a static analyzer (like Psalm [1]) to the mixture makes PHP's types much more powerful. Psalm can use docblock annotations to attach more complex types to functions and variables, including:
* A "list" type representing a non-associative array
* Types representing specific kinds of values, like numeric-string or non-empty-array, as well as exact values as types (like true and false, or string constants).
* Typing on keys and values in arrays, e.g. array<class-string,int> for an array with class names as keys and integers as values
* Complex typing on the contents of associative arrays, e.g. array{foo:int, bar:string}
* Templated types on functions, like a function which returns an instance of a class whose name is passed in as a string:
/**
* @template T
* @param T::class $class_name
* @return T
*/
[1]: https://psalm.dev/ https://psalm.dev/
- mhitza 6y agoPsalm is nice, however I would suggest PHPStan as an alternative for people that encounter a lot of friction with Psalm. For example, in a project using doctrine I have to add a bunch of is null/@psal-mutation-free annotations. As all the methods can return null. However, if Psalm had support for something like phantom types, maybe I could tag entities returned from the database for whom certain fields are guaranteed to have values.
- slifin 6y agoAdding Phan to this conversation initially created by the creator of a PHP, now in the hands of TysonAndre who does an insane amount of good work on it
- noir_lord 6y agoFor users of the intellij family (idea or phpstorm). https://plugins.jetbrains.com/plugin/10215-php-inspections-ea-ultimate- https://plugins.jetbrains.com/plugin/10215-php-inspections-e... is amazing and well worth the price. It has a free variant which is still amazing but lacks all the inspections of the paid variant. https://plugins.jetbrains.com/plugin/7622-php-inspections-ea-extended- https://plugins.jetbrains.com/plugin/7622-php-inspections-ea...
- muglug 6y agoPsalm has levels – higher levels show fewer warnings (just like in PHPStan). Maybe the `@psalm-ignore-nullable-return` annotation is more appropriate for those methods? Do you use the Doctrine plugin?
- mhitza 6y agoI set these kinds of tools up to the highest level possible. In my opinion that is where you get the highest value from them. I was not aware of the any psalm doctrine plugin and will definitely have a look at it. Thanks for the suggestion. In terms of @psalm-ignore-nullable-return annotation. That definitely sounds like a practical workaround, at the moment.