Skip to content

Commit

Permalink
Use composer.json reading just to locate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 24, 2020
1 parent 47a76e3 commit 4cbb589
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 196 deletions.
3 changes: 0 additions & 3 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,6 @@ services:
-
class: PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ public function create(): SourceLocator
$locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($this->singleReflectionFile);
}

foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
$locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath);
if ($locator === null) {
continue;
}
$locators[] = $locator;
}

$analysedDirectories = [];
$analysedFiles = [];

Expand Down Expand Up @@ -164,6 +156,13 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
use Nette\Utils\Json;
use PHPStan\File\FileReader;
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr0Mapping;
use Roave\BetterReflection\SourceLocator\Type\Composer\Psr\Psr4Mapping;
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;

class ComposerJsonAndInstalledJsonSourceLocatorMaker
{

private \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository $optimizedDirectorySourceLocatorRepository;

private \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository;

private \PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedPsrAutoloaderLocatorFactory $optimizedPsrAutoloaderLocatorFactory;

public function __construct(
OptimizedDirectorySourceLocatorRepository $optimizedDirectorySourceLocatorRepository,
OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository,
OptimizedPsrAutoloaderLocatorFactory $optimizedPsrAutoloaderLocatorFactory
OptimizedSingleFileSourceLocatorRepository $optimizedSingleFileSourceLocatorRepository
)
{
$this->optimizedDirectorySourceLocatorRepository = $optimizedDirectorySourceLocatorRepository;
$this->optimizedSingleFileSourceLocatorRepository = $optimizedSingleFileSourceLocatorRepository;
$this->optimizedPsrAutoloaderLocatorFactory = $optimizedPsrAutoloaderLocatorFactory;
}

public function create(string $projectInstallationPath): ?SourceLocator
Expand Down Expand Up @@ -58,17 +48,6 @@ public function create(string $projectInstallationPath): ?SourceLocator

$installed = $installedJson['packages'] ?? $installedJson;

$classMapPaths = array_merge(
$this->prefixPaths($this->packageToClassMapPaths($composer), $projectInstallationPath . '/'),
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
return $this->prefixPaths(
$this->packageToClassMapPaths($package),
$this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package)
);
}, $installed)
);
$classMapFiles = array_filter($classMapPaths, 'is_file');
$classMapDirectories = array_filter($classMapPaths, 'is_dir');
$filePaths = array_merge(
$this->prefixPaths($this->packageToFilePaths($composer), $projectInstallationPath . '/'),
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
Expand All @@ -80,42 +59,7 @@ public function create(string $projectInstallationPath): ?SourceLocator
);

$locators = [];
$locators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
Psr4Mapping::fromArrayMappings(array_merge_recursive(
$this->prefixWithInstallationPath($this->packageToPsr4AutoloadNamespaces($composer), $projectInstallationPath),
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
return $this->prefixWithPackagePath(
$this->packageToPsr4AutoloadNamespaces($package),
$projectInstallationPath,
$installedJsonDirectoryPath,
$package
);
}, $installed)
))
);

$locators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
Psr0Mapping::fromArrayMappings(array_merge_recursive(
$this->prefixWithInstallationPath($this->packageToPsr0AutoloadNamespaces($composer), $projectInstallationPath),
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
return $this->prefixWithPackagePath(
$this->packageToPsr0AutoloadNamespaces($package),
$projectInstallationPath,
$installedJsonDirectoryPath,
$package
);
}, $installed)
))
);

foreach ($classMapDirectories as $classMapDirectory) {
if (!is_dir($classMapDirectory)) {
continue;
}
$locators[] = $this->optimizedDirectorySourceLocatorRepository->getOrCreate($classMapDirectory);
}

foreach (array_merge($classMapFiles, $filePaths) as $file) {
foreach ($filePaths as $file) {
if (!is_file($file)) {
continue;
}
Expand All @@ -125,40 +69,6 @@ public function create(string $projectInstallationPath): ?SourceLocator
return new AggregateSourceLocator($locators);
}

/**
* @param mixed[] $package
*
* @return array<string, array<int, string>>
*/
private function packageToPsr4AutoloadNamespaces(array $package): array
{
return array_map(static function ($namespacePaths): array {
return (array) $namespacePaths;
}, $package['autoload']['psr-4'] ?? []);
}

/**
* @param mixed[] $package
*
* @return array<string, array<int, string>>
*/
private function packageToPsr0AutoloadNamespaces(array $package): array
{
return array_map(static function ($namespacePaths): array {
return (array) $namespacePaths;
}, $package['autoload']['psr-0'] ?? []);
}

/**
* @param mixed[] $package
*
* @return array<int, string>
*/
private function packageToClassMapPaths(array $package): array
{
return $package['autoload']['classmap'] ?? [];
}

/**
* @param mixed[] $package
*
Expand All @@ -185,33 +95,6 @@ private function packagePrefixPath(
return $projectInstallationPath . '/vendor/' . $package['name'] . '/';
}

/**
* @param array<string, array<int, string>> $paths
* @param array<string, array<int, string>> $package
*
* @return array<string, array<int, string>>
*/
private function prefixWithPackagePath(array $paths, string $projectInstallationPath, string $installedJsonDirectoryPath, array $package): array
{
$prefix = $this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package);

return array_map(function (array $paths) use ($prefix): array {
return $this->prefixPaths($paths, $prefix);
}, $paths);
}

/**
* @param array<int|string, array<string>> $paths
*
* @return array<int|string, array<string>>
*/
private function prefixWithInstallationPath(array $paths, string $trimmedInstallationPath): array
{
return array_map(function (array $paths) use ($trimmedInstallationPath): array {
return $this->prefixPaths($paths, $trimmedInstallationPath . '/');
}, $paths);
}

/**
* @param array<int, string> $paths
*
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 4cbb589

Please sign in to comment.