psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Typed class properties must not be ignored when phpVersion="7.3"

Open dsentker opened this issue 1 year ago • 11 comments

Given the simple class:

class Foo {
    private SomeClass $foo;
    // ...
}

As typed properties are introduced in PHP 7.4, I expect that psalm outputs any error when i use phpVersion=7.3 , but that is not the case.

As i cannot pass phpVersion on psalm.dev, i cannot create a reproducable online version.

dsentker avatar Apr 06 '23 16:04 dsentker

https://psalm.dev/r/6a40601023?phpVersion=7.3

orklah avatar Apr 06 '23 17:04 orklah

I found these snippets:

https://psalm.dev/r/6a40601023
<?php

class Foo {
    private int $foo;
    // ...
}
Psalm output (using commit 82b3a7c):

ERROR: MissingConstructor - 4:17 - Foo has an uninitialized property Foo::$foo, but no constructor

psalm-github-bot[bot] avatar Apr 06 '23 17:04 psalm-github-bot[bot]

This has nothing to do with a missing constructor, that was just an example. Just think of an existing constructor as well (or a public property).

dsentker avatar Apr 07 '23 13:04 dsentker

What would be the expected behaviour here? I think the php-parser should fail bulding the ast, since we have invalid syntax and psalm should report that as it does with any syntax problems....

ygottschalk avatar Apr 11 '23 14:04 ygottschalk

That should result in a parse error or sth like InvalidTypedProperty error (naming is hard)

dsentker avatar Apr 11 '23 18:04 dsentker

Updated example: https://psalm.dev/r/aabb5297e5?phpVersion=7.3

dsentker avatar Apr 11 '23 20:04 dsentker

I found these snippets:

https://psalm.dev/r/aabb5297e5
<?php

class Foo {
    public stdClass $foo;
    
    public function __construct() {
        $this->foo = new stdClass;
    }
}
Psalm output (using commit 71bb951):

No issues!

psalm-github-bot[bot] avatar Apr 11 '23 20:04 psalm-github-bot[bot]

It may be worth reporting that to https://github.com/nikic/PHP-Parser then. PHPStan has the same issue, it would solve both

orklah avatar Apr 12 '23 08:04 orklah

Generally PHP-Parser parses idealized PHP and doesn't get into version-dependent details. This is its both strong and weak point.

To make sure you have a syntactically valid PHP I'd recommend to complement Psalm with actual PHP lint. We ourselves do that: https://github.com/vimeo/psalm/blob/bf8e150f8ff4199171c1ea6ca88bbf1f27b14c66/.github/workflows/ci.yml#L50-L51

weirdan avatar Aug 17 '23 23:08 weirdan

Generally all of these changes for native types aren't reported - see changelog https://www.php.net/manual/en/language.types.declarations.php

As well as

  • duplicate types https://psalm.dev/r/cef7a48ca7
  • nullable null https://psalm.dev/r/65b61b9ee2
  • bool with false/true https://psalm.dev/r/0e4e72420b

All of these are a good first issue checking for $codebase->analysis_php_version_id if you want to give it a try

kkmuffme avatar Mar 22 '24 22:03 kkmuffme

I found these snippets:

https://psalm.dev/r/cef7a48ca7
<?php

class Foo {
    public function bar(): int|int { // duplicate types should give an error
        return 15;
    }
}
Psalm output (using commit ef3b018):

No issues!
https://psalm.dev/r/65b61b9ee2
<?php

class Foo {
    public function bar(): ?null { // ?null cannot be nullable
        return null;
    }
}
Psalm output (using commit ef3b018):

No issues!
https://psalm.dev/r/0e4e72420b
<?php

class Foo {
    public function bar(): bool|false { // bool and false (true) cannot be combined
        return rand(0, 1) > 0 ? false : true;
    }
}
Psalm output (using commit ef3b018):

No issues!

psalm-github-bot[bot] avatar Mar 22 '24 22:03 psalm-github-bot[bot]