Skip to content

Commit a4d5198

Browse files
committed
Improve generic Class static method and static property handling
1 parent d9afe73 commit a4d5198

6 files changed

Lines changed: 669 additions & 10 deletions

File tree

‎src/Internal/Checker/InlineChecker.php‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ final class InlineChecker
7878
*/
7979
private static array $classHasSelfOutCache = [];
8080

81+
/**
82+
* In-memory cache for resolved static property generic types: [$className][$propName] => TypeNode.
83+
*
84+
* @var array<string, array<string, TypeNode>>
85+
*/
86+
private static array $resolvedStaticPropertyTypeCache = [];
87+
8188
/**
8289
* Resets internal type node and function caches. Useful for test isolation.
8390
*/
@@ -88,6 +95,7 @@ public static function reset(): void
8895
self::$nullPropertyCache = [];
8996
self::$propertyUsesTemplatesCache = [];
9097
self::$classHasSelfOutCache = [];
98+
self::$resolvedStaticPropertyTypeCache = [];
9199
}
92100

93101
/**
@@ -304,9 +312,6 @@ private static function isArrayShapeType(TypeNode $node): bool
304312
return false;
305313
}
306314

307-
/**
308-
* Evaluates class property validation dynamically based on configuration with zero-allocation 2D caching.
309-
*/
310315
/**
311316
* Evaluates class property validation dynamically based on configuration with zero-allocation 2D caching.
312317
*/
@@ -326,8 +331,9 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
326331
return $value;
327332
}
328333

334+
// 1. Enforce magic property access permissions only for non-physical dynamic properties
329335
$rawTypeNode = null;
330-
if (Config::isMagicPropertiesEnabled()) {
336+
if (! property_exists($className, $propName) && Config::isMagicPropertiesEnabled()) {
331337
$magicContract = DocblockParser::parseMagicPropertyContract($className, $propName);
332338
if ($magicContract !== null) {
333339
if (! Config::isMagicPropertyWritesEnabled()) {
@@ -342,6 +348,7 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
342348
}
343349
}
344350

351+
// 2. Fall back to standard declared class property if not intercepted by a magic contract
345352
if ($rawTypeNode === null) {
346353
$rawTypeNode = DocblockParser::parseProperty($className, $propName);
347354
}
@@ -364,6 +371,11 @@ public static function checkProperty(mixed $value, mixed $objectOrClass, string
364371
if ($propertyUsesTemplates) {
365372
$typeNode = self::substitutePropertyGenerics($rawTypeNode, $objectOrClass, $className);
366373
}
374+
} elseif (\is_string($objectOrClass)) {
375+
$propertyUsesTemplates = self::propertyUsesTemplates($rawTypeNode, $className, $propName);
376+
if ($propertyUsesTemplates) {
377+
$typeNode = self::substituteStaticPropertyGenerics($rawTypeNode, $className, $propName);
378+
}
367379
}
368380

369381
try {
@@ -673,6 +685,40 @@ private static function substitutePropertyGenerics(TypeNode $typeNode, object $o
673685
return $typeNode;
674686
}
675687

688+
/**
689+
* Substitutes generic template types declared on static class properties with zero-allocation memoization.
690+
*/
691+
private static function substituteStaticPropertyGenerics(TypeNode $typeNode, string $className, string $propName): TypeNode
692+
{
693+
if (isset(self::$resolvedStaticPropertyTypeCache[$className][$propName])) {
694+
return self::$resolvedStaticPropertyTypeCache[$className][$propName];
695+
}
696+
697+
$constructorTarget = $className . '::__construct';
698+
$contract = DocblockParser::parse($constructorTarget);
699+
700+
$allTemplates = [...($contract['classTemplates'] ?? []), ...($contract['templates'] ?? [])];
701+
$classBindings = TemplateManager::getClassInheritedBindings($className);
702+
$classAliases = DocblockParser::parseClassAliases($className);
703+
$activeBindings = [...$classAliases, ...$classBindings];
704+
705+
if (\count($activeBindings) > 0 || \count($allTemplates) > 0) {
706+
$typeNode = TemplateSubstitutor::substitute($typeNode, $activeBindings, $allTemplates);
707+
708+
if (class_exists($className, false) || class_exists($className) || interface_exists($className) || trait_exists($className)) {
709+
try {
710+
/** @var class-string<object> $className */
711+
$refClass = new \ReflectionClass($className);
712+
$typeNode = SpecialTypeResolver::resolve($typeNode, $refClass);
713+
} catch (\ReflectionException $e) {
714+
// Silently continue if reflection fails
715+
}
716+
}
717+
}
718+
719+
return self::$resolvedStaticPropertyTypeCache[$className][$propName] = $typeNode;
720+
}
721+
676722
/**
677723
* Inspects a TypeNode to check if it requires caller context resolution (templates, aliases, self/static/$this).
678724
*/

‎src/Internal/Checker/ParamChecker.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,10 @@ private static function prepareGenericBindings(
273273
array $classTemplates,
274274
object|string|null $thisOrClass = null
275275
): void {
276-
if (\count($methodTemplates) > 0) {
277-
TemplateManager::clearCallBindings($effectiveFunction, $methodTemplates);
276+
$hasTemplatesToClear = \count($methodTemplates) > 0 || ($thisObj === null && \count($classTemplates) > 0);
277+
278+
if ($hasTemplatesToClear) {
279+
TemplateManager::clearCallBindings($effectiveFunction, [...$classTemplates, ...$methodTemplates]);
278280
}
279281

280282
if (\count($classTemplates) > 0 && str_contains($effectiveFunction, '::')) {

‎src/Internal/Docblock/DocblockExtractor.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public static function extractMagicPropertyContract(string $doc, string $propNam
111111
foreach ($phpDocNode->getPropertyTagValues() as $tag) {
112112
if (ltrim($tag->propertyName, '$') === $propName) {
113113
$propTag = $tag;
114+
114115
break;
115116
}
116117
}
@@ -119,6 +120,7 @@ public static function extractMagicPropertyContract(string $doc, string $propNam
119120
foreach ($phpDocNode->getPropertyReadTagValues() as $tag) {
120121
if (ltrim($tag->propertyName, '$') === $propName) {
121122
$readTag = $tag;
123+
122124
break;
123125
}
124126
}
@@ -127,6 +129,7 @@ public static function extractMagicPropertyContract(string $doc, string $propNam
127129
foreach ($phpDocNode->getPropertyWriteTagValues() as $tag) {
128130
if (ltrim($tag->propertyName, '$') === $propName) {
129131
$writeTag = $tag;
132+
130133
break;
131134
}
132135
}
@@ -338,9 +341,9 @@ public static function getVarTags(PhpDocNode $node): array
338341
}
339342

340343
$unnamed = [];
341-
$unnamedPhpstan = array_values(array_filter($node->getVarTagValues('@phpstan-var'), fn($t) => $t->variableName === ''));
342-
$unnamedPsalm = array_values(array_filter($node->getVarTagValues('@psalm-var'), fn($t) => $t->variableName === ''));
343-
$unnamedStandard = array_values(array_filter($node->getVarTagValues('@var'), fn($t) => $t->variableName === ''));
344+
$unnamedPhpstan = array_values(array_filter($node->getVarTagValues('@phpstan-var'), fn ($t) => $t->variableName === ''));
345+
$unnamedPsalm = array_values(array_filter($node->getVarTagValues('@psalm-var'), fn ($t) => $t->variableName === ''));
346+
$unnamedStandard = array_values(array_filter($node->getVarTagValues('@var'), fn ($t) => $t->variableName === ''));
344347

345348
if (\count($unnamedPhpstan) > 0) {
346349
$unnamed = $unnamedPhpstan;

‎src/Internal/RuntimeTypeChecker.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,11 @@ public static function setupScope(string $function, array $vars, object|string|n
253253

254254
$hasMethodTemplates = self::$hasMethodTemplatesCache[$effectiveFunction] ?? null;
255255
if ($hasMethodTemplates === null) {
256+
$isStaticCall = ($thisObj === null);
256257
$hasMethodTemplates = self::$hasMethodTemplatesCache[$effectiveFunction] = (
257-
(($contract['templates'] ?? []) !== []) || ($contract['returnUsesMethodTemplates'] ?? false)
258+
(($contract['templates'] ?? []) !== [])
259+
|| ($contract['returnUsesMethodTemplates'] ?? false)
260+
|| ($isStaticCall && ($contract['classTemplates'] ?? []) !== [])
258261
);
259262
self::$hasMethodTemplatesCache[$function] = $hasMethodTemplates;
260263
}

0 commit comments

Comments
 (0)