2
2
3
3
namespace PHPStan \Testing ;
4
4
5
- use Composer \Autoload \ClassLoader ;
6
- use PhpParser \PrettyPrinter \Standard ;
7
5
use PHPStan \Analyser \DirectScopeFactory ;
8
6
use PHPStan \Analyser \MutatingScope ;
9
7
use PHPStan \Analyser \NodeScopeResolver ;
10
8
use PHPStan \Analyser \ScopeFactory ;
11
9
use PHPStan \Analyser \TypeSpecifier ;
12
- use PHPStan \Analyser \TypeSpecifierFactory ;
13
10
use PHPStan \BetterReflection \Reflector \ClassReflector ;
14
11
use PHPStan \BetterReflection \Reflector \ConstantReflector ;
15
12
use PHPStan \BetterReflection \Reflector \FunctionReflector ;
16
- use PHPStan \BetterReflection \SourceLocator \Ast \Locator ;
17
- use PHPStan \BetterReflection \SourceLocator \SourceStubber \PhpStormStubsSourceStubber ;
18
- use PHPStan \BetterReflection \SourceLocator \SourceStubber \ReflectionSourceStubber ;
19
- use PHPStan \BetterReflection \SourceLocator \Type \AggregateSourceLocator ;
20
- use PHPStan \BetterReflection \SourceLocator \Type \EvaledCodeSourceLocator ;
21
- use PHPStan \BetterReflection \SourceLocator \Type \MemoizingSourceLocator ;
22
- use PHPStan \BetterReflection \SourceLocator \Type \PhpInternalSourceLocator ;
23
13
use PHPStan \DependencyInjection \Container ;
24
14
use PHPStan \DependencyInjection \ContainerFactory ;
25
15
use PHPStan \DependencyInjection \Reflection \ClassReflectionExtensionRegistryProvider ;
26
16
use PHPStan \DependencyInjection \Type \DynamicReturnTypeExtensionRegistryProvider ;
27
17
use PHPStan \DependencyInjection \Type \OperatorTypeSpecifyingExtensionRegistryProvider ;
28
18
use PHPStan \File \FileHelper ;
29
19
use PHPStan \Parser \CachedParser ;
30
- use PHPStan \Parser \PhpParserDecorator ;
31
20
use PHPStan \PhpDoc \TypeNodeResolver ;
32
21
use PHPStan \PhpDoc \TypeStringResolver ;
33
- use PHPStan \Reflection \BetterReflection \Reflector \MemoizingClassReflector ;
34
- use PHPStan \Reflection \BetterReflection \Reflector \MemoizingConstantReflector ;
35
- use PHPStan \Reflection \BetterReflection \Reflector \MemoizingFunctionReflector ;
36
- use PHPStan \Reflection \BetterReflection \SourceLocator \AutoloadSourceLocator ;
37
- use PHPStan \Reflection \BetterReflection \SourceLocator \ComposerJsonAndInstalledJsonSourceLocatorMaker ;
38
- use PHPStan \Reflection \BetterReflection \SourceLocator \FileNodesFetcher ;
39
22
use PHPStan \Reflection \ReflectionProvider ;
40
23
use PHPStan \Rules \Properties \PropertyReflectionFinder ;
41
24
use PHPStan \Type \TypeAliasResolver ;
@@ -50,12 +33,6 @@ abstract class PHPStanTestCase extends \PHPUnit\Framework\TestCase
50
33
/** @var array<string, Container> */
51
34
private static array $ containers = [];
52
35
53
- /** @var array{ClassReflector, FunctionReflector, ConstantReflector}|null */
54
- private static $ reflectors ;
55
-
56
- /** @var PhpStormStubsSourceStubber|null */
57
- private static $ phpStormStubsSourceStubber ;
58
-
59
36
/** @api */
60
37
public static function getContainer (): Container
61
38
{
@@ -118,74 +95,16 @@ public function createReflectionProvider(): ReflectionProvider
118
95
return self ::getContainer ()->getByType (ReflectionProvider::class);
119
96
}
120
97
121
- private static function getPhpStormStubsSourceStubber (): PhpStormStubsSourceStubber
122
- {
123
- if (self ::$ phpStormStubsSourceStubber === null ) {
124
- self ::$ phpStormStubsSourceStubber = self ::getContainer ()->getByType (PhpStormStubsSourceStubber::class);
125
- }
126
-
127
- return self ::$ phpStormStubsSourceStubber ;
128
- }
129
-
130
98
/**
131
99
* @return array{ClassReflector, FunctionReflector, ConstantReflector}
132
100
*/
133
101
public static function getReflectors (): array
134
102
{
135
- if (self ::$ reflectors !== null ) {
136
- return self ::$ reflectors ;
137
- }
138
-
139
- if (!class_exists (ClassLoader::class)) {
140
- self ::fail ('Composer ClassLoader is unknown ' );
141
- }
142
-
143
- $ classLoaderReflection = new \ReflectionClass (ClassLoader::class);
144
- if ($ classLoaderReflection ->getFileName () === false ) {
145
- self ::fail ('Unknown ClassLoader filename ' );
146
- }
147
-
148
- $ composerProjectPath = dirname ($ classLoaderReflection ->getFileName (), 3 );
149
- if (!is_file ($ composerProjectPath . '/composer.json ' )) {
150
- self ::fail (sprintf ('composer.json not found in directory %s ' , $ composerProjectPath ));
151
- }
152
-
153
- $ composerJsonAndInstalledJsonSourceLocatorMaker = self ::getContainer ()->getByType (ComposerJsonAndInstalledJsonSourceLocatorMaker::class);
154
- $ composerSourceLocator = $ composerJsonAndInstalledJsonSourceLocatorMaker ->create ($ composerProjectPath );
155
- if ($ composerSourceLocator === null ) {
156
- self ::fail ('Could not create composer source locator ' );
157
- }
158
-
159
- // these need to be synced with TestCase-staticReflection.neon file and TestCaseSourceLocatorFactory
160
-
161
- $ locators = [
162
- $ composerSourceLocator ,
103
+ return [
104
+ self ::getContainer ()->getService ('betterReflectionClassReflector ' ),
105
+ self ::getContainer ()->getService ('betterReflectionFunctionReflector ' ),
106
+ self ::getContainer ()->getService ('betterReflectionConstantReflector ' ),
163
107
];
164
-
165
- $ phpParser = new PhpParserDecorator (self ::getContainer ()->getByType (CachedParser::class));
166
-
167
- /** @var FunctionReflector $functionReflector */
168
- $ functionReflector = null ;
169
- $ astLocator = new Locator ($ phpParser , static function () use (&$ functionReflector ): FunctionReflector {
170
- return $ functionReflector ;
171
- });
172
- $ astPhp8Locator = new Locator (self ::getContainer ()->getService ('php8PhpParser ' ), static function () use (&$ functionReflector ): FunctionReflector {
173
- return $ functionReflector ;
174
- });
175
- $ reflectionSourceStubber = new ReflectionSourceStubber ();
176
- $ locators [] = new PhpInternalSourceLocator ($ astPhp8Locator , self ::getPhpStormStubsSourceStubber ());
177
- $ locators [] = new AutoloadSourceLocator (self ::getContainer ()->getByType (FileNodesFetcher::class));
178
- $ locators [] = new PhpInternalSourceLocator ($ astLocator , $ reflectionSourceStubber );
179
- $ locators [] = new EvaledCodeSourceLocator ($ astLocator , $ reflectionSourceStubber );
180
- $ sourceLocator = new MemoizingSourceLocator (new AggregateSourceLocator ($ locators ));
181
-
182
- $ classReflector = new MemoizingClassReflector ($ sourceLocator );
183
- $ functionReflector = new MemoizingFunctionReflector ($ sourceLocator , $ classReflector );
184
- $ constantReflector = new MemoizingConstantReflector ($ sourceLocator , $ classReflector );
185
-
186
- self ::$ reflectors = [$ classReflector , $ functionReflector , $ constantReflector ];
187
-
188
- return self ::$ reflectors ;
189
108
}
190
109
191
110
public function getClassReflectionExtensionRegistryProvider (): ClassReflectionExtensionRegistryProvider
@@ -233,30 +152,6 @@ protected function shouldTreatPhpDocTypesAsCertain(): bool
233
152
return true ;
234
153
}
235
154
236
- /**
237
- * @param \PhpParser\PrettyPrinter\Standard $printer
238
- * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider
239
- * @param \PHPStan\Type\MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions
240
- * @param \PHPStan\Type\StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions
241
- * @return \PHPStan\Analyser\TypeSpecifier
242
- */
243
- public function createTypeSpecifier (
244
- Standard $ printer ,
245
- ReflectionProvider $ reflectionProvider ,
246
- array $ methodTypeSpecifyingExtensions = [],
247
- array $ staticMethodTypeSpecifyingExtensions = []
248
- ): TypeSpecifier
249
- {
250
- return new TypeSpecifier (
251
- $ printer ,
252
- $ reflectionProvider ,
253
- true ,
254
- self ::getContainer ()->getServicesByTag (TypeSpecifierFactory::FUNCTION_TYPE_SPECIFYING_EXTENSION_TAG ),
255
- array_merge ($ methodTypeSpecifyingExtensions , self ::getContainer ()->getServicesByTag (TypeSpecifierFactory::METHOD_TYPE_SPECIFYING_EXTENSION_TAG )),
256
- array_merge ($ staticMethodTypeSpecifyingExtensions , self ::getContainer ()->getServicesByTag (TypeSpecifierFactory::STATIC_METHOD_TYPE_SPECIFYING_EXTENSION_TAG ))
257
- );
258
- }
259
-
260
155
public function getFileHelper (): FileHelper
261
156
{
262
157
return self ::getContainer ()->getByType (FileHelper::class);
0 commit comments