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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,8 +55,8 @@ final class CachedPhpInternalSourceLocator implements SourceLocator

private const RELATIVE_FILENAME_PREFIX = 'phpstorm-stubs:';

/** @var array<string, string>|null lowercase name => stub file, of every function in the stubs of any extension version */
private static ?array $stubFunctions = null;
/** @var array<string, true>|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;

Expand Down Expand Up @@ -178,29 +178,46 @@ private function resolveStubFilename(array $cached): ?array
return $cached;
}

/**
* @return list<Reflection>
*/
/**
* 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<Reflection>
*/
#[Override]
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, list<string>, list<string>}
* @return array{array<string, mixed>, array<string, mixed>, array<string, mixed>}
*/
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];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int|list<int>>, array<string, int|list<int>>, array<string, int|list<int>>}|null class, function and constant name => the locators declaring it */
private ?array $declaringLocators = null;
/** @var array{list<array<string, mixed>>, list<array<string, mixed>>, list<array<string, mixed>>}|null the class, function and constant maps of each locator, by its index */
private ?array $symbolMaps = null;

/**
* @param list<OptimizedDirectorySourceLocator> $locators
Expand Down Expand Up @@ -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;
Expand All @@ -79,28 +79,19 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
}

/**
* @return array{array<string, int|list<int>>, array<string, int|list<int>>, array<string, int|list<int>>}
* @return array{list<array<string, mixed>>, list<array<string, mixed>>, list<array<string, mixed>>}
*/
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]
Expand Down
8 changes: 7 additions & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
14 changes: 11 additions & 3 deletions turbo-ext/src/ObjectType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading