Skip to content

Commit

Permalink
Attempt to load remaining classes after skipping them in ClassBlackli…
Browse files Browse the repository at this point in the history
…stSourceLocator if they're not in Composer paths
  • Loading branch information
ondrejmirtes committed Jun 24, 2020
1 parent 3902d73 commit 2e61259
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
3 changes: 0 additions & 3 deletions build/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ parameters:
stubFiles:
- stubs/ReactChildProcess.php
- stubs/ReactStreams.php
staticReflectionClassNamePatterns!:
- '#^PhpParser\\#'
- '#^Hoa\\#'
services:
-
class: PHPStan\Build\ServiceLocatorDynamicReturnTypeExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPStan\DependencyInjection\Container;
use PHPStan\Reflection\BetterReflection\SourceLocator\AutoloadSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ClassBlacklistSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ClassWhitelistSourceLocator;
use PHPStan\Reflection\BetterReflection\SourceLocator\ComposerJsonAndInstalledJsonSourceLocatorMaker;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedDirectorySourceLocatorRepository;
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocatorRepository;
Expand Down Expand Up @@ -170,6 +171,7 @@ public function create(): SourceLocator
}
$locators[] = $locator;
}
$locators[] = new ClassWhitelistSourceLocator($this->autoloadSourceLocator, $this->staticReflectionClassNamePatterns);
$locators[] = new PhpInternalSourceLocator($astLocator, $this->reflectionSourceStubber);
$locators[] = new EvaledCodeSourceLocator($astLocator, $this->reflectionSourceStubber);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace PHPStan\Reflection\BetterReflection\SourceLocator;

use Nette\Utils\Strings;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflection\Reflection;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;

class ClassWhitelistSourceLocator implements SourceLocator
{

private SourceLocator $sourceLocator;

/** @var string[] */
private array $patterns;

/**
* @param SourceLocator $sourceLocator
* @param string[] $patterns
*/
public function __construct(
SourceLocator $sourceLocator,
array $patterns
)
{
$this->sourceLocator = $sourceLocator;
$this->patterns = $patterns;
}

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if ($identifier->isClass()) {
foreach ($this->patterns as $pattern) {
if (Strings::match($identifier->getName(), $pattern) !== null) {
return $this->sourceLocator->locateIdentifier($reflector, $identifier);
}
}

return null;
}

return $this->sourceLocator->locateIdentifier($reflector, $identifier);
}

public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return $this->sourceLocator->locateIdentifiersByType($reflector, $identifierType);
}

}

0 comments on commit 2e61259

Please sign in to comment.