Skip to content

Commit

Permalink
Single reflection file support
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 17, 2020
1 parent 6bcb400 commit e6ba8a0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
4 changes: 4 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ parametersSchema:
usedLevel: string()
cliAutoloadFile: schema(string(), nullable())

# internal - static reflection
singleReflectionFile: schema(string(), nullable())

services:
-
class: PhpParser\BuilderFactory
Expand Down Expand Up @@ -1154,6 +1157,7 @@ services:
analysedPaths: %analysedPaths%
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
analysedPathsFromConfig: %analysedPathsFromConfig%
singleReflectionFile: %singleReflectionFile%

-
implement: PHPStan\Reflection\BetterReflection\BetterReflectionProviderFactory
Expand Down
5 changes: 3 additions & 2 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static function begin(
?string $level,
bool $allowXdebug,
bool $manageMemoryLimitFile = true,
bool $debugEnabled = false
bool $debugEnabled = false,
?string $singleReflectionFile = null
): InceptionResult
{
if (!$allowXdebug) {
Expand Down Expand Up @@ -242,7 +243,7 @@ public static function begin(
}

try {
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $allCustomConfigFiles, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile);
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $allCustomConfigFiles, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile);
} catch (\Nette\DI\InvalidConfigurationException | \Nette\Utils\AssertionException $e) {
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($e->getMessage());
Expand Down
7 changes: 6 additions & 1 deletion src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(string $currentWorkingDirectory)
* @param string $usedLevel
* @param string|null $generateBaselineFile
* @param string|null $cliAutoloadFile
* @param string|null $singleReflectionFile
* @return \PHPStan\DependencyInjection\Container
*/
public function create(
Expand All @@ -57,7 +58,8 @@ public function create(
array $allCustomConfigFiles = [],
string $usedLevel = CommandHelper::DEFAULT_LEVEL,
?string $generateBaselineFile = null,
?string $cliAutoloadFile = null
?string $cliAutoloadFile = null,
?string $singleReflectionFile = null
): Container
{
$configurator = new Configurator(new LoaderFactory(
Expand Down Expand Up @@ -85,6 +87,9 @@ public function create(
'usedLevel' => $usedLevel,
'cliAutoloadFile' => $cliAutoloadFile,
]);
$configurator->addDynamicParameters([
'singleReflectionFile' => $singleReflectionFile,
]);
$configurator->addConfig($this->configDirectory . '/config.neon');
foreach ($additionalConfigFiles as $additionalConfigFile) {
$configurator->addConfig($additionalConfigFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class BetterReflectionSourceLocatorFactory
/** @var string[] */
private $analysedPathsFromConfig;

/** @var string|null */
private $singleReflectionFile;

/**
* @param string[] $autoloadDirectories
* @param string[] $autoloadFiles
Expand All @@ -75,6 +78,7 @@ class BetterReflectionSourceLocatorFactory
* @param string[] $analysedPaths
* @param string[] $composerAutoloaderProjectPaths
* @param string[] $analysedPathsFromConfig
* @param string|null $singleReflectionFile
*/
public function __construct(
\PhpParser\Parser $parser,
Expand All @@ -91,7 +95,8 @@ public function __construct(
array $scanDirectories,
array $analysedPaths,
array $composerAutoloaderProjectPaths,
array $analysedPathsFromConfig
array $analysedPathsFromConfig,
?string $singleReflectionFile
)
{
$this->parser = $parser;
Expand All @@ -109,12 +114,17 @@ public function __construct(
$this->analysedPaths = $analysedPaths;
$this->composerAutoloaderProjectPaths = $composerAutoloaderProjectPaths;
$this->analysedPathsFromConfig = $analysedPathsFromConfig;
$this->singleReflectionFile = $singleReflectionFile;
}

public function create(): SourceLocator
{
$locators = [];

if ($this->singleReflectionFile !== null) {
$locators[] = $this->optimizedSingleFileSourceLocatorRepository->getOrCreate($this->singleReflectionFile);
}

foreach ($this->composerAutoloaderProjectPaths as $composerAutoloaderProjectPath) {
$locator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerAutoloaderProjectPath);
if ($locator === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\GlobalConstantReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionWithFilename;
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;

class ClassBlacklistReflectionProvider implements ReflectionProvider
Expand All @@ -20,19 +21,23 @@ class ClassBlacklistReflectionProvider implements ReflectionProvider
/** @var string[] */
private array $patterns;

private ?string $singleReflectionFile;

/**
* @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
* @param string[] $patterns
*/
public function __construct(
ReflectionProvider $reflectionProvider,
PhpStormStubsSourceStubber $phpStormStubsSourceStubber,
array $patterns
array $patterns,
?string $singleReflectionFile
)
{
$this->reflectionProvider = $reflectionProvider;
$this->phpStormStubsSourceStubber = $phpStormStubsSourceStubber;
$this->patterns = $patterns;
$this->singleReflectionFile = $singleReflectionFile;
}

public function hasClass(string $className): bool
Expand Down Expand Up @@ -60,6 +65,11 @@ public function hasClass(string $className): bool
}

$classReflection = $this->reflectionProvider->getClass($className);
if ($this->singleReflectionFile !== null) {
if ($classReflection->getFileName() === $this->singleReflectionFile) {
return false;
}
}
if ($classReflection->isSubclassOf(\DateInterval::class)
|| $classReflection->isSubclassOf(\DatePeriod::class)
|| $classReflection->isSubclassOf(\DOMDocument::class)
Expand Down Expand Up @@ -99,7 +109,21 @@ public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNo

public function hasFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool
{
return $this->reflectionProvider->hasFunction($nameNode, $scope);
$has = $this->reflectionProvider->hasFunction($nameNode, $scope);
if (!$has) {
return false;
}

if ($this->singleReflectionFile === null) {
return true;
}

$functionReflection = $this->reflectionProvider->getFunction($nameNode, $scope);
if (!$functionReflection instanceof ReflectionWithFilename) {
return true;
}

return $functionReflection->getFileName() !== $this->singleReflectionFile;
}

public function getFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): FunctionReflection
Expand Down
3 changes: 2 additions & 1 deletion src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ private function createRuntimeReflectionProvider(ReflectionProvider $actualRefle
'#^PhpParser\\\\#',
'#^PHPStan\\\\#',
'#^Hoa\\\\#',
]
],
null
);
$this->setUpReflectionProvider(
$actualReflectionProvider,
Expand Down

0 comments on commit e6ba8a0

Please sign in to comment.