Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
I realized that "files" from composer.json are already loaded so AutoloadSourceLocator
will work even for functions
  • Loading branch information
ondrejmirtes committed Jun 24, 2020
1 parent 73aede3 commit 1cd97b4
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 192 deletions.
10 changes: 0 additions & 10 deletions compiler/build/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ function (string $filePath, string $prefix, string $content): string {

return \Nette\Neon\Neon::encode($updatedNeon, \Nette\Neon\Neon::BLOCK);
},
function (string $filePath, string $prefix, string $content): string {
if (!in_array($filePath, [
'src/Testing/TestCaseSourceLocatorFactory.php',
'src/Testing/TestCase.php',
], true)) {
return $content;
}

return str_replace(sprintf('%s\\Composer\\Autoload\\ClassLoader', $prefix), 'Composer\\Autoload\\ClassLoader', $content);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php') {
return $content;
Expand Down
4 changes: 0 additions & 4 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,6 @@ services:
-
class: PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator

-
class: PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker

-
implement: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorFactory

Expand Down Expand Up @@ -1152,7 +1149,6 @@ services:
scanFiles: %scanFiles%
scanDirectories: %scanDirectories%
analysedPaths: %analysedPaths%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
analysedPathsFromConfig: %analysedPathsFromConfig%
singleReflectionFile: %singleReflectionFile%

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPStan\DependencyInjection\Container;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository;
use PHPStan\Reflection\BetterReflection\SourceLocator\SkipClassAliasSourceLocator;
Expand Down Expand Up @@ -37,9 +36,6 @@ class BetterReflectionSourceLocatorFactory
/** @var \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository */
private $optimizedDirectorySourceLocatorRepository;

/** @var ComposerJsonAndInstalledJsonSourceLocatorMaker */
private $composerJsonAndInstalledJsonSourceLocatorMaker;

/** @var AutoloadSourceLocator */
private $autoloadSourceLocator;

Expand All @@ -61,9 +57,6 @@ class BetterReflectionSourceLocatorFactory
/** @var string[] */
private $analysedPaths;

/** @var string[] */
private $composerAutoloaderProjectPaths;

/** @var string[] */
private $analysedPathsFromConfig;

Expand All @@ -76,7 +69,6 @@ class BetterReflectionSourceLocatorFactory
* @param string[] $scanFiles
* @param string[] $scanDirectories
* @param string[] $analysedPaths
* @param string[] $composerAutoloaderProjectPaths
* @param string[] $analysedPathsFromConfig
* @param string|null $singleReflectionFile
*/
Expand All @@ -86,15 +78,13 @@ public function __construct(
ReflectionSourceStubber $reflectionSourceStubber,
OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository,
OptimizedDirectorySourceLocatorRepository $optimizedDirectorySourceLocatorRepository,
ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker,
AutoloadSourceLocator $autoloadSourceLocator,
Container $container,
array $autoloadDirectories,
array $autoloadFiles,
array $scanFiles,
array $scanDirectories,
array $analysedPaths,
array $composerAutoloaderProjectPaths,
array $analysedPathsFromConfig,
?string $singleReflectionFile
)
Expand All @@ -104,15 +94,13 @@ public function __construct(
$this->reflectionSourceStubber = $reflectionSourceStubber;
$this->optimizedSingleFileSourceLocatorRepository = $optimizedSingleFileSourceLocatorRepository;
$this->optimizedDirectorySourceLocatorRepository = $optimizedDirectorySourceLocatorRepository;
$this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker;
$this->autoloadSourceLocator = $autoloadSourceLocator;
$this->container = $container;
$this->autoloadDirectories = $autoloadDirectories;
$this->autoloadFiles = $autoloadFiles;
$this->scanFiles = $scanFiles;
$this->scanDirectories = $scanDirectories;
$this->analysedPaths = $analysedPaths;
$this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths;
$this->analysedPathsFromConfig = $analysedPathsFromConfig;
$this->singleReflectionFile = $singleReflectionFile;
}
Expand Down Expand Up @@ -156,13 +144,6 @@ public function create(): SourceLocator
});
$locators[] = new SkipClassAliasSourceLocator(new PhpInternalSourceLocator($astLocator, $this->phpstormStubsSourceStubber));
$locators[] = $this->autoloadSourceLocator;
foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
$locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath);
if ($locator === null) {
continue;
}
$locators[] = $locator;
}
$locators[] = new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber);
$locators[] = new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber);

Expand Down

This file was deleted.

26 changes: 1 addition & 25 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PHPStan\Testing;

use Composer\Autoload\ClassLoader;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\DirectScopeFactory;
use PHPStan\Analyser\MutatingScope;
Expand Down Expand Up @@ -37,7 +36,6 @@
use PHPStan\Reflection\BetterReflection\Reflector\MemoizingConstantReflector;
use PHPStan\Reflection\BetterReflection\Reflector\MemoizingFunctionReflector;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflectionFactory;
Expand Down Expand Up @@ -409,31 +407,9 @@ public static function getReflectors(): array
return self::$reflectors;
}

if (!class_exists(ClassLoader::class)) {
self::fail('Composer ClassLoader is unknown');
}

$classLoaderReflection = new \ReflectionClass(ClassLoader::class);
if ($classLoaderReflection->getFileName() === false) {
self::fail('Unknown ClassLoader filename');
}

$composerProjectPath = dirname($classLoaderReflection->getFileName(), 3);
if (!is_file($composerProjectPath . '/composer.json')) {
self::fail(sprintf('composer.json not found in directory %s', $composerProjectPath));
}

$composerJsonAndInstalledJsonSourceLocatorMaker = self::getContainer()->getByType(ComposerJsonAndInstalledJsonSourceLocatorMaker::class);
$composerSourceLocator = $composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath);
if ($composerSourceLocator === null) {
self::fail('Could not create composer source locator');
}

// these need to be synced with TestCase-staticReflection.neon file and TestCaseSourceLocatorFactory

$locators = [
$composerSourceLocator,
];
$locators = [];

$phpParser = new PhpParserDecorator(self::getContainer()->getByType(CachedParser::class));

Expand Down
25 changes: 1 addition & 24 deletions src/Testing/TestCaseSourceLocatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

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;
Expand All @@ -21,8 +19,6 @@ class TestCaseSourceLocatorFactory

private Container $container;

private ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker;

private AutoloadSourceLocator $autoloadSourceLocator;

private \PhpParser\Parser $phpParser;
Expand All @@ -33,15 +29,13 @@ class TestCaseSourceLocatorFactory

public function __construct(
Container $container,
ComposerJsonAndInstalledJsonSourceLocatorMaker $composerJsonAndInstalledJsonSourceLocatorMaker,
AutoloadSourceLocator $autoloadSourceLocator,
\PhpParser\Parser $phpParser,
PhpStormStubsSourceStubber $phpstormStubsSourceStubber,
ReflectionSourceStubber $reflectionSourceStubber
)
{
$this->container = $container;
$this->composerJsonAndInstalledJsonSourceLocatorMaker = $composerJsonAndInstalledJsonSourceLocatorMaker;
$this->autoloadSourceLocator = $autoloadSourceLocator;
$this->phpParser = $phpParser;
$this->phpstormStubsSourceStubber = $phpstormStubsSourceStubber;
Expand All @@ -50,24 +44,7 @@ public function __construct(

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,
];
$locators = [];
$astLocator = new Locator($this->phpParser, function (): FunctionReflector {
return $this->container->getService('testCaseFunctionReflector');
});
Expand Down

0 comments on commit 1cd97b4

Please sign in to comment.