Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions src/Internal/ComposerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class ComposerHelper

private static ?string $betterReflectionVersion = null;

private static ?string $phpstomStubsVersion = null;

private static ?string $phpDocParserVersion = null;

/** @var array<string, mixed[]> */
Expand Down Expand Up @@ -124,6 +126,21 @@ public static function getBetterReflectionVersion(): string
return self::$betterReflectionVersion = self::processPackageVersion($rootPackage);
}

public static function getPhpStormStubsVersion(): string
{
if (self::$phpstomStubsVersion !== null) {
return self::$phpstomStubsVersion;
}

$installed = self::getInstalled();
$rootPackage = $installed['versions']['jetbrains/phpstorm-stubs'] ?? null;
if ($rootPackage === null) {
return self::$phpstomStubsVersion = self::UNKNOWN_VERSION;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo phpstom

}

return self::$phpstomStubsVersion = self::processPackageVersion($rootPackage);
}

public static function getPhpDocParserVersion(): string
{
if (self::$phpDocParserVersion !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
use PHPStan\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Cache\Cache;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadFunctionsSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileCachedSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory;
Expand All @@ -34,6 +37,7 @@
use function extension_loaded;
use function is_dir;
use function is_file;
use function sprintf;
use const PHP_VERSION_ID;

#[AutowiredService]
Expand Down Expand Up @@ -74,6 +78,7 @@ public function __construct(
private bool $playgroundMode, // makes all PHPStan classes in the PHAR discoverable with PSR-4
#[AutowiredParameter]
private ?string $singleReflectionFile,
private Cache $cache,
)
{
}
Expand Down Expand Up @@ -161,8 +166,17 @@ public function create(): SourceLocator
}
}

$phpstormStubsVersion = ComposerHelper::getPhpStormStubsVersion();

$locators[] = new RewriteClassAliasSourceLocator(new AggregateSourceLocator($fileLocators));
$locators[] = new SkipClassAliasSourceLocator(new PhpInternalSourceLocator($astPhp8Locator, $this->phpstormStubsSourceStubber));
$locators[] = new SkipClassAliasSourceLocator(
new FileCachedSourceLocator(
new PhpInternalSourceLocator($astPhp8Locator, $this->phpstormStubsSourceStubber),
$this->cache,
$this->phpVersion,
sprintf('phpstorm-stubs-php8-%s', $phpstormStubsVersion),
),
);

$locators[] = new AutoloadSourceLocator($this->fileNodesFetcher, true);
$locators[] = new PhpVersionBlacklistSourceLocator(new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber), $this->phpstormStubsSourceStubber);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\BetterReflection\SourceLocator;

use Override;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflection\ReflectionClass;
use PHPStan\BetterReflection\Reflection\ReflectionConstant;
use PHPStan\BetterReflection\Reflection\ReflectionEnum;
use PHPStan\BetterReflection\Reflection\ReflectionFunction;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Cache\Cache;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstantNameHelper;
use PHPStan\ShouldNotHappenException;
use function array_key_exists;
use function sprintf;
use function strtolower;

final class FileCachedSourceLocator implements SourceLocator
{

/** @var array{classes: array<string, ?Reflection>, functions: array<string, ?Reflection>, constants: array<string, ?Reflection>}|null */
private ?array $cachedSymbols = null;

/**
* @param non-empty-string $cacheKey
*/
public function __construct(
private SourceLocator $locator,
private Cache $cache,
private PhpVersion $phpVersion,
private string $cacheKey,
)
{
}

#[Override]
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
$this->cachedSymbols ??= $this->loadCache($reflector);

if ($identifier->isClass()) {
$className = strtolower($identifier->getName());

if (!array_key_exists($className, $this->cachedSymbols['classes'])) {
$this->cachedSymbols['classes'][$className] = $this->locator->locateIdentifier($reflector, $identifier);
$this->storeCache();
}
return $this->cachedSymbols['classes'][$className];

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 54 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
}
if ($identifier->isFunction()) {
$className = strtolower($identifier->getName());

if (!array_key_exists($className, $this->cachedSymbols['functions'])) {
$this->cachedSymbols['functions'][$className] = $this->locator->locateIdentifier($reflector, $identifier);

Check failure on line 60 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 60 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Offset 'classes' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
$this->storeCache();
}
return $this->cachedSymbols['functions'][$className];

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 63 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
}
if ($identifier->isConstant()) {
$constantName = ConstantNameHelper::normalize($identifier->getName());

if (!array_key_exists($constantName, $this->cachedSymbols['constants'])) {
$this->cachedSymbols['constants'][$constantName] = $this->locator->locateIdentifier($reflector, $identifier);

Check failure on line 69 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 69 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Offset 'functions' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
$this->storeCache();
}
return $this->cachedSymbols['constants'][$constantName];

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.5)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.5, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 72 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
}

return null;
}

#[Override]

Check failure on line 78 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.

Check failure on line 78 in src/Reflection/BetterReflection/SourceLocator/FileCachedSourceLocator.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Offset 'constants' might not exist on array{classes: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, functions: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>, constants: array<string, PHPStan\BetterReflection\Reflection\Reflection|null>}|null.
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return $this->locator->locateIdentifiersByType($reflector, $identifierType);
}

/** @return non-empty-string */
private function getVariableCacheKey(): string
{
return sprintf('v2-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString());
}

/** @return array{classes: array<string, ReflectionClass|null>, functions: array<string, ReflectionFunction|null>, constants: array<string, ReflectionConstant|null>} */
private function loadCache(Reflector $reflector): array
{
$variableCacheKey = $this->getVariableCacheKey();
$cached = $this->cache->load($this->cacheKey, $variableCacheKey);

$restored = [
'classes' => [],
'functions' => [],
'constants' => [],
];
if ($cached === null) {
return $restored;
}

foreach ($cached['classes'] ?? [] as $class => $cachedReflection) {
if ($cachedReflection === null) {
$restored['classes'][$class] = null;
continue;
}

if (array_key_exists('backingType', $cachedReflection)) {
$restored['classes'][$class] = ReflectionEnum::importFromCache($reflector, $cachedReflection);
continue;
}

$restored['classes'][$class] = ReflectionClass::importFromCache($reflector, $cachedReflection);
}
foreach ($cached['functions'] ?? [] as $class => $cachedReflection) {
if ($cachedReflection === null) {
$restored['functions'][$class] = null;
continue;
}
$restored['functions'][$class] = ReflectionFunction::importFromCache($reflector, $cachedReflection);
}
foreach ($cached['constants'] ?? [] as $constantName => $cachedReflection) {
if ($cachedReflection === null) {
$restored['constants'][$constantName] = null;
continue;
}

$restored['constants'][$constantName] = ReflectionConstant::importFromCache($reflector, $cachedReflection);
Copy link
Copy Markdown
Contributor Author

@staabm staabm Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReflectionConstant/ReflectionParameter->importFromCache() is expensive.
it is dominated by PHPParser overhead

Image

looks like the parser instantiation happens multiple times

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return $restored;
}

private function storeCache(): void
{
$variableCacheKey = $this->getVariableCacheKey();

$exported = [
'classes' => [],
'functions' => [],
'constants' => [],
];
foreach ($this->cachedSymbols ?? [] as $type => $data) {
foreach ($data as $name => $reflection) {
if ($reflection === null) {
$exported[$type][$name] = $reflection;
continue;
}

if (
!$reflection instanceof ReflectionClass
&& !$reflection instanceof ReflectionFunction
&& !$reflection instanceof ReflectionConstant
) {
throw new ShouldNotHappenException();
}

$exported[$type][$name] = $reflection->exportToCache();
}
}

$this->cache->save($this->cacheKey, $variableCacheKey, $exported);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it tries to have a single cache for everything? It could have a separate cache for each symbol.

}

}
Loading