Skip to content

Commit 35470a0

Browse files
committed
Introduce ClassReflectionFactory
1 parent 821ff76 commit 35470a0

File tree

7 files changed

+45
-145
lines changed

7 files changed

+45
-145
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource;
6666
use PHPStan\DependencyInjection\AutowiredParameter;
6767
use PHPStan\DependencyInjection\AutowiredService;
68-
use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider;
6968
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
7069
use PHPStan\DependencyInjection\Type\ParameterClosureThisExtensionProvider;
7170
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
@@ -137,15 +136,13 @@
137136
use PHPStan\Php\PhpVersion;
138137
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
139138
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
140-
use PHPStan\PhpDoc\StubPhpDocProvider;
141139
use PHPStan\PhpDoc\Tag\VarTag;
142140
use PHPStan\Reflection\Assertions;
143-
use PHPStan\Reflection\AttributeReflectionFactory;
144141
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
145142
use PHPStan\Reflection\Callables\SimpleImpurePoint;
146143
use PHPStan\Reflection\Callables\SimpleThrowPoint;
147144
use PHPStan\Reflection\ClassReflection;
148-
use PHPStan\Reflection\Deprecation\DeprecationProvider;
145+
use PHPStan\Reflection\ClassReflectionFactory;
149146
use PHPStan\Reflection\ExtendedMethodReflection;
150147
use PHPStan\Reflection\ExtendedParameterReflection;
151148
use PHPStan\Reflection\ExtendedParametersAcceptor;
@@ -163,7 +160,6 @@
163160
use PHPStan\Reflection\Php\PhpMethodReflection;
164161
use PHPStan\Reflection\Php\PhpPropertyReflection;
165162
use PHPStan\Reflection\ReflectionProvider;
166-
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
167163
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
168164
use PHPStan\ShouldNotHappenException;
169165
use PHPStan\TrinaryLogic;
@@ -253,23 +249,18 @@ final class NodeScopeResolver
253249
/**
254250
* @param string[][] $earlyTerminatingMethodCalls className(string) => methods(string[])
255251
* @param array<int, string> $earlyTerminatingFunctionCalls
256-
* @param string[] $universalObjectCratesClasses
257252
*/
258253
public function __construct(
259254
private readonly ReflectionProvider $reflectionProvider,
260255
private readonly InitializerExprTypeResolver $initializerExprTypeResolver,
261256
#[AutowiredParameter(ref: '@nodeScopeResolverReflector')]
262257
private readonly Reflector $reflector,
263-
private readonly ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider,
258+
private readonly ClassReflectionFactory $classReflectionFactory,
264259
private readonly ParameterOutTypeExtensionProvider $parameterOutTypeExtensionProvider,
265260
#[AutowiredParameter(ref: '@defaultAnalysisParser')]
266261
private readonly Parser $parser,
267262
private readonly FileTypeMapper $fileTypeMapper,
268-
private readonly StubPhpDocProvider $stubPhpDocProvider,
269263
private readonly PhpVersion $phpVersion,
270-
private readonly SignatureMapProvider $signatureMapProvider,
271-
private readonly DeprecationProvider $deprecationProvider,
272-
private readonly AttributeReflectionFactory $attributeReflectionFactory,
273264
private readonly PhpDocInheritanceResolver $phpDocInheritanceResolver,
274265
private readonly FileHelper $fileHelper,
275266
private readonly TypeSpecifier $typeSpecifier,
@@ -288,8 +279,6 @@ public function __construct(
288279
private readonly array $earlyTerminatingMethodCalls,
289280
#[AutowiredParameter]
290281
private readonly array $earlyTerminatingFunctionCalls,
291-
#[AutowiredParameter]
292-
private readonly array $universalObjectCratesClasses,
293282
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
294283
private readonly bool $implicitThrows,
295284
#[AutowiredParameter]
@@ -2326,23 +2315,12 @@ private function createAstClassReflection(Node\Stmt\ClassLike $stmt, string $cla
23262315

23272316
$enumAdapter = base64_decode('UEhQU3RhblxCZXR0ZXJSZWZsZWN0aW9uXFJlZmxlY3Rpb25cQWRhcHRlclxSZWZsZWN0aW9uRW51bQ==', true);
23282317

2329-
return new ClassReflection(
2330-
$this->reflectionProvider,
2331-
$this->initializerExprTypeResolver,
2332-
$this->fileTypeMapper,
2333-
$this->stubPhpDocProvider,
2334-
$this->phpDocInheritanceResolver,
2335-
$this->phpVersion,
2336-
$this->signatureMapProvider,
2337-
$this->deprecationProvider,
2338-
$this->attributeReflectionFactory,
2339-
$this->classReflectionExtensionRegistryProvider,
2318+
return $this->classReflectionFactory->create(
23402319
$betterReflectionClass->getName(),
23412320
$betterReflectionClass instanceof ReflectionEnum && PHP_VERSION_ID >= 80000 ? new $enumAdapter($betterReflectionClass) : new ReflectionClass($betterReflectionClass),
23422321
null,
23432322
null,
23442323
null,
2345-
$this->universalObjectCratesClasses,
23462324
sprintf('%s:%d', $scope->getFile(), $stmt->getStartLine()),
23472325
);
23482326
}

src/Reflection/BetterReflection/BetterReflectionProvider.php

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@
2424
use PHPStan\Broker\FunctionNotFoundException;
2525
use PHPStan\DependencyInjection\AutowiredParameter;
2626
use PHPStan\DependencyInjection\NonAutowiredService;
27-
use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider;
2827
use PHPStan\File\FileHelper;
2928
use PHPStan\File\FileReader;
3029
use PHPStan\File\RelativePathHelper;
3130
use PHPStan\Parser\AnonymousClassVisitor;
3231
use PHPStan\Php\PhpVersion;
33-
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
3432
use PHPStan\PhpDoc\StubPhpDocProvider;
3533
use PHPStan\PhpDoc\Tag\ParamClosureThisTag;
3634
use PHPStan\PhpDoc\Tag\ParamOutTag;
3735
use PHPStan\Reflection\Assertions;
3836
use PHPStan\Reflection\AttributeReflectionFactory;
3937
use PHPStan\Reflection\ClassNameHelper;
4038
use PHPStan\Reflection\ClassReflection;
39+
use PHPStan\Reflection\ClassReflectionFactory;
4140
use PHPStan\Reflection\Constant\RuntimeConstantReflection;
4241
use PHPStan\Reflection\ConstantReflection;
4342
use PHPStan\Reflection\Deprecation\DeprecationProvider;
@@ -50,7 +49,6 @@
5049
use PHPStan\Reflection\Php\PhpFunctionReflection;
5150
use PHPStan\Reflection\ReflectionProvider;
5251
use PHPStan\Reflection\SignatureMap\NativeFunctionReflectionProvider;
53-
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
5452
use PHPStan\ShouldNotHappenException;
5553
use PHPStan\TrinaryLogic;
5654
use PHPStan\Type\FileTypeMapper;
@@ -85,13 +83,11 @@ final class BetterReflectionProvider implements ReflectionProvider
8583
* @param list<string> $universalObjectCratesClasses
8684
*/
8785
public function __construct(
88-
private ReflectionProvider\ReflectionProviderProvider $reflectionProviderProvider,
8986
private InitializerExprTypeResolver $initializerExprTypeResolver,
90-
private ClassReflectionExtensionRegistryProvider $classReflectionExtensionRegistryProvider,
87+
private ClassReflectionFactory $classReflectionFactory,
9188
#[AutowiredParameter(ref: '@betterReflectionReflector')]
9289
private Reflector $reflector,
9390
private FileTypeMapper $fileTypeMapper,
94-
private PhpDocInheritanceResolver $phpDocInheritanceResolver,
9591
private DeprecationProvider $deprecationProvider,
9692
private PhpVersion $phpVersion,
9793
private NativeFunctionReflectionProvider $nativeFunctionReflectionProvider,
@@ -101,7 +97,6 @@ public function __construct(
10197
private AnonymousClassNameHelper $anonymousClassNameHelper,
10298
private FileHelper $fileHelper,
10399
private PhpStormStubsSourceStubber $phpstormStubsSourceStubber,
104-
private SignatureMapProvider $signatureMapProvider,
105100
private AttributeReflectionFactory $attributeReflectionFactory,
106101
#[AutowiredParameter(ref: '%universalObjectCratesClasses%')]
107102
private array $universalObjectCratesClasses,
@@ -149,28 +144,13 @@ public function getClass(string $className): ClassReflection
149144

150145
$enumAdapter = base64_decode('UEhQU3RhblxCZXR0ZXJSZWZsZWN0aW9uXFJlZmxlY3Rpb25cQWRhcHRlclxSZWZsZWN0aW9uRW51bQ==', true);
151146

152-
$classReflection = new ClassReflection(
153-
$this->reflectionProviderProvider->getReflectionProvider(),
154-
$this->initializerExprTypeResolver,
155-
$this->fileTypeMapper,
156-
$this->stubPhpDocProvider,
157-
$this->phpDocInheritanceResolver,
158-
$this->phpVersion,
159-
$this->signatureMapProvider,
160-
$this->deprecationProvider,
161-
$this->attributeReflectionFactory,
162-
$this->classReflectionExtensionRegistryProvider,
147+
return $this->classReflections[$reflectionClassName] = $this->classReflectionFactory->create(
163148
$reflectionClass->getName(),
164149
$reflectionClass instanceof ReflectionEnum && PHP_VERSION_ID >= 80000 ? new $enumAdapter($reflectionClass) : new ReflectionClass($reflectionClass),
165150
null,
166151
null,
167152
$this->stubPhpDocProvider->findClassPhpDoc($reflectionClass->getName()),
168-
$this->universalObjectCratesClasses,
169153
);
170-
171-
$this->classReflections[$reflectionClassName] = $classReflection;
172-
173-
return $classReflection;
174154
}
175155

176156
public function getClassName(string $className): string
@@ -241,27 +221,13 @@ public function getAnonymousClassReflection(Node\Stmt\Class_ $classNode, Scope $
241221
$displayName = sprintf('%s@anonymous/%s:%s:%d', $displayParentName, $filename, $classNode->getStartLine(), $classLineIndex);
242222
}
243223

244-
self::$anonymousClasses[$className] = new ClassReflection(
245-
$this->reflectionProviderProvider->getReflectionProvider(),
246-
$this->initializerExprTypeResolver,
247-
$this->fileTypeMapper,
248-
$this->stubPhpDocProvider,
249-
$this->phpDocInheritanceResolver,
250-
$this->phpVersion,
251-
$this->signatureMapProvider,
252-
$this->deprecationProvider,
253-
$this->attributeReflectionFactory,
254-
$this->classReflectionExtensionRegistryProvider,
224+
return $this->classReflections[$className] = self::$anonymousClasses[$className] = $this->classReflectionFactory->create(
255225
$displayName,
256226
new ReflectionClass($reflectionClass),
257227
$scopeFile,
258228
null,
259229
$this->stubPhpDocProvider->findClassPhpDoc($className),
260-
$this->universalObjectCratesClasses,
261230
);
262-
$this->classReflections[$className] = self::$anonymousClasses[$className];
263-
264-
return self::$anonymousClasses[$className];
265231
}
266232

267233
public function getUniversalObjectCratesClasses(): array

src/Reflection/ClassReflection.php

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
1414
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase;
1515
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
16+
use PHPStan\DependencyInjection\GenerateFactory;
1617
use PHPStan\DependencyInjection\Reflection\ClassReflectionExtensionRegistryProvider;
1718
use PHPStan\Php\PhpVersion;
1819
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
@@ -73,6 +74,7 @@
7374
/**
7475
* @api
7576
*/
77+
#[GenerateFactory(interface: ClassReflectionFactory::class)]
7678
final class ClassReflection
7779
{
7880

@@ -160,10 +162,8 @@ final class ClassReflection
160162
/** @var array<string, bool> */
161163
private array $hasStaticPropertyCache = [];
162164

163-
/**
164-
* @param string[] $universalObjectCratesClasses
165-
*/
166165
public function __construct(
166+
private ClassReflectionFactory $classReflectionFactory,
167167
private ReflectionProvider $reflectionProvider,
168168
private InitializerExprTypeResolver $initializerExprTypeResolver,
169169
private FileTypeMapper $fileTypeMapper,
@@ -179,7 +179,6 @@ public function __construct(
179179
private ?string $anonymousFilename,
180180
private ?TemplateTypeMap $resolvedTemplateTypeMap,
181181
private ?ResolvedPhpDocBlock $stubPhpDocBlock,
182-
private array $universalObjectCratesClasses,
183182
private ?string $extraCacheKey = null,
184183
private ?TemplateTypeVarianceMap $resolvedCallSiteVarianceMap = null,
185184
private ?bool $finalByKeywordOverride = null,
@@ -1778,23 +1777,12 @@ public function varianceMapToList(TemplateTypeVarianceMap $varianceMap): array
17781777
*/
17791778
public function withTypes(array $types): self
17801779
{
1781-
return new self(
1782-
$this->reflectionProvider,
1783-
$this->initializerExprTypeResolver,
1784-
$this->fileTypeMapper,
1785-
$this->stubPhpDocProvider,
1786-
$this->phpDocInheritanceResolver,
1787-
$this->phpVersion,
1788-
$this->signatureMapProvider,
1789-
$this->deprecationProvider,
1790-
$this->attributeReflectionFactory,
1791-
$this->classReflectionExtensionRegistryProvider,
1780+
return $this->classReflectionFactory->create(
17921781
$this->displayName,
17931782
$this->reflection,
17941783
$this->anonymousFilename,
17951784
$this->typeMapFromList($types),
17961785
$this->stubPhpDocBlock,
1797-
$this->universalObjectCratesClasses,
17981786
null,
17991787
$this->resolvedCallSiteVarianceMap,
18001788
$this->finalByKeywordOverride,
@@ -1806,23 +1794,12 @@ public function withTypes(array $types): self
18061794
*/
18071795
public function withVariances(array $variances): self
18081796
{
1809-
return new self(
1810-
$this->reflectionProvider,
1811-
$this->initializerExprTypeResolver,
1812-
$this->fileTypeMapper,
1813-
$this->stubPhpDocProvider,
1814-
$this->phpDocInheritanceResolver,
1815-
$this->phpVersion,
1816-
$this->signatureMapProvider,
1817-
$this->deprecationProvider,
1818-
$this->attributeReflectionFactory,
1819-
$this->classReflectionExtensionRegistryProvider,
1797+
return $this->classReflectionFactory->create(
18201798
$this->displayName,
18211799
$this->reflection,
18221800
$this->anonymousFilename,
18231801
$this->resolvedTemplateTypeMap,
18241802
$this->stubPhpDocBlock,
1825-
$this->universalObjectCratesClasses,
18261803
null,
18271804
$this->varianceMapFromList($variances),
18281805
$this->finalByKeywordOverride,
@@ -1844,23 +1821,12 @@ public function asFinal(): self
18441821
return $this;
18451822
}
18461823

1847-
return new self(
1848-
$this->reflectionProvider,
1849-
$this->initializerExprTypeResolver,
1850-
$this->fileTypeMapper,
1851-
$this->stubPhpDocProvider,
1852-
$this->phpDocInheritanceResolver,
1853-
$this->phpVersion,
1854-
$this->signatureMapProvider,
1855-
$this->deprecationProvider,
1856-
$this->attributeReflectionFactory,
1857-
$this->classReflectionExtensionRegistryProvider,
1824+
return $this->classReflectionFactory->create(
18581825
$this->displayName,
18591826
$this->reflection,
18601827
$this->anonymousFilename,
18611828
$this->resolvedTemplateTypeMap,
18621829
$this->stubPhpDocBlock,
1863-
$this->universalObjectCratesClasses,
18641830
null,
18651831
$this->resolvedCallSiteVarianceMap,
18661832
true,
@@ -1882,23 +1848,12 @@ public function removeFinalKeywordOverride(): self
18821848
return $this;
18831849
}
18841850

1885-
return new self(
1886-
$this->reflectionProvider,
1887-
$this->initializerExprTypeResolver,
1888-
$this->fileTypeMapper,
1889-
$this->stubPhpDocProvider,
1890-
$this->phpDocInheritanceResolver,
1891-
$this->phpVersion,
1892-
$this->signatureMapProvider,
1893-
$this->deprecationProvider,
1894-
$this->attributeReflectionFactory,
1895-
$this->classReflectionExtensionRegistryProvider,
1851+
return $this->classReflectionFactory->create(
18961852
$this->displayName,
18971853
$this->reflection,
18981854
$this->anonymousFilename,
18991855
$this->resolvedTemplateTypeMap,
19001856
$this->stubPhpDocBlock,
1901-
$this->universalObjectCratesClasses,
19021857
null,
19031858
$this->resolvedCallSiteVarianceMap,
19041859
false,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection;
4+
5+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
6+
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
7+
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
8+
use PHPStan\Type\Generic\TemplateTypeMap;
9+
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
10+
11+
interface ClassReflectionFactory
12+
{
13+
14+
public function create(
15+
string $displayName,
16+
ReflectionClass|ReflectionEnum $reflection,
17+
?string $anonymousFilename,
18+
?TemplateTypeMap $resolvedTemplateTypeMap,
19+
?ResolvedPhpDocBlock $stubPhpDocBlock,
20+
?string $extraCacheKey = null,
21+
?TemplateTypeVarianceMap $resolvedCallSiteVarianceMap = null,
22+
?bool $finalByKeywordOverride = null,
23+
): ClassReflection;
24+
25+
}

0 commit comments

Comments
 (0)