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 when using readonly property in by-ref arg #10505

Merged
Merged
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 @@ -2,11 +2,13 @@

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

use InvalidArgumentException;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Internal\Analyzer\AttributesAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\InstancePropertyAssignmentAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\AssignmentAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
Expand Down Expand Up @@ -45,6 +47,7 @@
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
Expand Down Expand Up @@ -1253,6 +1256,42 @@ private static function evaluateArbitraryParam(
return null;
}

private static function handleByRefReadonlyArg(
StatementsAnalyzer $statements_analyzer,
Context $context,
PhpParser\Node\Expr\PropertyFetch $stmt,
string $fq_class_name,
string $prop_name
): void {
$property_id = $fq_class_name . '::$' . $prop_name;

$codebase = $statements_analyzer->getCodebase();
$declaring_property_class = (string) $codebase->properties->getDeclaringClassForProperty(
$property_id,
true,
$statements_analyzer,
);

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];

InstancePropertyAssignmentAnalyzer::trackPropertyImpurity(
$statements_analyzer,
$stmt,
$property_id,
$property_storage,
$declaring_class_storage,
$context,
);
}
}

/**
* @return false|null
*/
Expand All @@ -1274,6 +1313,46 @@ private static function handleByRefFunctionArg(
'reset', 'end', 'next', 'prev', 'array_pop', 'array_shift',
];

if ($arg->value instanceof PhpParser\Node\Expr\PropertyFetch
&& $arg->value->name instanceof PhpParser\Node\Identifier) {
$prop_name = $arg->value->name->name;
if (!empty($statements_analyzer->getFQCLN())) {
$fq_class_name = $statements_analyzer->getFQCLN();

self::handleByRefReadonlyArg(
$statements_analyzer,
$context,
$arg->value,
$fq_class_name,
$prop_name,
);
} else {
// @todo atm only works for simple fetch, $a->foo, not $a->foo->bar
// I guess there's a function to do this, but I couldn't locate it
$var_id = ExpressionIdentifier::getVarId(
$arg->value->var,
$statements_analyzer->getFQCLN(),
$statements_analyzer,
);

if ($var_id && isset($context->vars_in_scope[$var_id])) {
foreach ($context->vars_in_scope[$var_id]->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof TNamedObject) {
$fq_class_name = $atomic_type->value;

self::handleByRefReadonlyArg(
$statements_analyzer,
$context,
$arg->value,
$fq_class_name,
$prop_name,
);
}
}
}
}
}

if (($var_id && isset($context->vars_in_scope[$var_id]))
|| ($method_id
&& in_array(
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
75 changes: 75 additions & 0 deletions tests/ImmutableAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,81 @@ public function getShortMutating() : string {
}',
'error_message' => 'ImpurePropertyAssignment',
],
'readonlyByRefInClass' => [
'code' => '<?php
namespace World;

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 class Foo
{
/**
* @readonly
*/
public array $values;

public function __construct(array $values)
{
$this->values = $values;
}
}

$x = new Foo([]);
reset($x->values);',
'error_message' => 'InaccessibleProperty',
],
'readonlyByRefCustomFunction' => [
'code' => '<?php
namespace World;

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' => [
'code' => '<?php
/**
Expand Down