Support advanced PHPStan data types (general, arrays)

Created on 9 December 2021, about 3 years ago
Updated 1 August 2023, over 1 year ago

Problem/Motivation

Allow advanced type styles as implemented by PHPStan and Psalm, and recognised by PHPStorm.

For example array shapes:

array{entity_type_id:string,langcode:string}

array<string, string>

array<string, SomethingMoreComplex>

Right now to use these its a frustrating dance of ignores and in some circumstances wholesale disable/modification of sniffs.

Pull request: https://github.com/pfrenssen/coder/pull/158

โœจ Feature request
Status

Fixed

Version

3.0

Component

Coder Sniffer

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States dww

    Coming from ๐Ÿ“Œ [PP-1] Support the recommended phpdoc syntax for documenting Iterator return types Closed: duplicate which I had opened before finding this issue (which would solve a superset of the problem I was worried about).

    Looks like this is now being handled at https://github.com/pfrenssen/coder/pull/158

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    Thanks for all the work on the pull request!

    The problem I see here is that you are trying support all the possible magic type strings from PHPStan all at once. That makes the pull request big and full of contradictions. Instead, I think we need to split this into smaller chunks and work on this iteratively. Support the most basic array shapes first and then go from there. I think the test cases are very useful, thanks for that.

    One consequence of this change is that we will have to remove the check for equality of the data types with type hints from Coder. That is a bit of an unfortunate regression, but with all the possibilities in PHPStan Coder will not be able to keep up. For example the doc data type "sstring" (typo double s) with type hint "string" will not throw an error anymore. I think that is acceptable if we make life for PHPStan users easier as tradeoff.

    I will check if I can get simple array shapes working now.

    • klausi โ†’ authored 95052fc6 on 8.3.x
      feat(FunctionComment): Allow PHPStan basic data types in doc comments (#...
  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    Before starting with array shapes I found that we do not support all basic types by PHPStan, pushed a change for that with test cases. https://github.com/pfrenssen/coder/pull/191

    Next on the PHPStan page is Integer ranges, but I will do General arrays first as it seems more important for people to use.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    At what point do we allow any arbitrary data to be before the dollar-sign, or any data until a new line (in the case of returns)?

    Or should we even be validating this?

    What are other linting projects doing in this regard? Seems like it should be a solved problem, or one we can ignore.

    • klausi โ†’ authored 4ee6a357 on 8.3.x
      feat(FunctionComment): Allow PHPStan array shapes in doc data types (#...
  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    I don't want to allow arbitrary characters before the dollar sign yet, because that would hurt new contributors to Drupal. They make lots of tiny data type mistakes that Coder detects right now. Coder is a big help for them learning to write proper doc data types.

    Now also pushed support for PHPStan general array notation.

    Next: supporting complex array shapes of phpstan. Then I think we can call this issue fixed and follow-up with support for more PHPstan data types. We can then also discuss again if we should simply drop any data type validation in Coder.

    I agree that it makes sense to research what PHPCS upstream is validating for doc data types, maybe we can use similar approaches.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    Another thought: now that we have PHPStan becoming increasingly tied in, make this a responsibility for static analysis to enforce. If static analysis doesn't recognize types, then rely on those tools for errors.

    I think it makes sense to defer responsibilities previously held by linters, onto static analysis.

  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    +1 for #17: donโ€™t try overcharging a linter with static anaysis; the best youโ€™ll get is a paler dupeโ€ฆ

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    Fully agreed with you both - Coder cannot and will not do static analysis. We still need to do Coding standards checks on data types, for example that developers write "int" instead of "integer". That is the job of a linter.

    That means we cannot drop data type validation completely from Coder. Instead we will adapt Coder to not contradict PHPStan and limit the validation to very basic things.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    for example that developers write "int" instead of "integer". That is the job of a linter.

    Detecting obvious typos, or detecting and being able to fix (cbf) unapproved variants im totally in favor of.

  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    And pushed advanced array shape support tests (it already worked), plus integer range support.

    Closing this issue is fixed now, but we are not done with all the possible things at https://phpstan.org/writing-php-code/phpdoc-types . So some further relaxing of validation will be necessary, please open new issues as you see fit.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    We were previously running the MR as a patch and have updated to 8.3.18 but am now getting unexpected errors, it seems that commas are not accepted?

    22 | ERROR | [x] Expected "array<intarray<intint>>" but found
        |       |     "array<int,array<int,int>>" for @var tag in member
        |       |     variable comment
    
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    Looks like @var docs aren't handled.

  • ๐Ÿ‡ฆ๐Ÿ‡นAustria klausi ๐Ÿ‡ฆ๐Ÿ‡น Vienna

    Thanks for reporting! Created โœจ Allow arbitrary data types for PHPStan compatibility in @var class properties Fixed as follow-up for that.

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly mondrake ๐Ÿ‡ฎ๐Ÿ‡น

    It looks like #24 addressed @var annotations, but (at least) not @return ones: https://www.drupal.org/pift-ci-job/2730350 โ†’ , ๐Ÿ“Œ Refactor transactions Fixed

    Filed โœจ Allow arbitrary data types for PHPStan compatibility in @return class methods Active

Production build 0.71.5 2024