Skip to content

Commit

Permalink
Merge pull request #10228 from ging-dev/fix
Browse files Browse the repository at this point in the history
fix: #10080
  • Loading branch information
orklah authored Oct 16, 2023
2 parents b432d81 + 0162e75 commit db82d26
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Psalm/Internal/Diff/ClassStatementsDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Psalm\Internal\Diff;

use PhpParser;
use UnexpectedValueException;

use function count;
use function get_class;
use function is_string;
use function strpos;
use function strtolower;
use function substr;
Expand Down Expand Up @@ -230,7 +232,19 @@ static function (
/** @var PhpParser\Node */
$affected_elem = $diff_elem->type === DiffElem::TYPE_REMOVE ? $diff_elem->old : $diff_elem->new;
if ($affected_elem instanceof PhpParser\Node\Stmt\ClassMethod) {
$add_or_delete[] = $name_lc . '::' . strtolower((string) $affected_elem->name);
$method_name = strtolower((string) $affected_elem->name);
$add_or_delete[] = $name_lc . '::' . $method_name;
if ($method_name === '__construct') {
foreach ($affected_elem->getParams() as $param) {
if (!$param->flags || !$param->var instanceof PhpParser\Node\Expr\Variable) {
continue;
}
if ($param->var instanceof PhpParser\Node\Expr\Error || !is_string($param->var->name)) {
throw new UnexpectedValueException('Not expecting param name to be non-string');
}
$add_or_delete[] = $name_lc . '::$' . $param->var->name;
}
}
} elseif ($affected_elem instanceof PhpParser\Node\Stmt\Property) {
foreach ($affected_elem->props as $prop) {
$add_or_delete[] = $name_lc . '::$' . $prop->name;
Expand Down
46 changes: 46 additions & 0 deletions tests/Cache/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,51 @@ public function foo($baz): void
],
],
];

yield 'constructorPropertyPromotionChange' => [
[
[
'files' => [
'/src/A.php' => <<<'PHP'
<?php
class A {
public function __construct(private string $foo)
{
}
public function bar(): string
{
return $this->foo;
}
}
PHP,
],
'issues' => [],
],
[
'files' => [
'/src/A.php' => <<<'PHP'
<?php
class A
{
public function __construct()
{
}
public function bar(): string
{
return $this->foo;
}
}
PHP,
],
'issues' => [
'/src/A.php' => [
"UndefinedThisPropertyFetch: Instance property A::\$foo is not defined",
"MixedReturnStatement: Could not infer a return type",
"MixedInferredReturnType: Could not verify return type 'string' for A::bar",
],
],
],
],
];
}
}

0 comments on commit db82d26

Please sign in to comment.