Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/manual-tests
composer.lock
index.php
benchmark.php
.php-cs-fixer.cache
2 changes: 2 additions & 0 deletions src/Contract/ContractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\PhpDocParser\Ast\Type\OffsetAccessTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use TypePHP\Internal\Checker\InlineChecker;
use TypePHP\Internal\Config;
use TypePHP\Internal\StubManager;
use TypePHP\Resolver\SpecialTypeResolver;
Expand Down Expand Up @@ -68,6 +69,7 @@ public static function reset(): void
self::$propertyCache = [];
self::$magicMethodCache = [];
self::$classLevelDocCache = [];
InlineChecker::reset();
DocblockExtractor::reset();
FileFilter::reset();
TypeValidatorRegistry::reset();
Expand Down
34 changes: 28 additions & 6 deletions src/Internal/Checker/InlineChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ final class InlineChecker
*/
private static array $parsedTypeNodeCache = [];

/**
* In-memory cache for fully resolved type nodes per type string and file.
*
* @var array<string, TypeNode>
*/
private static array $resolvedTypeNodeCache = [];

/**
* Resets internal type node caches. Useful for test isolation.
*/
public static function reset(): void
{
self::$parsedTypeNodeCache = [];
self::$resolvedTypeNodeCache = [];
}

/**
* Fast lookup set for scalar refinement types.
*/
Expand Down Expand Up @@ -103,15 +119,21 @@ public static function checkVariable(mixed $value, string $typeString, string $v
}

try {
$typeString = DocblockNormalizer::normalize($typeString);
$typeNode = self::parseTypeString($typeString);
$cacheKey = $typeString . '|' . $file;
if (isset(self::$resolvedTypeNodeCache[$cacheKey])) {
$typeNode = self::$resolvedTypeNodeCache[$cacheKey];
} else {
$normalized = DocblockNormalizer::normalize($typeString);
$typeNode = self::parseTypeString($normalized);

if ($file !== '') {
$typeNode = SpecialTypeResolver::resolveForFile($typeNode, $file);
}

if ($file !== '') {
$typeNode = SpecialTypeResolver::resolveForFile($typeNode, $file);
$typeNode = self::resolveCallerContext($typeNode);
self::$resolvedTypeNodeCache[$cacheKey] = $typeNode;
}

$typeNode = self::resolveCallerContext($typeNode);

if (! self::shouldValidateType($typeNode, $config)) {
return $value;
}
Expand Down
27 changes: 18 additions & 9 deletions src/Internal/Checker/ParamChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ public static function checkParams(
string $function,
array $vars,
object|string|null $thisOrClass,
TypeValidatorRegistry $registry
TypeValidatorRegistry $registry,
string $effectiveFunction = ''
): ?ErrorMessage {
if (! Config::isParamsEnabled()) {
return null;
}

$thisObj = \is_object($thisOrClass) ? $thisOrClass : null;
$effectiveFunction = self::resolveEffectiveFunction($function, $thisOrClass, $thisObj);
if ($effectiveFunction === '') {
$effectiveFunction = self::resolveEffectiveFunction($function, $thisOrClass, $thisObj);
}

$magicError = self::handleMagicCall($effectiveFunction, $vars, $thisObj, $registry);
if ($magicError !== null) {
Expand Down Expand Up @@ -140,20 +143,23 @@ public static function resolveEffectiveFunction(string $function, object|string|
}

$actualClassName = \is_object($thisOrClass) ? \get_class($thisOrClass) : (\is_string($thisOrClass) ? $thisOrClass : '');
$cacheKey = $function . '|' . $actualClassName;
if ($actualClassName === '') {
return $function;
}

[$classOrTrait, $methodName] = explode('::', $function, 2);

$cacheKey = $function . '|' . $actualClassName;
if (isset(self::$effectiveFunctionCache[$cacheKey])) {
return self::$effectiveFunctionCache[$cacheKey];
}

[$classOrTrait, $methodName] = explode('::', $function, 2);

$effectiveFunction = ($actualClassName !== '' && $actualClassName !== $classOrTrait)
$effectiveFunction = ($actualClassName !== $classOrTrait)
? $actualClassName . '::' . $methodName
: $function;

if ($thisObj !== null) {
$targetClass = $actualClassName !== '' ? $actualClassName : $classOrTrait;
$targetClass = $actualClassName;
$traitAliases = HierarchyResolver::getTraitAliases($targetClass);

if (\count($traitAliases) > 0) {
Expand Down Expand Up @@ -195,8 +201,11 @@ private static function handleMagicCall(
?object $thisObj,
TypeValidatorRegistry $registry
): ?ErrorMessage {
$isMagicCall = str_ends_with($effectiveFunction, '::__call') || str_ends_with($effectiveFunction, '::__callStatic');
if (! $isMagicCall || ! Config::isMagicMethodsEnabled()) {
if (! Config::isMagicMethodsEnabled()) {
return null;
}

if (! str_ends_with($effectiveFunction, '::__call') && ! str_ends_with($effectiveFunction, '::__callStatic')) {
return null;
}

Expand Down
20 changes: 13 additions & 7 deletions src/Internal/Checker/ReturnChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,23 @@ private static function resolveEffectiveFunction(string $function, object|string
}

$actualClassName = \is_object($thisOrClass) ? \get_class($thisOrClass) : (\is_string($thisOrClass) ? $thisOrClass : '');
$cacheKey = $function . '|' . $actualClassName;
if ($actualClassName === '') {
return $function;
}

[$classOrTrait, $methodName] = explode('::', $function, 2);

$cacheKey = $function . '|' . $actualClassName;
if (isset(self::$effectiveFunctionCache[$cacheKey])) {
return self::$effectiveFunctionCache[$cacheKey];
}

[$classOrTrait, $methodName] = explode('::', $function, 2);

$effectiveFunction = ($actualClassName !== '' && $actualClassName !== $classOrTrait)
$effectiveFunction = ($actualClassName !== $classOrTrait)
? $actualClassName . '::' . $methodName
: $function;

if ($thisObj !== null) {
$targetClass = $actualClassName !== '' ? $actualClassName : $classOrTrait;
$targetClass = $actualClassName;
$traitAliases = HierarchyResolver::getTraitAliases($targetClass);

if (\count($traitAliases) > 0) {
Expand Down Expand Up @@ -163,8 +166,11 @@ private static function handleMagicReturn(
TypeValidatorRegistry $registry,
callable $wrapIterableCallback
): mixed {
$isMagicCall = str_ends_with($effectiveFunction, '::__call') || str_ends_with($effectiveFunction, '::__callStatic');
if (! $isMagicCall || ! Config::isMagicMethodsEnabled()) {
if (! Config::isMagicMethodsEnabled()) {
return null;
}

if (! str_ends_with($effectiveFunction, '::__call') && ! str_ends_with($effectiveFunction, '::__callStatic')) {
return null;
}

Expand Down
Loading
Loading