From 7d1bde44afc0c0f7e3b29f2d75a7c33d5e6a56ec Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 23 Jul 2024 14:02:23 +0200 Subject: [PATCH] BetterReflectionSourceLocator - playground mode --- conf/config.neon | 2 ++ conf/parametersSchema.neon | 3 +++ .../BetterReflectionSourceLocatorFactory.php | 14 ++++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/conf/config.neon b/conf/config.neon index ac8f18be39..a16f09fd39 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -257,6 +257,7 @@ parameters: editorUrlTitle: null errorFormat: null sysGetTempDir: ::sys_get_temp_dir() + sourceLocatorPlaygroundMode: false pro: dnsServers: - '1.1.1.2' @@ -2063,6 +2064,7 @@ services: analysedPaths: %analysedPaths% composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths% analysedPathsFromConfig: %analysedPathsFromConfig% + playgroundMode: %sourceLocatorPlaygroundMode% - implement: PHPStan\Reflection\BetterReflection\BetterReflectionProviderFactory diff --git a/conf/parametersSchema.neon b/conf/parametersSchema.neon index f33fc0ba5d..111d864480 100644 --- a/conf/parametersSchema.neon +++ b/conf/parametersSchema.neon @@ -224,6 +224,9 @@ parametersSchema: env: arrayOf(string(), anyOf(int(), string())) sysGetTempDir: string() + # playground mode + sourceLocatorPlaygroundMode: bool() + # irrelevant Nette parameters debugMode: bool() productionMode: bool() diff --git a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php index 5a207d0476..f9c3649cf9 100644 --- a/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php +++ b/src/Reflection/BetterReflection/BetterReflectionSourceLocatorFactory.php @@ -55,6 +55,7 @@ public function __construct( private array $analysedPaths, private array $composerAutoloaderProjectPaths, private array $analysedPathsFromConfig, + private bool $playgroundMode, // makes all PHPStan classes in the PHAR discoverable with PSR-4 ) { } @@ -112,11 +113,16 @@ public function create(): SourceLocator if (extension_loaded('phar')) { $pharProtocolPath = Phar::running(); if ($pharProtocolPath !== '') { + $mappings = [ + 'PHPStan\\BetterReflection\\' => [$pharProtocolPath . '/vendor/ondrejmirtes/better-reflection/src/'], + ]; + if ($this->playgroundMode) { + $mappings['PHPStan\\'] = [$pharProtocolPath . '/src/']; + } else { + $mappings['PHPStan\\Testing\\'] = [$pharProtocolPath . '/src/Testing/']; + } $fileLocators[] = $this->optimizedPsrAutoloaderLocatorFactory->create( - Psr4Mapping::fromArrayMappings([ - 'PHPStan\\Testing\\' => [$pharProtocolPath . '/src/Testing/'], - 'PHPStan\\BetterReflection\\' => [$pharProtocolPath . '/vendor/ondrejmirtes/better-reflection/src/'], - ]), + Psr4Mapping::fromArrayMappings($mappings), ); } }