|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Testing; |
| 4 | + |
| 5 | +use PHPStan\Analyser\DirectScopeFactory; |
| 6 | +use PHPStan\Analyser\NodeScopeResolver; |
| 7 | +use PHPStan\Analyser\ScopeContext; |
| 8 | +use PHPStan\Broker\AnonymousClassNameHelper; |
| 9 | +use PHPStan\Broker\Broker; |
| 10 | +use PHPStan\Cache\Cache; |
| 11 | +use PHPStan\File\FileHelper; |
| 12 | +use PHPStan\File\SimpleRelativePathHelper; |
| 13 | +use PHPStan\Php\PhpVersion; |
| 14 | +use PHPStan\PhpDoc\PhpDocInheritanceResolver; |
| 15 | +use PHPStan\PhpDoc\PhpDocNodeResolver; |
| 16 | +use PHPStan\PhpDoc\PhpDocStringResolver; |
| 17 | +use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider; |
| 18 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 19 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 20 | +use PHPStan\Type\FileTypeMapper; |
| 21 | + |
| 22 | +abstract class TypeInferenceTest extends \PHPStan\Testing\TestCase |
| 23 | +{ |
| 24 | + |
| 25 | + /** @var bool */ |
| 26 | + protected $polluteCatchScopeWithTryAssignments = true; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param string $file |
| 30 | + * @param callable(\PhpParser\Node, \PHPStan\Analyser\Scope): void $callback |
| 31 | + * @param DynamicMethodReturnTypeExtension[] $dynamicMethodReturnTypeExtensions |
| 32 | + * @param DynamicStaticMethodReturnTypeExtension[] $dynamicStaticMethodReturnTypeExtensions |
| 33 | + * @param \PHPStan\Type\MethodTypeSpecifyingExtension[] $methodTypeSpecifyingExtensions |
| 34 | + * @param \PHPStan\Type\StaticMethodTypeSpecifyingExtension[] $staticMethodTypeSpecifyingExtensions |
| 35 | + * @param string[] $dynamicConstantNames |
| 36 | + */ |
| 37 | + public function processFile( |
| 38 | + string $file, |
| 39 | + callable $callback, |
| 40 | + array $dynamicMethodReturnTypeExtensions = [], |
| 41 | + array $dynamicStaticMethodReturnTypeExtensions = [], |
| 42 | + array $methodTypeSpecifyingExtensions = [], |
| 43 | + array $staticMethodTypeSpecifyingExtensions = [], |
| 44 | + array $dynamicConstantNames = [] |
| 45 | + ): void |
| 46 | + { |
| 47 | + $phpDocStringResolver = self::getContainer()->getByType(PhpDocStringResolver::class); |
| 48 | + $phpDocNodeResolver = self::getContainer()->getByType(PhpDocNodeResolver::class); |
| 49 | + |
| 50 | + $printer = new \PhpParser\PrettyPrinter\Standard(); |
| 51 | + $broker = $this->createBroker($dynamicMethodReturnTypeExtensions, $dynamicStaticMethodReturnTypeExtensions); |
| 52 | + Broker::registerInstance($broker); |
| 53 | + $typeSpecifier = $this->createTypeSpecifier($printer, $broker, $methodTypeSpecifyingExtensions, $staticMethodTypeSpecifyingExtensions); |
| 54 | + $currentWorkingDirectory = $this->getCurrentWorkingDirectory(); |
| 55 | + $fileHelper = new FileHelper($currentWorkingDirectory); |
| 56 | + $fileTypeMapper = new FileTypeMapper(new DirectReflectionProviderProvider($broker), $this->getParser(), $phpDocStringResolver, $phpDocNodeResolver, $this->createMock(Cache::class), new AnonymousClassNameHelper($fileHelper, new SimpleRelativePathHelper($currentWorkingDirectory))); |
| 57 | + $phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper); |
| 58 | + $resolver = new NodeScopeResolver( |
| 59 | + $broker, |
| 60 | + self::getReflectors()[0], |
| 61 | + $this->getClassReflectionExtensionRegistryProvider(), |
| 62 | + $this->getParser(), |
| 63 | + $fileTypeMapper, |
| 64 | + self::getContainer()->getByType(PhpVersion::class), |
| 65 | + $phpDocInheritanceResolver, |
| 66 | + $fileHelper, |
| 67 | + $typeSpecifier, |
| 68 | + true, |
| 69 | + $this->polluteCatchScopeWithTryAssignments, |
| 70 | + true, |
| 71 | + [ |
| 72 | + \EarlyTermination\Foo::class => [ |
| 73 | + 'doFoo', |
| 74 | + 'doBar', |
| 75 | + ], |
| 76 | + ], |
| 77 | + ['baz'], |
| 78 | + true, |
| 79 | + true |
| 80 | + ); |
| 81 | + $resolver->setAnalysedFiles(array_map(static function (string $file) use ($fileHelper): string { |
| 82 | + return $fileHelper->normalizePath($file); |
| 83 | + }, array_merge([$file], $this->getAdditionalAnalysedFiles()))); |
| 84 | + |
| 85 | + $scopeFactory = $this->createScopeFactory($broker, $typeSpecifier); |
| 86 | + if (count($dynamicConstantNames) > 0) { |
| 87 | + $reflectionProperty = new \ReflectionProperty(DirectScopeFactory::class, 'dynamicConstantNames'); |
| 88 | + $reflectionProperty->setAccessible(true); |
| 89 | + $reflectionProperty->setValue($scopeFactory, $dynamicConstantNames); |
| 90 | + } |
| 91 | + $scope = $scopeFactory->create(ScopeContext::create($file)); |
| 92 | + |
| 93 | + $resolver->processNodes( |
| 94 | + $this->getParser()->parseFile($file), |
| 95 | + $scope, |
| 96 | + $callback |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** @return string[] */ |
| 101 | + protected function getAdditionalAnalysedFiles(): array |
| 102 | + { |
| 103 | + return []; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments