Skip to content

Commit 1cc337a

Browse files
committed
Introduce new simple ScopeFactory
1 parent 69d7e81 commit 1cc337a

9 files changed

+82
-65
lines changed

conf/config.neon

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,14 @@ services:
590590
reportUnmatchedIgnoredErrors: %reportUnmatchedIgnoredErrors%
591591

592592
-
593-
class: PHPStan\Analyser\LazyScopeFactory
593+
class: PHPStan\Analyser\LazyInternalScopeFactory
594594
arguments:
595595
scopeClass: %scopeClass%
596596
autowired:
597-
- PHPStan\Analyser\ScopeFactory
597+
- PHPStan\Analyser\InternalScopeFactory
598+
599+
-
600+
class: PHPStan\Analyser\ScopeFactory
598601

599602
-
600603
class: PHPStan\Analyser\NodeScopeResolver

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ parameters:
33
-
44
message: "#^Function is_a\\(\\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#"
55
count: 1
6-
path: src/Analyser/DirectScopeFactory.php
6+
path: src/Analyser/DirectInternalScopeFactory.php
77

88
-
99
message: "#^Cannot assign offset 'realCount' to array\\|string\\.$#"
@@ -13,7 +13,7 @@ parameters:
1313
-
1414
message: "#^Function is_a\\(\\) is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine\\. Use objects retrieved from ReflectionProvider instead\\.$#"
1515
count: 1
16-
path: src/Analyser/LazyScopeFactory.php
16+
path: src/Analyser/LazyInternalScopeFactory.php
1717

1818
-
1919
message: """

src/Analyser/DirectScopeFactory.php renamed to src/Analyser/DirectInternalScopeFactory.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
use PHPStan\ShouldNotHappenException;
1616
use function is_a;
1717

18-
/**
19-
* @internal
20-
*/
21-
class DirectScopeFactory implements ScopeFactory
18+
class DirectInternalScopeFactory implements InternalScopeFactory
2219
{
2320

2421
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Analyser;
4+
5+
use PHPStan\Reflection\FunctionReflection;
6+
use PHPStan\Reflection\MethodReflection;
7+
use PHPStan\Reflection\ParametersAcceptor;
8+
9+
interface InternalScopeFactory
10+
{
11+
12+
/**
13+
* @param ExpressionTypeHolder[] $expressionTypes
14+
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
15+
* @param array<string, true> $currentlyAssignedExpressions
16+
* @param array<string, true> $currentlyAllowedUndefinedExpressions
17+
* @param ExpressionTypeHolder[] $nativeExpressionTypes
18+
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
19+
*/
20+
public function create(
21+
ScopeContext $context,
22+
bool $declareStrictTypes = false,
23+
FunctionReflection|MethodReflection|null $function = null,
24+
?string $namespace = null,
25+
array $expressionTypes = [],
26+
array $conditionalExpressions = [],
27+
?string $inClosureBindScopeClass = null,
28+
?ParametersAcceptor $anonymousFunctionReflection = null,
29+
bool $inFirstLevelStatement = true,
30+
array $currentlyAssignedExpressions = [],
31+
array $currentlyAllowedUndefinedExpressions = [],
32+
array $nativeExpressionTypes = [],
33+
array $inFunctionCallsStack = [],
34+
bool $afterExtractCall = false,
35+
?Scope $parentScope = null,
36+
bool $nativeTypesPromoted = false,
37+
): MutatingScope;
38+
39+
}

src/Analyser/LazyScopeFactory.php renamed to src/Analyser/LazyInternalScopeFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPStan\ShouldNotHappenException;
1616
use function is_a;
1717

18-
class LazyScopeFactory implements ScopeFactory
18+
class LazyInternalScopeFactory implements InternalScopeFactory
1919
{
2020

2121
private bool $treatPhpDocTypesAsCertain;

src/Analyser/MutatingScope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class MutatingScope implements Scope
162162
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
163163
*/
164164
public function __construct(
165-
private ScopeFactory $scopeFactory,
165+
private InternalScopeFactory $scopeFactory,
166166
private ReflectionProvider $reflectionProvider,
167167
private InitializerExprTypeResolver $initializerExprTypeResolver,
168168
private DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry,

src/Analyser/ScopeFactory.php

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,17 @@
22

33
namespace PHPStan\Analyser;
44

5-
use PHPStan\Reflection\FunctionReflection;
6-
use PHPStan\Reflection\MethodReflection;
7-
use PHPStan\Reflection\ParametersAcceptor;
8-
9-
interface ScopeFactory
5+
/** @api */
6+
class ScopeFactory
107
{
118

12-
/**
13-
* @api
14-
* @param ExpressionTypeHolder[] $expressionTypes
15-
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
16-
* @param array<string, true> $currentlyAssignedExpressions
17-
* @param array<string, true> $currentlyAllowedUndefinedExpressions
18-
* @param ExpressionTypeHolder[] $nativeExpressionTypes
19-
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
20-
*
21-
*/
22-
public function create(
23-
ScopeContext $context,
24-
bool $declareStrictTypes = false,
25-
FunctionReflection|MethodReflection|null $function = null,
26-
?string $namespace = null,
27-
array $expressionTypes = [],
28-
array $conditionalExpressions = [],
29-
?string $inClosureBindScopeClass = null,
30-
?ParametersAcceptor $anonymousFunctionReflection = null,
31-
bool $inFirstLevelStatement = true,
32-
array $currentlyAssignedExpressions = [],
33-
array $currentlyAllowedUndefinedExpressions = [],
34-
array $nativeExpressionTypes = [],
35-
array $inFunctionCallsStack = [],
36-
bool $afterExtractCall = false,
37-
?Scope $parentScope = null,
38-
bool $nativeTypesPromoted = false,
39-
): MutatingScope;
9+
public function __construct(private InternalScopeFactory $internalScopeFactory)
10+
{
11+
}
12+
13+
public function create(ScopeContext $context): MutatingScope
14+
{
15+
return $this->internalScopeFactory->create($context);
16+
}
4017

4118
}

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,11 @@ private function inferAndCachePropertyTypes(
942942
$namespace = implode('\\', array_slice($classNameParts, 0, -1));
943943
}
944944

945-
$classScope = $this->scopeFactory->create(
946-
ScopeContext::create($fileName),
947-
false,
948-
$constructor,
949-
$namespace,
950-
)->enterClass($declaringClass);
945+
$classScope = $this->scopeFactory->create(ScopeContext::create($fileName));
946+
if ($namespace !== null) {
947+
$classScope = $classScope->enterNamespace($namespace);
948+
}
949+
$classScope = $classScope->enterClass($declaringClass);
951950
[$templateTypeMap, $phpDocParameterTypes, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->nodeScopeResolver->getPhpDocs($classScope, $methodNode);
952951
$methodScope = $classScope->enterClassMethod(
953952
$methodNode,

src/Testing/PHPStanTestCase.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PHPStan\Testing;
44

55
use PHPStan\Analyser\ConstantResolver;
6-
use PHPStan\Analyser\DirectScopeFactory;
6+
use PHPStan\Analyser\DirectInternalScopeFactory;
77
use PHPStan\Analyser\Error;
88
use PHPStan\Analyser\MutatingScope;
99
use PHPStan\Analyser\NodeScopeResolver;
@@ -164,21 +164,23 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
164164
$reflectionProviderProvider = new DirectReflectionProviderProvider($reflectionProvider);
165165
$constantResolver = new ConstantResolver($reflectionProviderProvider, $dynamicConstantNames);
166166

167-
return new DirectScopeFactory(
168-
MutatingScope::class,
169-
$reflectionProvider,
170-
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)),
171-
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
172-
$container->getByType(ExprPrinter::class),
173-
$typeSpecifier,
174-
new PropertyReflectionFinder(),
175-
$this->getParser(),
176-
$container->getByType(NodeScopeResolver::class),
177-
$this->shouldTreatPhpDocTypesAsCertain(),
178-
$container->getByType(PhpVersion::class),
179-
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
180-
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
181-
$constantResolver,
167+
return new ScopeFactory(
168+
new DirectInternalScopeFactory(
169+
MutatingScope::class,
170+
$reflectionProvider,
171+
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)),
172+
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
173+
$container->getByType(ExprPrinter::class),
174+
$typeSpecifier,
175+
new PropertyReflectionFinder(),
176+
$this->getParser(),
177+
$container->getByType(NodeScopeResolver::class),
178+
$this->shouldTreatPhpDocTypesAsCertain(),
179+
$container->getByType(PhpVersion::class),
180+
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
181+
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
182+
$constantResolver,
183+
),
182184
);
183185
}
184186

0 commit comments

Comments
 (0)