Skip to content

Commit

Permalink
Introduce new simple ScopeFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 31, 2022
1 parent 69d7e81 commit 1cc337a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 65 deletions.
7 changes: 5 additions & 2 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -590,11 +590,14 @@ services:
reportUnmatchedIgnoredErrors: %reportUnmatchedIgnoredErrors%

-
class: PHPStan\Analyser\LazyScopeFactory
class: PHPStan\Analyser\LazyInternalScopeFactory
arguments:
scopeClass: %scopeClass%
autowired:
- PHPStan\Analyser\ScopeFactory
- PHPStan\Analyser\InternalScopeFactory

-
class: PHPStan\Analyser\ScopeFactory

-
class: PHPStan\Analyser\NodeScopeResolver
Expand Down
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ parameters:
-
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\\.$#"
count: 1
path: src/Analyser/DirectScopeFactory.php
path: src/Analyser/DirectInternalScopeFactory.php

-
message: "#^Cannot assign offset 'realCount' to array\\|string\\.$#"
Expand All @@ -13,7 +13,7 @@ parameters:
-
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\\.$#"
count: 1
path: src/Analyser/LazyScopeFactory.php
path: src/Analyser/LazyInternalScopeFactory.php

-
message: """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
use PHPStan\ShouldNotHappenException;
use function is_a;

/**
* @internal
*/
class DirectScopeFactory implements ScopeFactory
class DirectInternalScopeFactory implements InternalScopeFactory
{

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Analyser/InternalScopeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;

interface InternalScopeFactory
{

/**
* @param ExpressionTypeHolder[] $expressionTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param array<string, true> $currentlyAssignedExpressions
* @param array<string, true> $currentlyAllowedUndefinedExpressions
* @param ExpressionTypeHolder[] $nativeExpressionTypes
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
*/
public function create(
ScopeContext $context,
bool $declareStrictTypes = false,
FunctionReflection|MethodReflection|null $function = null,
?string $namespace = null,
array $expressionTypes = [],
array $conditionalExpressions = [],
?string $inClosureBindScopeClass = null,
?ParametersAcceptor $anonymousFunctionReflection = null,
bool $inFirstLevelStatement = true,
array $currentlyAssignedExpressions = [],
array $currentlyAllowedUndefinedExpressions = [],
array $nativeExpressionTypes = [],
array $inFunctionCallsStack = [],
bool $afterExtractCall = false,
?Scope $parentScope = null,
bool $nativeTypesPromoted = false,
): MutatingScope;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPStan\ShouldNotHappenException;
use function is_a;

class LazyScopeFactory implements ScopeFactory
class LazyInternalScopeFactory implements InternalScopeFactory
{

private bool $treatPhpDocTypesAsCertain;
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class MutatingScope implements Scope
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
*/
public function __construct(
private ScopeFactory $scopeFactory,
private InternalScopeFactory $scopeFactory,
private ReflectionProvider $reflectionProvider,
private InitializerExprTypeResolver $initializerExprTypeResolver,
private DynamicReturnTypeExtensionRegistry $dynamicReturnTypeExtensionRegistry,
Expand Down
43 changes: 10 additions & 33 deletions src/Analyser/ScopeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,17 @@

namespace PHPStan\Analyser;

use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;

interface ScopeFactory
/** @api */
class ScopeFactory
{

/**
* @api
* @param ExpressionTypeHolder[] $expressionTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
* @param array<string, true> $currentlyAssignedExpressions
* @param array<string, true> $currentlyAllowedUndefinedExpressions
* @param ExpressionTypeHolder[] $nativeExpressionTypes
* @param array<MethodReflection|FunctionReflection> $inFunctionCallsStack
*
*/
public function create(
ScopeContext $context,
bool $declareStrictTypes = false,
FunctionReflection|MethodReflection|null $function = null,
?string $namespace = null,
array $expressionTypes = [],
array $conditionalExpressions = [],
?string $inClosureBindScopeClass = null,
?ParametersAcceptor $anonymousFunctionReflection = null,
bool $inFirstLevelStatement = true,
array $currentlyAssignedExpressions = [],
array $currentlyAllowedUndefinedExpressions = [],
array $nativeExpressionTypes = [],
array $inFunctionCallsStack = [],
bool $afterExtractCall = false,
?Scope $parentScope = null,
bool $nativeTypesPromoted = false,
): MutatingScope;
public function __construct(private InternalScopeFactory $internalScopeFactory)
{
}

public function create(ScopeContext $context): MutatingScope
{
return $this->internalScopeFactory->create($context);
}

}
11 changes: 5 additions & 6 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,11 @@ private function inferAndCachePropertyTypes(
$namespace = implode('\\', array_slice($classNameParts, 0, -1));
}

$classScope = $this->scopeFactory->create(
ScopeContext::create($fileName),
false,
$constructor,
$namespace,
)->enterClass($declaringClass);
$classScope = $this->scopeFactory->create(ScopeContext::create($fileName));
if ($namespace !== null) {
$classScope = $classScope->enterNamespace($namespace);
}
$classScope = $classScope->enterClass($declaringClass);
[$templateTypeMap, $phpDocParameterTypes, $phpDocReturnType, $phpDocThrowType, $deprecatedDescription, $isDeprecated, $isInternal, $isFinal, $isPure, $acceptsNamedArguments, , $phpDocComment, $asserts, $selfOutType, $phpDocParameterOutTypes] = $this->nodeScopeResolver->getPhpDocs($classScope, $methodNode);
$methodScope = $classScope->enterClassMethod(
$methodNode,
Expand Down
34 changes: 18 additions & 16 deletions src/Testing/PHPStanTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace PHPStan\Testing;

use PHPStan\Analyser\ConstantResolver;
use PHPStan\Analyser\DirectScopeFactory;
use PHPStan\Analyser\DirectInternalScopeFactory;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
Expand Down Expand Up @@ -164,21 +164,23 @@ public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeS
$reflectionProviderProvider = new DirectReflectionProviderProvider($reflectionProvider);
$constantResolver = new ConstantResolver($reflectionProviderProvider, $dynamicConstantNames);

return new DirectScopeFactory(
MutatingScope::class,
$reflectionProvider,
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)),
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
$container->getByType(ExprPrinter::class),
$typeSpecifier,
new PropertyReflectionFinder(),
$this->getParser(),
$container->getByType(NodeScopeResolver::class),
$this->shouldTreatPhpDocTypesAsCertain(),
$container->getByType(PhpVersion::class),
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
$constantResolver,
return new ScopeFactory(
new DirectInternalScopeFactory(
MutatingScope::class,
$reflectionProvider,
new InitializerExprTypeResolver($constantResolver, $reflectionProviderProvider, new PhpVersion(PHP_VERSION_ID), $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class)),
$container->getByType(DynamicReturnTypeExtensionRegistryProvider::class),
$container->getByType(ExprPrinter::class),
$typeSpecifier,
new PropertyReflectionFinder(),
$this->getParser(),
$container->getByType(NodeScopeResolver::class),
$this->shouldTreatPhpDocTypesAsCertain(),
$container->getByType(PhpVersion::class),
$container->getParameter('featureToggles')['explicitMixedInUnknownGenericNew'],
$container->getParameter('featureToggles')['explicitMixedForGlobalVariables'],
$constantResolver,
),
);
}

Expand Down

0 comments on commit 1cc337a

Please sign in to comment.