psalm
psalm copied to clipboard
Typed class properties must not be ignored when phpVersion="7.3"
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.
https://psalm.dev/r/6a40601023?phpVersion=7.3
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
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).
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....
That should result in a parse error or sth like InvalidTypedProperty error (naming is hard)
Updated example: https://psalm.dev/r/aabb5297e5?phpVersion=7.3
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!
It may be worth reporting that to https://github.com/nikic/PHP-Parser then. PHPStan has the same issue, it would solve both
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
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
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!