From d6f53a7eb09fbe4c4963802b25b0670190ae9328 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:22:51 +0100 Subject: [PATCH] make tests work in PHP < 8.2 --- .../Expression/Call/ArgumentsAnalyzer.php | 12 ++- tests/ImmutableAnnotationTest.php | 95 ++++++++++--------- 2 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php index 97026ac25e2..003355bfcd4 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Call/ArgumentsAnalyzer.php @@ -2,6 +2,7 @@ namespace Psalm\Internal\Analyzer\Statements\Expression\Call; +use InvalidArgumentException; use PhpParser; use Psalm\CodeLocation; use Psalm\Codebase; @@ -1267,10 +1268,15 @@ private static function handleByRefReadonlyArg( $codebase = $statements_analyzer->getCodebase(); $declaring_property_class = (string) $codebase->properties->getDeclaringClassForProperty( $property_id, - false, # what does this do? @todo + true, $statements_analyzer, ); - $declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_property_class); + + try { + $declaring_class_storage = $codebase->classlike_storage_provider->get($declaring_property_class); + } catch (InvalidArgumentException $_) { + return; + } if (isset($declaring_class_storage->properties[$prop_name])) { $property_storage = $declaring_class_storage->properties[$prop_name]; @@ -1310,7 +1316,7 @@ private static function handleByRefFunctionArg( if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch && $arg->value->name instanceof PhpParser\Node\Identifier) { $prop_name = $arg->value->name->name; - if ($statements_analyzer->getFQCLN()) { + if (!empty($statements_analyzer->getFQCLN())) { $fq_class_name = $statements_analyzer->getFQCLN(); self::handleByRefReadonlyArg( diff --git a/tests/ImmutableAnnotationTest.php b/tests/ImmutableAnnotationTest.php index 56838b9411f..c9343dd1153 100644 --- a/tests/ImmutableAnnotationTest.php +++ b/tests/ImmutableAnnotationTest.php @@ -768,63 +768,72 @@ public function getShortMutating() : string { 'code' => 'values = $values; - } - - public function bar(): mixed - { - return reset($this->values); - } - }', + final class Foo + { + /** + * @readonly + */ + public array $values; + + public function __construct(array $values) + { + $this->values = $values; + } + + public function bar(): mixed + { + return reset($this->values); + } + }', 'error_message' => 'InaccessibleProperty', ], 'readonlyByRef' => [ 'code' => 'values = $values; - } - } + public function __construct(array $values) + { + $this->values = $values; + } + } - $x = new Foo([]); - reset($x->values);', + $x = new Foo([]); + reset($x->values);', 'error_message' => 'InaccessibleProperty', ], 'readonlyByRefCustomFunction' => [ 'code' => 'values = $values; - } - } - - /** - * @param string $a - * @param array $b - * @return void - */ - function bar($a, &$b) {} - - $x = new Foo([]); - bar("hello", $x->values);', + final class Foo + { + /** + * @readonly + */ + public array $values; + + public function __construct(array $values) + { + $this->values = $values; + } + } + + /** + * @param string $a + * @param array $b + * @return void + */ + function bar($a, &$b) {} + + $x = new Foo([]); + bar("hello", $x->values);', 'error_message' => 'InaccessibleProperty', ], 'preventUnset' => [