| 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/ |
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.