Skip to content

@readonly property cannot be passed by-ref #3850

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

Merged
merged 3 commits into from
Mar 7, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Rules/FunctionCallParametersCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,25 @@ public function check(
if ($nativePropertyReflection === null) {
continue;
}
if (!$nativePropertyReflection->isReadOnly()) {
continue;
}

if ($nativePropertyReflection->isStatic()) {
$propertyDescription = sprintf('static readonly property %s::$%s', $propertyReflection->getDeclaringClass()->getDisplayName(), $propertyReflection->getName());
if ($nativePropertyReflection->isReadOnly()) {
if ($nativePropertyReflection->isStatic()) {
$errorFormat = 'static readonly property %s::$%s';
} else {
$errorFormat = 'readonly property %s::$%s';
}
} elseif ($nativePropertyReflection->isReadOnlyByPhpDoc()) {
if ($nativePropertyReflection->isStatic()) {
$errorFormat = 'static @readonly property %s::$%s';
} else {
$errorFormat = '@readonly property %s::$%s';
}
} else {
$propertyDescription = sprintf('readonly property %s::$%s', $propertyReflection->getDeclaringClass()->getDisplayName(), $propertyReflection->getName());
continue;
}

$propertyDescription = sprintf($errorFormat, $propertyReflection->getDeclaringClass()->getDisplayName(), $propertyReflection->getName());

$errors[] = RuleErrorBuilder::message(sprintf(
'%s is passed by reference so it does not accept %s.',
$this->describeParameter($parameter, $argumentName === null ? $i + 1 : null),
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1982,4 +1982,41 @@ public function testBug3107(): void
$this->analyse([__DIR__ . '/data/bug-3107.php'], []);
}

public function testBug12676(): void
{
$errors = [
[
'Parameter #1 $array is passed by reference so it does not accept @readonly property Bug12676\A::$a.',
15,
],
[
'Parameter #1 $array is passed by reference so it does not accept @readonly property Bug12676\B::$readonlyArr.',
25,
],
[
'Parameter #1 $array is passed by reference so it does not accept static @readonly property Bug12676\C::$readonlyArr.',
35,
],
];

if (PHP_VERSION_ID < 80000) {
$errors = [
[
'Parameter #1 $array_arg is passed by reference so it does not accept @readonly property Bug12676\A::$a.',
15,
],
[
'Parameter #1 $array_arg is passed by reference so it does not accept @readonly property Bug12676\B::$readonlyArr.',
25,
],
[
'Parameter #1 $array_arg is passed by reference so it does not accept static @readonly property Bug12676\C::$readonlyArr.',
35,
],
];
}

$this->analyse([__DIR__ . '/data/bug-12676.php'], $errors);
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-12676.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug12676;

/**
* @immutable
*/
class A {

/** @var array<string, int> */
public array $a;

public function __construct() {
$this->a = ['b' => 2, 'a' => 1];
ksort($this->a);
}
}

class B {
/** @readonly */
public array $readonlyArr;

public function __construct() {
$this->readonlyArr = ['b' => 2, 'a' => 1];
ksort($this->readonlyArr);
}
}

class C {
/** @readonly */
static public array $readonlyArr;

public function __construct() {
self::$readonlyArr = ['b' => 2, 'a' => 1];
ksort(self::$readonlyArr);
}
}
Loading