-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar reflection provider in Broker::getInstance() for unit tests v…
…ia static reflection
- Loading branch information
1 parent
f5de06d
commit cb479ed
Showing
7 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
parameters: | ||
featureToggles: | ||
# these are needed only with "partial static reflection" | ||
disableRobotLoader: true | ||
disableRuntimeReflectionProvider: false | ||
enableScanningPaths: false | ||
|
||
services: | ||
- | ||
class: PHPStan\Testing\TestCaseSourceLocatorFactory | ||
arguments: | ||
phpParser: @phpParserDecorator | ||
|
||
testCaseBetterReflectionProvider: | ||
class: PHPStan\Reflection\BetterReflection\BetterReflectionProvider | ||
arguments: | ||
classReflector: @testCaseClassReflector | ||
functionReflector: @testCaseFunctionReflector | ||
constantReflector: @testCaseConstantReflector | ||
autowired: false | ||
|
||
testCaseClassReflector: | ||
class: PHPStan\Reflection\BetterReflection\Reflector\MemoizingClassReflector | ||
arguments: | ||
sourceLocator: @testCaseSourceLocator | ||
autowired: false | ||
|
||
testCaseFunctionReflector: | ||
class: PHPStan\Reflection\BetterReflection\Reflector\MemoizingFunctionReflector | ||
arguments: | ||
classReflector: @testCaseClassReflector | ||
sourceLocator: @testCaseSourceLocator | ||
autowired: false | ||
|
||
testCaseConstantReflector: | ||
class: PHPStan\Reflection\BetterReflection\Reflector\MemoizingConstantReflector | ||
arguments: | ||
classReflector: @testCaseClassReflector | ||
sourceLocator: @testCaseSourceLocator | ||
autowired: false | ||
|
||
testCaseSourceLocator: | ||
class: Roave\BetterReflection\SourceLocator\Type\SourceLocator | ||
factory: @PHPStan\Testing\TestCaseSourceLocatorFactory::create() | ||
autowired: false | ||
|
||
# total static reflection without RuntimeReflectionProvider involved | ||
#reflectionProvider: | ||
# factory: @testCaseBetterReflectionProvider | ||
# autowired: | ||
# - PHPStan\Reflection\ReflectionProvider | ||
|
||
# partial static reflection with RuntimeReflectionProvider involved | ||
phpParserReflectionProvider: | ||
factory: @testCaseBetterReflectionProvider | ||
arguments!: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Testing; | ||
|
||
use Composer\Autoload\ClassLoader; | ||
use PHPStan\DependencyInjection\Container; | ||
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator; | ||
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker; | ||
use Roave\BetterReflection\Reflector\FunctionReflector; | ||
use Roave\BetterReflection\SourceLocator\Ast\Locator; | ||
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; | ||
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator; | ||
use Roave\BetterReflection\SourceLocator\Type\SourceLocator; | ||
|
||
class TestCaseSourceLocatorFactory | ||
{ | ||
|
||
private Container $container; | ||
|
||
private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker; | ||
|
||
private \PhpParser\Parser $phpParser; | ||
|
||
private PhpStormStubsSourceStubber $phpStormStubsSourceStubber; | ||
|
||
public function __construct( | ||
Container $container, | ||
ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker, | ||
\PhpParser\Parser $phpParser, | ||
PhpStormStubsSourceStubber $phpStormStubsSourceStubber | ||
) | ||
{ | ||
$this->container = $container; | ||
$this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker; | ||
$this->phpParser = $phpParser; | ||
$this->phpStormStubsSourceStubber = $phpStormStubsSourceStubber; | ||
} | ||
|
||
public function create(): SourceLocator | ||
{ | ||
$classLoaderReflection = new \ReflectionClass(ClassLoader::class); | ||
if ($classLoaderReflection->getFileName() === false) { | ||
throw new \PHPStan\ShouldNotHappenException('Unknown ClassLoader filename'); | ||
} | ||
|
||
$composerProjectPath = dirname($classLoaderReflection->getFileName(), 3); | ||
if (!is_file($composerProjectPath . '/composer.json')) { | ||
throw new \PHPStan\ShouldNotHappenException(sprintf('composer.json not found in directory %s', $composerProjectPath)); | ||
} | ||
|
||
$composerSourceLocator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath); | ||
if ($composerSourceLocator === null) { | ||
throw new \PHPStan\ShouldNotHappenException('Could not create composer source locator'); | ||
} | ||
|
||
$locators = [ | ||
$composerSourceLocator, | ||
]; | ||
$astLocator = new Locator($this->phpParser, function (): FunctionReflector { | ||
return $this->container->getService('testCaseFunctionReflector'); | ||
}); | ||
|
||
$locators[] = new AutoloadSourceLocator($astLocator); | ||
$locators[] = new PhpInternalSourceLocator($astLocator, $this->phpStormStubsSourceStubber); | ||
|
||
return new MemoizingSourceLocator(new AggregateSourceLocator($locators)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
require_once __DIR__ . '/bootstrap.php'; | ||
|
||
\PHPStan\Testing\TestCase::getContainer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters