Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -669,18 +669,6 @@ parameters:
count: 1
path: src/Rules/Generics/TemplateTypeCheck.php

-
rawMessage: 'Function class_implements() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.'
identifier: phpstanApi.runtimeReflection
count: 1
path: src/Rules/LazyRegistry.php

-
rawMessage: 'Function class_parents() is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.'
identifier: phpstanApi.runtimeReflection
count: 1
path: src/Rules/LazyRegistry.php

-
rawMessage: 'Method PHPStan\Rules\LazyRegistry::getRulesByNodeType() return type with generic interface PHPStan\Rules\Rule does not specify its types: TNodeType'
identifier: missingType.generics
Expand Down
11 changes: 7 additions & 4 deletions src/Collectors/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use PhpParser\Node;
use PHPStan\DependencyInjection\AutowiredService;
use function class_implements;
use function class_parents;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ExtensionClassHelper;

#[AutowiredService(factory: '@PHPStan\Collectors\RegistryFactory::create')]
final class Registry
Expand All @@ -20,7 +20,10 @@ final class Registry
/**
* @param Collector[] $collectors
*/
public function __construct(array $collectors)
public function __construct(
array $collectors,
private ReflectionProvider $reflectionProvider,
)
{
foreach ($collectors as $collector) {
$this->collectors[$collector->getNodeType()][] = $collector;
Expand All @@ -35,7 +38,7 @@ public function __construct(array $collectors)
public function getCollectors(string $nodeType): array
{
if (!isset($this->cache[$nodeType])) {
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType);
$parentNodeTypes = ExtensionClassHelper::getExtensionClassNames($this->reflectionProvider, $nodeType);

$collectors = [];
foreach ($parentNodeTypes as $parentNodeType) {
Expand Down
3 changes: 3 additions & 0 deletions src/Collectors/RegistryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\DependencyInjection\AutowiredExtensions;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\ExtensionsCollection;
use PHPStan\Reflection\ReflectionProvider;

#[AutowiredService]
final class RegistryFactory
Expand All @@ -19,6 +20,7 @@ final class RegistryFactory
public function __construct(
#[AutowiredExtensions(of: Collector::class)]
private ExtensionsCollection $collectors,
private ReflectionProvider $reflectionProvider,
)
{
}
Expand All @@ -27,6 +29,7 @@ public function create(): Registry
{
return new Registry(
$this->collectors->getAll(),
$this->reflectionProvider,
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/PhpDoc/StubValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\DependencyInjection\Container;
use PHPStan\DependencyInjection\ContainerFactory;
use PHPStan\DependencyInjection\DerivativeContainerFactory;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\DirectRegistry as DirectRuleRegistry;
use Throwable;
use function array_fill_keys;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function validate(array $stubFiles, bool $debug): array
$analysedFiles = array_fill_keys($stubFiles, true);

$ruleRegistry = new DirectRuleRegistry($container->getServicesByTag(self::SERVICE_RULE_TAG));
$collectorRegistry = new CollectorRegistry([]);
$collectorRegistry = new CollectorRegistry([], $container->getByType(ReflectionProvider::class));

$errors = [];
foreach ($stubFiles as $stubFile) {
Expand Down
7 changes: 4 additions & 3 deletions src/Rules/LazyRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PHPStan\DependencyInjection\AutowiredExtensions;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\ExtensionsCollection;
use function class_implements;
use function class_parents;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ExtensionClassHelper;

#[AutowiredService(name: 'registry', as: Registry::class)]
final class LazyRegistry implements Registry
Expand All @@ -27,6 +27,7 @@ final class LazyRegistry implements Registry
public function __construct(
#[AutowiredExtensions(of: Rule::class)]
private ExtensionsCollection $rules,
private ReflectionProvider $reflectionProvider,
)
{
}
Expand All @@ -39,7 +40,7 @@ public function __construct(
public function getRules(string $nodeType): array
{
if (!isset($this->cache[$nodeType])) {
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType);
$parentNodeTypes = ExtensionClassHelper::getExtensionClassNames($this->reflectionProvider, $nodeType);

$rules = [];
$rulesFromContainer = $this->getRulesByNodeType();
Expand Down
9 changes: 7 additions & 2 deletions src/Testing/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,18 @@ protected function createNodeScopeResolver(): NodeScopeResolver
private function getAnalyser(DirectRuleRegistry $ruleRegistry): Analyser
{
if ($this->analyser === null) {
$collectorRegistry = new CollectorRegistry($this->getCollectors());
$reflectionProvider = $this->createReflectionProvider();

$collectorRegistry = new CollectorRegistry(
$this->getCollectors(),
$reflectionProvider,
);

$nodeScopeResolver = $this->createNodeScopeResolver();

$fileAnalyser = new FileAnalyser(
self::createScopeFactory(
$this->createReflectionProvider(),
$reflectionProvider,
$this->getTypeSpecifier(),
),
$nodeScopeResolver,
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/AnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,14 +802,14 @@ private function runAnalyser(

private function createAnalyser(): Analyser
{
$reflectionProvider = self::createReflectionProvider();

$ruleRegistry = new DirectRuleRegistry([
new AlwaysFailRule(),
]);
$collectorRegistry = new CollectorRegistry([]);
$collectorRegistry = new CollectorRegistry([], $reflectionProvider);

$reflectionProvider = self::createReflectionProvider();
$fileHelper = $this->getFileHelper();

$container = self::getContainer();
$typeSpecifier = $container->getService('typeSpecifier');
$fileTypeMapper = $container->getByType(FileTypeMapper::class);
Expand Down
15 changes: 8 additions & 7 deletions tests/PHPStan/Collectors/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public function testGetCollectors(): void
{
$collector = new DummyCollector();

$registry = new Registry([
$collector,
]);
$registry = new Registry(
[$collector],
$this->createReflectionProvider(),
);

$collectors = $registry->getCollectors(Node\Expr\FuncCall::class);
$this->assertCount(1, $collectors);
Expand All @@ -29,10 +30,10 @@ public function testGetCollectorsWithTwoDifferentInstances(): void
$fooCollector = new UniversalCollector(Node\Expr\FuncCall::class, static fn (Node\Expr\FuncCall $node, Scope $scope): array => ['Foo error']);
$barCollector = new UniversalCollector(Node\Expr\FuncCall::class, static fn (Node\Expr\FuncCall $node, Scope $scope): array => ['Bar error']);

$registry = new Registry([
$fooCollector,
$barCollector,
]);
$registry = new Registry(
[$fooCollector, $barCollector],
$this->createReflectionProvider(),
);

$collectors = $registry->getCollectors(Node\Expr\FuncCall::class);
$this->assertCount(2, $collectors);
Expand Down
Loading