Skip to content

Commit d23a555

Browse files
committed
Fix unsetting array item triggers unset.possiblyHookedProperty
1 parent 274e766 commit d23a555

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/Rules/Variables/UnsetRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
4848
return $errors;
4949
}
5050

51-
private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
51+
private function canBeUnset(Node $node, Scope $scope, bool $inArrayDimFetch = false): ?IdentifierRuleError
5252
{
5353
if ($node instanceof Node\Expr\Variable && is_string($node->name)) {
5454
$hasVariable = $scope->hasVariableType($node->name);
@@ -77,7 +77,7 @@ private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
7777
->build();
7878
}
7979

80-
return $this->canBeUnset($node->var, $scope);
80+
return $this->canBeUnset($node->var, $scope, true);
8181
} elseif (
8282
$node instanceof Node\Expr\PropertyFetch
8383
&& $node->name instanceof Node\Identifier
@@ -117,7 +117,7 @@ private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
117117
->line($node->getStartLine())
118118
->identifier('unset.hookedProperty')
119119
->build();
120-
} elseif ($this->phpVersion->supportsPropertyHooks()) {
120+
} elseif ($this->phpVersion->supportsPropertyHooks() && !$inArrayDimFetch) {
121121
if (
122122
!$propertyReflection->isPrivate()
123123
&& !$propertyReflection->isFinal()->yes()

tests/PHPStan/Rules/Variables/UnsetRuleTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,7 @@ public function testBug2752(): void
6161

6262
public function testBug4289(): void
6363
{
64-
$errors = [];
65-
66-
if (PHP_VERSION_ID >= 80400) {
67-
$errors = [
68-
[
69-
'Cannot unset property Bug4289\BaseClass::$fields because it might have hooks in a subclass.',
70-
25,
71-
],
72-
];
73-
}
74-
75-
$this->analyse([__DIR__ . '/data/bug-4289.php'], $errors);
64+
$this->analyse([__DIR__ . '/data/bug-4289.php'], []);
7665
}
7766

7867
public function testBug5223(): void

tests/PHPStan/Rules/Variables/data/unset-hooked-property.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,41 @@ function doFoo() {
6464
unset($this->privateProperty);
6565
}
6666
}
67+
68+
class Bug12695
69+
{
70+
/** @var int[] */
71+
public array $values = [1];
72+
public function test(): void
73+
{
74+
unset($this->values[0]);
75+
}
76+
}
77+
78+
abstract class Bug12695_AbstractJsonView
79+
{
80+
protected array $variables = [];
81+
82+
public function render(): array
83+
{
84+
return $this->variables;
85+
}
86+
}
87+
88+
class Bug12695_GetSeminarDateJsonView extends Bug12695_AbstractJsonView
89+
{
90+
public function render(): array
91+
{
92+
unset($this->variables['settings']);
93+
return parent::render();
94+
}
95+
}
96+
97+
class Bug12695_AddBookingsJsonView extends Bug12695_GetSeminarDateJsonView
98+
{
99+
public function render(): array
100+
{
101+
unset($this->variables['seminarDate']);
102+
return parent::render();
103+
}
104+
}

0 commit comments

Comments
 (0)