Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error for promoted property usage pre-PHP 8 #10813

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Psalm\Issue\DuplicateMethod;
use Psalm\Issue\DuplicateParam;
use Psalm\Issue\InvalidDocblock;
use Psalm\Issue\InvalidPropertyAssignment;
use Psalm\Issue\MissingDocblockType;
use Psalm\Issue\ParseError;
use Psalm\Issue\PrivateFinalMethod;
Expand Down Expand Up @@ -626,6 +627,18 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
}
}

if ($this->codebase->analysis_php_version_id < 8_00_00) {
if (IssueBuffer::accepts(
new InvalidPropertyAssignment(
'Promoted properties are only supported in PHP 8 or newer,'
. ' but promoted property ' . $param_storage->name . ' found',
new CodeLocation($this->file_scanner, $param, null, true),
),
)) {
return false;
}
}

//no docblock type was provided for param but we have one for property
if ($var_comment_type) {
$param_storage->type = $var_comment_type;
Expand Down
24 changes: 24 additions & 0 deletions tests/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,18 @@ function foo() {
'MixedPropertyFetch',
],
],
'promotedPropertyOldPhp' => [
'code' => '<?php
class User
{
public function __construct(
protected string $name,
){}
}',
'assertions' => [],
'ignored_issues' => [],
'php_version' => '8.0',
],
'sharedPropertyInIf' => [
'code' => '<?php
class A {
Expand Down Expand Up @@ -2957,6 +2969,18 @@ public function barBar(): void
$a->foo = "bar";',
'error_message' => 'InvalidPropertyAssignment',
],
'promotedPropertyOldPhp' => [
'code' => '<?php
class User
{
public function __construct(
protected string $name,
){}
}',
'error_message' => 'InvalidPropertyAssignment',
'ignored_issues' => [],
'php_version' => '7.4',
],
'badFetch' => [
'code' => '<?php
$a = "hello";
Expand Down