Psalm 7 release: PHP security&static analysis software

11 days ago (psalm.dev)

For anyone not familiar with PHP, static analysis is pretty much required because the official PHP type system is minimal. The detailed types are added through phpdoc comments, and they also help IDEs to provide a sane completion. For instance, the plain "array" should be detailed with phpdoc:

    /**
     * @param array<string, MyRecord> $input
     * @return list<int>
     */
    function foo(array $input): array
    {
        foreach ($input as $string => $record) {
            // IDE knows $record is an instance of MyRecord

The static analysis will use both type systems, and it's the only way to check that phpdoc are consistent. Psalm has various levels of strictness, and even at a very lax level, it can help detect errors or code smells.

There are two major static analysis tools for PHP, Psalm and PHPstan, both written in PHP. The latter used to be much slower, but last time I tried it there was little difference (about 20 s for 100k lines + dependencies).

My guess is that a new contender, Mago, written in Rust, will probably soon replace those other tools. Its commands (analyze, lint) are already almost as complete as psalm/phpstan. Though the migration won't be easy because it does not have a notion of levels.

  • PHP type system is lacking but I wouldn't call it minimal.

    PHP even has union types, just like TypeScript.

    The major issue is the lack of array types and it's 90% of the reason I see projects using extra linting tools.

    [1] https://php.watch/versions/8.0/union-types

    • Maybe "minimal" was not the right word. I hope the context made clear that I meant the type system was lacking even for basic uses. Here are 3 common cases.

      Some PHP functions (e.g. opening a file) return a `resource` type, but PHP has no official way to declare it. So the right way is to declare `mixed` as an official type (which means `any`) and fix that declaration with a phpdoc comment.

      Phpdoc provides generics (named "templates", à la C++), e.g. `iterable<Something>` where `iterable` is an official PHP type (which is awfully imprecise without phpdoc).

      Phpdoc has tuples and (associative) array shapes, e.g. `@return list{int, \Error}`.

      In past PHP versions, the type system was worse because the internal types were ambiguous: types were implicitly nullable, i.e. `string` meant `null|string`. When PHP gradually added sum types, they fixed that ambiguity.

      1 reply →

    • That's exactly it but PHP Core refuses to address the typed array issue in wait for a consensus on Generics even though those two things serve different audiences.

      I haven't actually run into a single issue with PHP's type system in the last few years. Literally the only thing that remains is typed arrays but that seems to be ways off due to fragmentation in how the core devs want it handled.

      7 replies →

    • > PHP type system is lacking but I wouldn't call it minimal.

      PHP still doesn't have generics, right? IMO any type system without higher-order types is definitely on the minimal side.

      3 replies →

Thou shall use the Rust rewrite of PHP.

  • ”Rewrite in Rust” fanboys are more annoying than crypto bros. How is such a feat even possible?

    • I dunno,unlike the crypto/NFT era, Rust rewrites have demonstrated measured performance and memory savings gains. Some grimace when they see "written in Rust" as a tagline but my immediate thought tends to land more on the "thank God this isn't an electron app" side of things

      2 replies →

    • Google funding, I suspect. One day they will turn off the tap and the mantra will be silenced.

      "Strange; seems like all I hear about these days are projects funded by the world's largest advertising corporation."

Ah boy o boy, PHP was the first lanuage I teached myself but man it still needs PHPDoc comment on return types?

Come on PHP has so many shitty things, but even something basic like this is still not in the lanuage itself?

  • PHP does have return type annotations. The reason the PHPDoc comments are needed here is that it lacks generics. Unfortunately, adding generics has proven problematic, but I still hope that we'll see them in the next few years.

  • It's had return type declarations since 7. There's even intersection and literal types now. You could have typed this into google and it would have told you

  • TFA even shows examples of native return types. How could you not see that?

    • The page itself shows plenty of examples were this is not true.

      Including generic type arrays which is not supported.