diff --git a/src/Broker/Broker.php b/src/Broker/Broker.php index 4422e22e05..86dc087974 100644 --- a/src/Broker/Broker.php +++ b/src/Broker/Broker.php @@ -25,7 +25,7 @@ class Broker implements ReflectionProvider /** @var string[] */ private array $universalObjectCratesClasses; - private static ?\PHPStan\Broker\Broker $instance = null; + private static ?Broker $instance = null; /** * @param \PHPStan\Reflection\ReflectionProvider $reflectionProvider @@ -46,9 +46,9 @@ public function __construct( $this->universalObjectCratesClasses = $universalObjectCratesClasses; } - public static function registerInstance(Broker $reflectionProvider): void + public static function registerInstance(Broker $broker): void { - self::$instance = $reflectionProvider; + self::$instance = $broker; } public static function getInstance(): Broker diff --git a/src/Testing/PHPStanTestCase.php b/src/Testing/PHPStanTestCase.php index 22ab3fa362..3f65cdc23f 100644 --- a/src/Testing/PHPStanTestCase.php +++ b/src/Testing/PHPStanTestCase.php @@ -30,6 +30,8 @@ use PHPStan\DependencyInjection\Reflection\DirectClassReflectionExtensionRegistryProvider; use PHPStan\DependencyInjection\Type\DirectDynamicReturnTypeExtensionRegistryProvider; use PHPStan\DependencyInjection\Type\DirectOperatorTypeSpecifyingExtensionRegistryProvider; +use PHPStan\DependencyInjection\Type\DynamicReturnTypeExtensionRegistryProvider; +use PHPStan\DependencyInjection\Type\OperatorTypeSpecifyingExtensionRegistryProvider; use PHPStan\File\FileHelper; use PHPStan\File\SimpleRelativePathHelper; use PHPStan\Parser\CachedParser; @@ -143,24 +145,34 @@ public function getParser(): \PHPStan\Parser\Parser /** * @api - * @param \PHPStan\Type\DynamicMethodReturnTypeExtension[] $dynamicMethodReturnTypeExtensions - * @param \PHPStan\Type\DynamicStaticMethodReturnTypeExtension[] $dynamicStaticMethodReturnTypeExtensions - * @return \PHPStan\Broker\Broker + * @return \PHPStan\Reflection\ReflectionProvider */ - public function createBroker( - array $dynamicMethodReturnTypeExtensions = [], - array $dynamicStaticMethodReturnTypeExtensions = [] - ): Broker + public function createBroker(): ReflectionProvider { + return $this->createReflectionProvider(); + } + + /** @api */ + public function createReflectionProvider(): ReflectionProvider + { + $setterReflectionProviderProvider = new ReflectionProvider\SetterReflectionProviderProvider(); + $staticReflectionProvider = $this->createStaticReflectionProvider($setterReflectionProviderProvider); + $reflectionProvider = $this->createReflectionProviderByParameters( + $this->createRuntimeReflectionProvider($setterReflectionProviderProvider), + $staticReflectionProvider, + self::$useStaticReflectionProvider + ); + $setterReflectionProviderProvider->setReflectionProvider($reflectionProvider); + $dynamicReturnTypeExtensionRegistryProvider = new DirectDynamicReturnTypeExtensionRegistryProvider( - array_merge(self::getContainer()->getServicesByTag(BrokerFactory::DYNAMIC_METHOD_RETURN_TYPE_EXTENSION_TAG), $dynamicMethodReturnTypeExtensions, $this->getDynamicMethodReturnTypeExtensions()), - array_merge(self::getContainer()->getServicesByTag(BrokerFactory::DYNAMIC_STATIC_METHOD_RETURN_TYPE_EXTENSION_TAG), $dynamicStaticMethodReturnTypeExtensions, $this->getDynamicStaticMethodReturnTypeExtensions()), + array_merge(self::getContainer()->getServicesByTag(BrokerFactory::DYNAMIC_METHOD_RETURN_TYPE_EXTENSION_TAG), $this->getDynamicMethodReturnTypeExtensions()), + array_merge(self::getContainer()->getServicesByTag(BrokerFactory::DYNAMIC_STATIC_METHOD_RETURN_TYPE_EXTENSION_TAG), $this->getDynamicStaticMethodReturnTypeExtensions()), array_merge(self::getContainer()->getServicesByTag(BrokerFactory::DYNAMIC_FUNCTION_RETURN_TYPE_EXTENSION_TAG), $this->getDynamicFunctionReturnTypeExtensions()) ); $operatorTypeSpecifyingExtensionRegistryProvider = new DirectOperatorTypeSpecifyingExtensionRegistryProvider( $this->getOperatorTypeSpecifyingExtensions() ); - $reflectionProvider = $this->createReflectionProvider(); + $broker = new Broker( $reflectionProvider, $dynamicReturnTypeExtensionRegistryProvider, @@ -172,21 +184,6 @@ public function createBroker( $operatorTypeSpecifyingExtensionRegistryProvider->setBroker($broker); $this->getClassReflectionExtensionRegistryProvider()->setBroker($broker); - return $broker; - } - - /** @api */ - public function createReflectionProvider(): ReflectionProvider - { - $setterReflectionProviderProvider = new ReflectionProvider\SetterReflectionProviderProvider(); - $staticReflectionProvider = $this->createStaticReflectionProvider($setterReflectionProviderProvider); - $reflectionProvider = $this->createReflectionProviderByParameters( - $this->createRuntimeReflectionProvider($setterReflectionProviderProvider), - $staticReflectionProvider, - self::$useStaticReflectionProvider - ); - $setterReflectionProviderProvider->setReflectionProvider($reflectionProvider); - return $reflectionProvider; } @@ -563,15 +560,15 @@ public function getClassReflectionExtensionRegistryProvider(): DirectClassReflec return $this->classReflectionExtensionRegistryProvider; } - public function createScopeFactory(Broker $broker, TypeSpecifier $typeSpecifier): ScopeFactory + public function createScopeFactory(ReflectionProvider $reflectionProvider, TypeSpecifier $typeSpecifier): ScopeFactory { $container = self::getContainer(); return new DirectScopeFactory( MutatingScope::class, - $broker, - $broker->getDynamicReturnTypeExtensionRegistryProvider(), - $broker->getOperatorTypeSpecifyingExtensionRegistryProvider(), + $reflectionProvider, + $container->getByType(DynamicReturnTypeExtensionRegistryProvider::class), + $container->getByType(OperatorTypeSpecifyingExtensionRegistryProvider::class), new \PhpParser\PrettyPrinter\Standard(), $typeSpecifier, new PropertyReflectionFinder(), diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index 8225650da6..30bcdcbdb9 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -10,7 +10,6 @@ use PHPStan\Analyser\Scope; use PHPStan\Analyser\ScopeContext; use PHPStan\Broker\AnonymousClassNameHelper; -use PHPStan\Broker\Broker; use PHPStan\Cache\Cache; use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider; use PHPStan\File\FileHelper; @@ -22,8 +21,6 @@ use PHPStan\PhpDoc\StubPhpDocProvider; use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider; use PHPStan\TrinaryLogic; -use PHPStan\Type\DynamicMethodReturnTypeExtension; -use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\FileTypeMapper; use PHPStan\Type\VerbosityLevel; @@ -34,8 +31,6 @@ abstract class TypeInferenceTestCase extends \PHPStan\Testing\PHPStanTestCase /** * @param string $file * @param callable(\PhpParser\Node, \PHPStan\Analyser\Scope): void $callback - * @param DynamicMethodReturnTypeExtension[] $dynamicMethodReturnTypeExtensions - * @param DynamicStaticMethodReturnTypeExtension[] $dynamicStaticMethodReturnTypeExtensions * @param \PHPStan\Type\MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions * @param \PHPStan\Type\StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions * @param string[] $dynamicConstantNames @@ -43,8 +38,6 @@ abstract class TypeInferenceTestCase extends \PHPStan\Testing\PHPStanTestCase public function processFile( string $file, callable $callback, - array $dynamicMethodReturnTypeExtensions = [], - array $dynamicStaticMethodReturnTypeExtensions = [], array $methodTypeSpecifyingExtensions = [], array $staticMethodTypeSpecifyingExtensions = [], array $dynamicConstantNames = [] @@ -54,15 +47,14 @@ public function processFile( $phpDocNodeResolver = self::getContainer()->getByType(PhpDocNodeResolver::class); $printer = new \PhpParser\PrettyPrinter\Standard(); - $broker = $this->createBroker($dynamicMethodReturnTypeExtensions, $dynamicStaticMethodReturnTypeExtensions); - Broker::registerInstance($broker); - $typeSpecifier = $this->createTypeSpecifier($printer, $broker, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions); + $reflectionProvider = $this->createReflectionProvider(); + $typeSpecifier = $this->createTypeSpecifier($printer, $reflectionProvider, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions); $currentWorkingDirectory = $this->getCurrentWorkingDirectory(); $fileHelper = new FileHelper($currentWorkingDirectory); - $fileTypeMapper = new FileTypeMapper(new DirectReflectionProviderProvider($broker), $this->getParser(), $phpDocStringResolver, $phpDocNodeResolver, $this->createMock(Cache::class), new AnonymousClassNameHelper($fileHelper, new SimpleRelativePathHelper($currentWorkingDirectory))); + $fileTypeMapper = new FileTypeMapper(new DirectReflectionProviderProvider($reflectionProvider), $this->getParser(), $phpDocStringResolver, $phpDocNodeResolver, $this->createMock(Cache::class), new AnonymousClassNameHelper($fileHelper, new SimpleRelativePathHelper($currentWorkingDirectory))); $phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper); $resolver = new NodeScopeResolver( - $broker, + $reflectionProvider, self::getReflectors()[0], $this->getClassReflectionExtensionRegistryProvider(), $this->getParser(), @@ -83,7 +75,7 @@ public function processFile( return $fileHelper->normalizePath($file); }, array_merge([$file], $this->getAdditionalAnalysedFiles()))); - $scopeFactory = $this->createScopeFactory($broker, $typeSpecifier); + $scopeFactory = $this->createScopeFactory($reflectionProvider, $typeSpecifier); if (count($dynamicConstantNames) > 0) { $reflectionProperty = new \ReflectionProperty(DirectScopeFactory::class, 'dynamicConstantNames'); $reflectionProperty->setAccessible(true); diff --git a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php index 6b12af562f..05b93d6684 100644 --- a/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php @@ -1466,8 +1466,6 @@ public function testVarAnnotations( $expression, [], [], - [], - [], 'die', [], false @@ -3434,8 +3432,6 @@ public function testLiteralArraysKeys( '$key', [], [], - [], - [], $evaluatedPointExpressionType ); } @@ -4188,8 +4184,6 @@ public function testSwitchGetClass( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -4587,8 +4581,6 @@ public function testOverwritingVariable( $expression, [], [], - [], - [], $evaluatedPointExpressionType ); } @@ -4931,8 +4923,6 @@ public function testForeachObjectType( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -6451,8 +6441,6 @@ public function testTypeSpecifyingExtensions( __DIR__ . '/data/type-specifying-extensions.php', $description, $expression, - [], - [], [new AssertionClassMethodTypeSpecifyingExtension($nullContext)], [new AssertionClassStaticMethodTypeSpecifyingExtension($nullContext)], 'die', @@ -6513,8 +6501,6 @@ public function testTypeSpecifyingExtensions2( __DIR__ . '/data/type-specifying-extensions2.php', $description, $expression, - [], - [], [new AssertionClassMethodTypeSpecifyingExtension($nullContext)], [new AssertionClassStaticMethodTypeSpecifyingExtension($nullContext)] ); @@ -6572,8 +6558,6 @@ public function testTypeSpecifyingExtensions3( __DIR__ . '/data/type-specifying-extensions3.php', $description, $expression, - [], - [], [new AssertionClassMethodTypeSpecifyingExtension($nullContext)], [new AssertionClassStaticMethodTypeSpecifyingExtension($nullContext)], 'die', @@ -7114,8 +7098,6 @@ public function testTypeElimination( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -7772,8 +7754,6 @@ public function testForeachLoopVariables( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -7797,8 +7777,6 @@ public function testWhileLoopVariables( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -7822,8 +7800,6 @@ public function testForLoopVariables( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -7938,8 +7914,6 @@ public function testDoWhileLoopVariables( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -7978,8 +7952,6 @@ public function testMultipleClassesInOneFile( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -8483,8 +8455,6 @@ public function testClosureWithUsePassedByReference( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -8587,8 +8557,6 @@ public function testClosureWithUsePassedByReferenceReturn( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -8623,8 +8591,6 @@ public function testClosureWithInferredTypehint( $expression, [], [], - [], - [], 'die', [], false @@ -8928,8 +8894,6 @@ public function testSpecifiedFunctionCall( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -9092,8 +9056,6 @@ public function testConstantTypeAfterDuplicateCondition( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -9152,8 +9114,6 @@ public function testAnonymousClassName( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -9227,8 +9187,6 @@ public function testDynamicConstants( $expression, [], [], - [], - [], 'die', [ 'DynamicConstants\\DynamicConstantClass::DYNAMIC_CONSTANT_IN_CLASS', @@ -9369,8 +9327,6 @@ public function testPropertyArrayAssignment( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -9506,8 +9462,6 @@ public function testGetParentClass( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -9546,8 +9500,6 @@ public function testIsCountable( $expression, [], [], - [], - [], $evaluatedPointExpression ); } @@ -10366,8 +10318,6 @@ public function testTryCatchScope( $expression, [], [], - [], - [], $evaluatedPointExpression, [], false @@ -10378,8 +10328,6 @@ public function testTryCatchScope( * @param string $file * @param string $description * @param string $expression - * @param DynamicMethodReturnTypeExtension[] $dynamicMethodReturnTypeExtensions - * @param DynamicStaticMethodReturnTypeExtension[] $dynamicStaticMethodReturnTypeExtensions * @param \PHPStan\Type\MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions * @param \PHPStan\Type\StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions * @param string $evaluatedPointExpression @@ -10390,8 +10338,6 @@ private function assertTypes( string $file, string $description, string $expression, - array $dynamicMethodReturnTypeExtensions = [], - array $dynamicStaticMethodReturnTypeExtensions = [], array $methodTypeSpecifyingExtensions = [], array $staticMethodTypeSpecifyingExtensions = [], string $evaluatedPointExpression = 'die', @@ -10429,8 +10375,6 @@ static function (\PhpParser\Node $node, Scope $scope) use ($file, $evaluatedPoin $assertType($scope); }, - $dynamicMethodReturnTypeExtensions, - $dynamicStaticMethodReturnTypeExtensions, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions, $dynamicConstantNames