3 comments

  • idoubtit1 hour ago
    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 &quot;array&quot; should be detailed with phpdoc:<p><pre><code> &#x2F;** * @param array&lt;string, MyRecord&gt; $input * @return list&lt;int&gt; *&#x2F; function foo(array $input): array { foreach ($input as $string =&gt; $record) { &#x2F;&#x2F; IDE knows $record is an instance of MyRecord </code></pre> The static analysis will use both type systems, and it&#x27;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.<p>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).<p>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&#x2F;phpstan. Though the migration won&#x27;t be easy because it does not have a notion of levels.
    • bel81 hour ago
      PHP type system is lacking but I wouldn&#x27;t call it minimal.<p>PHP even has union types, just like TypeScript.<p>The major issue is the lack of array types and it&#x27;s 90% of the reason I see projects using extra linting tools.<p>[1] <a href="https:&#x2F;&#x2F;php.watch&#x2F;versions&#x2F;8.0&#x2F;union-types" rel="nofollow">https:&#x2F;&#x2F;php.watch&#x2F;versions&#x2F;8.0&#x2F;union-types</a>
      • wesammikhail1 hour ago
        That&#x27;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.<p>I haven&#x27;t actually run into a single issue with PHP&#x27;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.
        • hparadiz53 minutes ago
          With how good PHPDoc and Phan are these days I don&#x27;t really think about it anymore. You can sort of have typed array by implementing Iterator, Countable, ArrayAccess, and optionally Indexing but I assume you mean defining types of array values.
  • anthk3 hours ago
    Thou shall use the Rust rewrite of PHP.
    • Y-bar2 hours ago
      ”Rewrite in Rust” fanboys are more annoying than crypto bros. How is such a feat even possible?
      • wesammikhail1 hour ago
        inb4 &quot;Rewrite bitcoin in Rust&quot; comments stars pouring in
  • origini3 hours ago
    Ah boy o boy, PHP was the first lanuage I teached myself but man it still needs PHPDoc comment on return types?<p>Come on PHP has so many shitty things, but even something basic like this is still not in the lanuage itself?
    • monster_truck3 hours ago
      It&#x27;s had return type declarations since 7. There&#x27;s even intersection and literal types now. You could have typed this into google and it would have told you
      • bakugo2 hours ago
        Type declarations are still quite limited, though. For example, you can declare that a function returns an array or a string, but not an array of strings.
        • lofaszvanitt1 hour ago
          Use a class if you need them.
          • bakugo1 hour ago
            How does that solve the problem? You can&#x27;t declare an array of strings inside a class, either.
            • lofaszvanitt43 minutes ago
              Do some research :D.
              • origini10 minutes ago
                Just to be clear, you suggest to wrap a typed array into its own class? is that the suggestion you think of?<p>Because if yes, thats just ugly and a hack&#x2F;workaround.
      • origini2 hours ago
        I did and it told me that it doesn&#x27;t support typed arrays.
    • Y-bar3 hours ago
      TFA even shows examples of native return types. How could you not see that?
      • origini2 hours ago
        The page itself shows plenty of examples were this is not true.<p>Including generic type arrays which is not supported.
    • spiderfarmer2 hours ago
      &quot;Cool! A PHP article on HN, let&#x27;s rehash some of my comments that got lots of upvotes 11 years ago!&quot;
      • origini2 hours ago
        Yepp, especially because not much changed and other languages did quite a lot. Strong contrast don&#x27;t you think?
        • wesammikhail1 hour ago
          Hmmm not much changed you say... Lets take a look at <a href="https:&#x2F;&#x2F;wiki.php.net&#x2F;rfc" rel="nofollow">https:&#x2F;&#x2F;wiki.php.net&#x2F;rfc</a><p>ooh wait, you couldn&#x27;t be more wrong. Pikachuface.png
          • origini1 hour ago
            Core issues have not been fixed.<p>You remember when HHVM &#x2F; hack came out from Facebook? Besides the fact that it was quite a dick move from Facebook to bring it out and then stop supporting it, it pushed PHP to take performance temporarily serious and there was so much air in it.<p>But the standard library is still inconsistent as hell, type system is still garbage especially mixxing strict and non strict.<p>The concurrency implementation? Has to be fixed again by frameworks.<p>And yeah i mean the critisism of my original comment stands as well and thats just an eye sore.
    • throw12345678912 hours ago
      &quot;Taught&quot;, not &quot;teached&quot;.