diff --git a/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php index e5a0fa8b99d..bca6a852036 100644 --- a/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php +++ b/src/Reflection/BetterReflection/SourceLocator/CachedPhpInternalSourceLocator.php @@ -18,8 +18,8 @@ use PHPStan\Internal\ComposerHelper; use PHPStan\Php\PhpVersion; use PHPStan\Reflection\BetterReflection\SourceStubber\ExtensionVersionProvider; -use function array_change_key_case; use function array_key_exists; +use function array_keys; use function is_array; use function is_file; use function is_string; @@ -55,8 +55,8 @@ final class CachedPhpInternalSourceLocator implements SourceLocator private const RELATIVE_FILENAME_PREFIX = 'phpstorm-stubs:'; - /** @var array|null lowercase name => stub file, of every function in the stubs of any extension version */ - private static ?array $stubFunctions = null; + /** @var array|null lowercase names of the functions in the stubs not listed in lowercase in PhpStormStubsMap::FUNCTIONS, see isStubFunction() */ + private static ?array $otherStubFunctions = null; private ?string $variableCacheKey = null; @@ -178,29 +178,46 @@ private function resolveStubFilename(array $cached): ?array return $cached; } - /** - * @return list - */ /** * Whether the stubber could have a function: the stubber has no cheap way to tell, its own * answer parses the stub file. The functions of the extension versions it selects are added * to the default ones, so they are all included. + * + * Nearly all names in the map are lowercase already and are looked up in it directly: a + * lowercased copy of the whole map would stay in memory for the rest of the process. */ private static function isStubFunction(string $lowercaseName): bool { - if (self::$stubFunctions === null) { - $functions = array_change_key_case(PhpStormStubsMap::FUNCTIONS); + if (array_key_exists($lowercaseName, PhpStormStubsMap::FUNCTIONS)) { + return true; + } + + if (self::$otherStubFunctions === null) { + $functions = []; + foreach (array_keys(PhpStormStubsMap::FUNCTIONS) as $name) { + $lowercaseFunctionName = strtolower($name); + if ($lowercaseFunctionName === $name) { + continue; + } + + $functions[$lowercaseFunctionName] = true; + } foreach (PhpStormStubsMap::EXTENSION_VERSIONS as $versionMaps) { foreach ($versionMaps as $versionMap) { - $functions += array_change_key_case($versionMap['functions']); + foreach (array_keys($versionMap['functions']) as $name) { + $functions[strtolower($name)] = true; + } } } - self::$stubFunctions = $functions; + self::$otherStubFunctions = $functions; } - return array_key_exists($lowercaseName, self::$stubFunctions); + return array_key_exists($lowercaseName, self::$otherStubFunctions); } + /** + * @return list + */ #[Override] public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array { diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php index a7ad252f288..f05cef1bff0 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocator.php @@ -77,18 +77,19 @@ public function fillScanned(array $classToFile, array $functionToFiles, array $c } /** - * The lowercase class names, the lowercase function names and the normalized constant names - * it declares - see OptimizedDirectorySourceLocatorGroup. + * The maps of the symbols it declares, keyed by the lowercase class names, the lowercase + * function names and the normalized constant names - see OptimizedDirectorySourceLocatorGroup. + * They are not changed after the scan, so they are shared, not copied. * - * @return array{list, list, list} + * @return array{array, array, array} */ - public function getDeclaredSymbolNames(): array + public function getSymbolMaps(): array { if ($this->awaitingScan) { throw new ShouldNotHappenException('Symbols were looked up in a directory whose batch has not been scanned yet.'); } - return [array_keys($this->classToFile), array_keys($this->functionToFiles), array_keys($this->constantToFile)]; + return [$this->classToFile, $this->functionToFiles, $this->constantToFile]; } /** diff --git a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php index 6702410fb93..fdcd9d4cb7d 100644 --- a/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php +++ b/src/Reflection/BetterReflection/SourceLocator/OptimizedDirectorySourceLocatorGroup.php @@ -11,21 +11,22 @@ use PHPStan\Reflection\ConstantNameHelper; use function array_merge; use function count; -use function is_int; use function strtolower; /** * Asks OptimizedDirectorySourceLocators as if they were asked in turn, the way - * AggregateSourceLocator would: the first one locating the symbol answers. Which of them declare - * it is looked up once, in a map of all their symbols built when the group is first asked, instead - * of in every one of them - a project with many Composer packages has a locator for each classmap - * directory, and most names asked for are declared in none of them. + * AggregateSourceLocator would: the first one locating the symbol answers. Only the ones whose + * symbol map has the name are asked, and the maps are checked directly instead of through each + * locator - a project with many Composer packages has a locator for each classmap directory, and + * most names asked for are declared in none of them. The maps are the locators' own: one map of + * all their symbols would answer in one lookup, but it would keep a second copy of every name for + * the rest of the process. */ final class OptimizedDirectorySourceLocatorGroup implements SourceLocator { - /** @var array{array>, array>, array>}|null class, function and constant name => the locators declaring it */ - private ?array $declaringLocators = null; + /** @var array{list>, list>, list>}|null the class, function and constant maps of each locator, by its index */ + private ?array $symbolMaps = null; /** * @param list $locators @@ -62,13 +63,12 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): return null; } - $this->declaringLocators ??= $this->buildDeclaringLocators(); - $indexes = $this->declaringLocators[$kind][$name] ?? null; - if ($indexes === null) { - return null; - } + $this->symbolMaps ??= $this->collectSymbolMaps(); + foreach ($this->symbolMaps[$kind] as $index => $symbols) { + if (!isset($symbols[$name])) { + continue; + } - foreach (is_int($indexes) ? [$indexes] : $indexes as $index) { $reflection = $this->locators[$index]->locateIdentifier($reflector, $identifier); if ($reflection !== null) { return $reflection; @@ -79,28 +79,19 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier): } /** - * @return array{array>, array>, array>} + * @return array{list>, list>, list>} */ - private function buildDeclaringLocators(): array + private function collectSymbolMaps(): array { - $declaringLocators = [[], [], []]; - foreach ($this->locators as $index => $locator) { - foreach ($locator->getDeclaredSymbolNames() as $kind => $names) { - foreach ($names as $name) { - $declaring = $declaringLocators[$kind][$name] ?? null; - if ($declaring === null) { - $declaringLocators[$kind][$name] = $index; - continue; - } - - $declaring = is_int($declaring) ? [$declaring] : $declaring; - $declaring[] = $index; - $declaringLocators[$kind][$name] = $declaring; - } - } + $symbolMaps = [[], [], []]; + foreach ($this->locators as $locator) { + [$classes, $functions, $constants] = $locator->getSymbolMaps(); + $symbolMaps[0][] = $classes; + $symbolMaps[1][] = $functions; + $symbolMaps[2][] = $constants; } - return $declaringLocators; + return $symbolMaps; } #[Override] diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 0115cea2a85..4b96125e2d7 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -1505,7 +1505,13 @@ private function isExtraOffsetAccessibleClass(): TrinaryLogic } foreach (self::EXTRA_OFFSET_CLASSES as $extraOffsetClass) { - if ($classReflection->is($extraOffsetClass)) { + // like $classReflection->is($extraOffsetClass), without looking up the class: the answer + // depends only on the ancestors, and a lookup is recorded as a dependency of the analysed file + $ancestor = $classReflection->getAncestorWithClassName($extraOffsetClass); + if ($ancestor === null) { + continue; + } + if ($ancestor === $classReflection || $classReflection->isSubclassOfClass($ancestor)) { return TrinaryLogic::createYes(); } } diff --git a/turbo-ext/src/ObjectType.cpp b/turbo-ext/src/ObjectType.cpp index 1e81b93bad3..10f66302d3a 100644 --- a/turbo-ext/src/ObjectType.cpp +++ b/turbo-ext/src/ObjectType.cpp @@ -1888,9 +1888,17 @@ class ObjectType zend_object *reflection = Z_OBJ_P(classReflection.raw()); for (const pt_superglobal_name &extraOffsetClass : pt_ot_extra_offset_classes) { - bool is; - if (UNEXPECTED(!reflectionIs(reflection, extraOffsetClass.name, extraOffsetClass.len, is))) return -1; - if (is) return PT_TRI_YES; + /* like $classReflection->is(), without looking up the class: the + * answer depends only on the ancestors, and a lookup is recorded + * as a dependency of the analysed file */ + zv::Val name = zv::Val::string(extraOffsetClass.name, extraOffsetClass.len); + zv::Val ancestor = pt_type_call(reflection, PT_LC("getancestorwithclassname"), 1, name.raw()); + if (UNEXPECTED(ancestor.isUndef())) return -1; + if (ancestor.isNull()) continue; + if (Z_OBJ_P(ancestor.raw()) == reflection) return PT_TRI_YES; + bool subclass; + if (UNEXPECTED(!pt_type_call_bool(reflection, PT_LC("issubclassofclass"), 1, ancestor.raw(), subclass))) return -1; + if (subclass) return PT_TRI_YES; } bool isInterface;