Skip to content

Commit

Permalink
Fix #4386 - fix issues with property promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Oct 21, 2020
1 parent fd43ba7 commit ad5a8c2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Psalm/Internal/Codebase/ClassLikes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,7 @@ private function checkMethodReferences(ClassLikeStorage $classlike_storage, Meth
$offset
)
&& $param_storage->location
&& !$param_storage->promoted_property
) {
if ($method_storage->final) {
if (IssueBuffer::accepts(
Expand Down
14 changes: 14 additions & 0 deletions src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2399,6 +2399,19 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, bool $f
continue;
}

if (isset($class_storage->properties[$param_storage->name]) && $param_storage->location) {
IssueBuffer::add(
new \Psalm\Issue\ParseError(
'Promoted propertty ' . $param_storage->name . ' clashes with an existing property',
$param_storage->location
)
);

$storage->has_visitor_issues = true;
$this->file_storage->has_visitor_issues = true;
continue;
}

$property_storage = $class_storage->properties[$param_storage->name] = new PropertyStorage();
$property_storage->is_static = false;
$property_storage->type = $param_storage->type;
Expand All @@ -2408,6 +2421,7 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, bool $f
$property_storage->location = $param_storage->location;
$property_storage->stmt_location = new CodeLocation($this->file_scanner, $param);
$property_storage->has_default = $param->default ? true : false;
$param_storage->promoted_property = true;

$property_id = $fq_classlike_name . '::$' . $param_storage->name;

Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Storage/FunctionLikeParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class FunctionLikeParameter
*/
public $expect_variable = false;

/**
* @var bool
*/
public $promoted_property = false;

public function __construct(
string $name,
bool $by_ref,
Expand Down
10 changes: 10 additions & 0 deletions tests/UnusedCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,16 @@ public function bar(array $map) : void {
(new References)->bar(["a" => "b"]);'
],
'promotedPropertyIsUsed' => [
'<?php
class Test {
public function __construct(public int $id, public string $name) {}
}
$test = new Test(1, "ame");
echo $test->id;
echo $test->name;'
],
];
}

Expand Down

0 comments on commit ad5a8c2

Please sign in to comment.