Skip to content

Commit

Permalink
make tests work in PHP < 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Dec 19, 2023
1 parent 1ff8518 commit 9e463bb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Psalm\Internal\Analyzer\Statements\Expression\Call;

use InvalidArgumentException;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public function setProperties(array $properties): self
if ($cloned->is_list) {
$last_k = -1;
$had_possibly_undefined = false;

/** @psalm-suppress InaccessibleProperty */
ksort($cloned->properties);
foreach ($cloned->properties as $k => $v) {
if (is_string($k) || $last_k !== ($k-1) || ($had_possibly_undefined && !$v->possibly_undefined)) {
Expand Down
98 changes: 55 additions & 43 deletions tests/ImmutableAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,63 +768,75 @@ public function getShortMutating() : string {
'code' => '<?php
namespace World;
final readonly class Foo
{
public array $values;
public function __construct(array $values)
{
$this->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;
}
/**
* @return mixed
*/
public function bar()
{
return reset($this->values);
}
}',
'error_message' => 'InaccessibleProperty',
],
'readonlyByRef' => [
'code' => '<?php
namespace World;
final readonly class Foo
{
public array $values;
final class Foo
{
/**
* @readonly
*/
public array $values;
public function __construct(array $values)
{
$this->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' => '<?php
namespace World;
final readonly class Foo
{
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);',
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' => [
Expand Down

0 comments on commit 9e463bb

Please sign in to comment.