Skip to content

Commit 45928f4

Browse files
committed
Keep protected properties of a final class across calls like private ones
No subclass reaches a protected property of a final class, so only the class's own code writes it - and the parent's code, when the parent declares the property too, which the resolver checks by reflection. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
1 parent 428ff02 commit 45928f4

6 files changed

Lines changed: 156 additions & 5 deletions

File tree

‎src/Analyser/PreservedProperties/PreservedProperties.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ final class PreservedProperties
2323
* and does not write otherwise
2424
* @param array<string, true> $closureWrittenPropertyNames written by the class's closures, which whoever
2525
* holds one can run at any time, so the walk of a method does not tell their type after it
26+
* @param array<string, true> $unlessParentDeclares the ones among $propertyNames kept only when the parent class
27+
* does not declare them: protected properties of a final class, which the parent's code can write otherwise
2628
* @param array<string, array<string, array{list<string>, list<array{list<non-empty-string>, int, string}>}|null>> $overrideWrittenPropertyNames
2729
* the private properties each non-private method of the class writes, by lowercase name - the parent's code can
2830
* run the ones that override its methods whenever it runs - each with the methods it runs that assign it and the
@@ -35,6 +37,7 @@ public function __construct(
3537
private array $writtenFunctionArguments,
3638
private array $closureWrittenPropertyNames,
3739
private ?string $parentClassName = null,
40+
private array $unlessParentDeclares = [],
3841
private array $overrideWrittenPropertyNames = [],
3942
)
4043
{
@@ -85,6 +88,14 @@ public function getParentClassName(): ?string
8588
return $this->parentClassName;
8689
}
8790

91+
/**
92+
* @return array<string, true>
93+
*/
94+
public function getUnlessParentDeclares(): array
95+
{
96+
return $this->unlessParentDeclares;
97+
}
98+
8899
/**
89100
* @return array<string, array<string, array{list<string>, list<array{list<non-empty-string>, int, string}>}|null>>
90101
*/

‎src/Analyser/PreservedProperties/PreservedPropertiesResolver.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ public function resolve(Expr $call): ?array
264264
}
265265

266266
$propertyNames = $preservedProperties->getPropertyNames();
267+
$unlessParentDeclares = $preservedProperties->getUnlessParentDeclares();
268+
if (count($unlessParentDeclares) > 0) {
269+
$parentClassName = $preservedProperties->getParentClassName();
270+
$parentClassReflection = $parentClassName !== null && $this->reflectionProvider->hasClass($parentClassName)
271+
? $this->reflectionProvider->getClass($parentClassName)
272+
: null;
273+
foreach (array_keys($unlessParentDeclares) as $propertyName) {
274+
if ($parentClassReflection !== null && !$parentClassReflection->hasNativeProperty($propertyName)) {
275+
continue;
276+
}
277+
unset($propertyNames[$propertyName]);
278+
}
279+
}
267280
foreach ($preservedProperties->getFunctionArguments() as [$functionNames, $position, $propertyName]) {
268281
if (!isset($propertyNames[$propertyName]) || $this->isFunctionArgumentByValue($functionNames, $position)) {
269282
continue;

‎src/Parser/PreservedPropertiesClassScan.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ final class PreservedPropertiesClassScan
4646
/** @var array<string, bool> declared instance properties, each with whether only the class's own code can write it */
4747
public array $properties = [];
4848

49+
/** @var array<string, true> protected properties of a final class with a parent, which only the class's own code writes unless the parent declares them too */
50+
private array $unlessParentDeclares = [];
51+
4952
/** @var array<string, true> properties written in ways no walk sees the value of: not by assigning it, or in closures */
5053
public array $nonPlainWrites = [];
5154

@@ -103,6 +106,17 @@ public function __construct(
103106
{
104107
}
105108

109+
public function addProperty(string $propertyName, bool $isPrivate, bool $isProtected): void
110+
{
111+
// no subclass reaches a protected property of a final class, and the parent's code only one it declares too
112+
$this->properties[$propertyName] = $isPrivate || ($isProtected && $this->isFinal);
113+
if ($isPrivate || !$isProtected || !$this->isFinal || !$this->hasParent) {
114+
return;
115+
}
116+
117+
$this->unlessParentDeclares[$propertyName] = true;
118+
}
119+
106120
/**
107121
* @param self::CALL_* $kind
108122
*/
@@ -301,11 +315,16 @@ private function resolve(int $kind, string $methodName, array $overrideWrites):
301315
[$written, $functionArguments, $escapes, $directWritten, $directFunctionArguments] = $collected;
302316

303317
$preserved = [];
318+
$unlessParentDeclares = [];
304319
foreach ($this->properties as $propertyName => $isPrivate) {
305320
if (isset($written[$propertyName]) || ($escapes && !$isPrivate)) {
306321
continue;
307322
}
308323
$preserved[$propertyName] = true;
324+
if (!$escapes || !isset($this->unlessParentDeclares[$propertyName])) {
325+
continue;
326+
}
327+
$unlessParentDeclares[$propertyName] = true;
309328
}
310329

311330
$preservedFunctionArguments = [];
@@ -328,7 +347,7 @@ private function resolve(int $kind, string $methodName, array $overrideWrites):
328347
return null;
329348
}
330349

331-
return new PreservedProperties($preserved, $preservedFunctionArguments, $directWritten, $directFunctionArgumentsUnwritten, $this->closures['writes'], $this->parentName, $overrideWrites);
350+
return new PreservedProperties($preserved, $preservedFunctionArguments, $directWritten, $directFunctionArgumentsUnwritten, $this->closures['writes'], $this->parentName, $unlessParentDeclares, $overrideWrites);
332351
}
333352

334353
/**

‎src/Parser/PreservedPropertiesVisitor.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function createScan(Node\Stmt\ClassLike $node): ?PreservedPropertiesClas
171171
$scan->unsupported = true;
172172
}
173173
foreach ($stmt->props as $prop) {
174-
$scan->properties[$prop->name->toString()] = $stmt->isPrivate() || $stmt->isPrivateSet();
174+
$scan->addProperty($prop->name->toString(), $stmt->isPrivate() || $stmt->isPrivateSet(), $stmt->isProtected() || $stmt->isProtectedSet());
175175
}
176176
} elseif ($stmt instanceof Node\Stmt\ClassMethod) {
177177
$methodName = $stmt->name->toLowerString();
@@ -200,7 +200,7 @@ private function createScan(Node\Stmt\ClassLike $node): ?PreservedPropertiesClas
200200
if ($param->hooks !== []) {
201201
$scan->unsupported = true;
202202
}
203-
$scan->properties[$param->var->name] = $param->isPrivate() || $param->isPrivateSet();
203+
$scan->addProperty($param->var->name, $param->isPrivate() || $param->isPrivateSet(), $param->isProtected() || $param->isProtectedSet());
204204
}
205205
}
206206
}

‎tests/PHPStan/Analyser/nsrt/method-call-preserved-properties-hooks.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,23 @@ private function doNothing(): void
3232
}
3333

3434
}
35+
36+
/** In a final class only its own code writes a protected(set) property. */
37+
final class FinalProtectedSet
38+
{
39+
40+
public protected(set) int $protectedSet = 0;
41+
42+
public function run(): void
43+
{
44+
$this->protectedSet = 1;
45+
$this->log();
46+
assertType('1', $this->protectedSet);
47+
}
48+
49+
private function log(): void
50+
{
51+
\error_log('x');
52+
}
53+
54+
}

‎tests/PHPStan/Analyser/nsrt/method-call-preserved-properties.php‎

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,94 @@ protected function post(): int
533533

534534
}
535535

536+
/** In a final class only its own code writes a protected property, as long as no parent declares it. */
537+
final class FinalProtected
538+
{
539+
540+
protected int $protected = 0;
541+
542+
public int $public = 0;
543+
544+
public function __construct(
545+
protected int $promoted = 0,
546+
)
547+
{
548+
}
549+
550+
public function run(): void
551+
{
552+
$this->protected = 1;
553+
$this->public = 1;
554+
$this->promoted = 1;
555+
$this->log();
556+
assertType('1', $this->protected);
557+
assertType('int', $this->public);
558+
assertType('1', $this->promoted);
559+
}
560+
561+
private function log(): void
562+
{
563+
\error_log('x');
564+
}
565+
566+
}
567+
568+
/** Code outside a class a subclass can extend can write its protected properties. */
569+
class NonFinalProtected
570+
{
571+
572+
protected int $protected = 0;
573+
574+
public function run(): void
575+
{
576+
$this->protected = 1;
577+
$this->log();
578+
assertType('int', $this->protected);
579+
}
580+
581+
private function log(): void
582+
{
583+
\error_log('x');
584+
}
585+
586+
}
587+
588+
class ProtectedParent
589+
{
590+
591+
protected int $declaredByParent = 0;
592+
593+
public function reset(): void
594+
{
595+
$this->declaredByParent = 0;
596+
}
597+
598+
}
599+
600+
/** The parent's code writes a protected property the parent declares too. */
601+
final class FinalProtectedChild extends ProtectedParent
602+
{
603+
604+
protected int $declaredByParent = 0;
605+
606+
protected int $childOnly = 0;
607+
608+
public function run(): void
609+
{
610+
$this->declaredByParent = 1;
611+
$this->childOnly = 1;
612+
$this->log();
613+
assertType('int', $this->declaredByParent);
614+
assertType('1', $this->childOnly);
615+
}
616+
617+
private function log(): void
618+
{
619+
\error_log('x');
620+
}
621+
622+
}
623+
536624
/** Receivers other than $this are not followed. */
537625
final class Receiver
538626
{
@@ -847,7 +935,7 @@ private function takeRef(?int &$value): bool
847935

848936
/**
849937
* A call that can run code outside the class - a function, another object's method, 'new' -
850-
* forgets public and protected properties; private ones when the followed code or the class's closures write them.
938+
* forgets public properties; private ones, and protected ones of a final class, when the followed code or the class's closures write them.
851939
*/
852940
final class OutsideCalls
853941
{
@@ -883,7 +971,7 @@ public function run(): void
883971
$this->promoted = true;
884972
$this->callsFunction();
885973
assertType('bool', $this->public);
886-
assertType('bool', $this->protected);
974+
assertType('true', $this->protected);
887975
assertType('true', $this->private);
888976
assertType('true', $this->writtenByPublicMethod);
889977
assertType('bool', $this->writtenInClosure);

0 commit comments

Comments
 (0)