Skip to content
Open
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
7 changes: 4 additions & 3 deletions src/Rules/Properties/ReadOnlyByPhpDocPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public function processNode(Node $node, Scope $scope): array
}

$inCloneWith = (bool) $propertyFetch->getAttribute('inCloneWith', false);
if ($inCloneWith) {
return [];
}

$inFunction = $scope->getFunction();
if (
Expand Down Expand Up @@ -97,6 +94,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ($inCloneWith) {
continue;
}

$scopeMethod = $scope->getFunction();
if (!$scopeMethod instanceof MethodReflection) {
throw new ShouldNotHappenException();
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/Properties/ReadOnlyPropertyAssignRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public function processNode(Node $node, Scope $scope): array
}

$inCloneWith = (bool) $propertyFetch->getAttribute('inCloneWith', false);
if ($inCloneWith) {
return [];
}

$errors = [];
$reflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($propertyFetch, $scope);
Expand Down Expand Up @@ -84,6 +81,10 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

if ($inCloneWith) {
continue;
}

$scopeMethod = $scope->getFunction();
if (!$scopeMethod instanceof MethodReflection) {
throw new ShouldNotHappenException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ public function testPropertyHooks(): void
#[RequiresPhp('>= 8.5')]
public function testCloneWith(): void
{
$this->analyse([__DIR__ . '/data/readonly-phpdoc-property-assign-clone-with.php'], []);
$this->analyse([__DIR__ . '/data/readonly-phpdoc-property-assign-clone-with.php'], [
[
'@readonly property ReadonlyPhpDocPropertyAssignCloneWith\Foo::$pub is assigned outside of its declaring class.',
29,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,27 @@ public function testBug12537(): void
#[RequiresPhp('>= 8.5')]
public function testCloneWith(): void
{
$this->analyse([__DIR__ . '/data/readonly-property-assign-clone-with.php'], []);
$this->analyse([__DIR__ . '/data/readonly-property-assign-clone-with.php'], [
[
'Readonly property ReadonlyPropertyAssignCloneWith\Foo::$pubSet is assigned outside of its declaring class.',
28,
],
[
'Readonly property ReadonlyPropertyAssignCloneWith\Bar::$value is assigned outside of its declaring class.',
47,
],
]);
}

#[RequiresPhp('>= 8.5')]
public function testBug14063(): void
{
$this->analyse([__DIR__ . '/data/bug-14063.php'], [
[
'Readonly property Bug14063\Obj::$value is assigned outside of its declaring class.',
18,
],
]);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-14063.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php // lint >= 8.5

declare(strict_types = 1);

namespace Bug14063;

final readonly class Obj
{
public function __construct(public string $value) {}

public function withValue(string $value): self
{
return clone($this, ['value' => $value]); // OK - inside declaring class
}
}

$obj = new Obj('val');
$newObj = clone($obj, ['value' => 'newVal']);
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ function (Foo $foo): void {
clone ($foo, [
'priv' => 1, // reported in AccessPropertiesInAssignRule
'prot' => 1, // reported in AccessPropertiesInAssignRule
'pub' => 1, // reported in AccessPropertiesInAssignRule
'pub' => 1, // reported here - @readonly property assigned outside declaring class
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ function (Foo $foo): void {
'priv' => 1, // reported in AccessPropertiesInAssignRule
'prot' => 1, // reported in AccessPropertiesInAssignRule
'pub' => 1, // reported in AccessPropertiesInAssignRule
'pubSet' => 1,
'pubSet' => 1, // reported here - readonly property assigned outside declaring class
]);
};

final readonly class Bar
{
public function __construct(public string $value) {}

public function withValue(string $value): self
{
return clone($this, ['value' => $value]); // OK - inside declaring class
}
}

function (Bar $bar): void {
clone ($bar, [
'value' => 'newVal', // reported in AccessPropertiesInAssignRule (protected(set)) - and also here
]);
};
Loading