|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +use PHPStan\AnalysedCodeException; |
| 6 | +use PHPStan\Analyser\Error; |
| 7 | +use PHPStan\Analyser\RuleErrorTransformer; |
| 8 | +use PHPStan\Analyser\ScopeContext; |
| 9 | +use PHPStan\Analyser\ScopeFactory; |
| 10 | +use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode; |
| 11 | +use PHPStan\BetterReflection\Reflection\Exception\CircularReference; |
| 12 | +use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; |
| 13 | +use PHPStan\Collectors\CollectedData; |
| 14 | +use PHPStan\Node\CollectedDataNode; |
| 15 | +use Symfony\Component\Console\Formatter\OutputFormatter; |
| 16 | + |
| 17 | +require __DIR__.'/vendor/autoload.php'; |
| 18 | + |
| 19 | +error_reporting(E_ALL); |
| 20 | +ini_set('display_errors', '1'); |
| 21 | + |
| 22 | +$phpstanVersion = \Composer\InstalledVersions::getPrettyVersion('phpstan/phpstan'); |
| 23 | +$phpstanDrupalVersion = \Composer\InstalledVersions::getPrettyVersion('mglaman/phpstan-drupal'); |
| 24 | +$drupalCoreVersion = \Composer\InstalledVersions::getPrettyVersion('drupal/core'); |
| 25 | + |
| 26 | +/** |
| 27 | + * @param CollectedData[] $collectedData |
| 28 | + * @return Error[] |
| 29 | + */ |
| 30 | +function getCollectedDataErrors(\PHPStan\DependencyInjection\Container $container, array $collectedData): array |
| 31 | +{ |
| 32 | + $nodeType = CollectedDataNode::class; |
| 33 | + $node = new CollectedDataNode($collectedData, true); |
| 34 | + $file = 'N/A'; |
| 35 | + $scope = $container->getByType(ScopeFactory::class)->create(ScopeContext::create($file)); |
| 36 | + $ruleRegistry = $container->getByType(\PHPStan\Rules\Registry::class); |
| 37 | + $ruleErrorTransformer = $container->getByType(RuleErrorTransformer::class); |
| 38 | + $errors = []; |
| 39 | + foreach ($ruleRegistry->getRules($nodeType) as $rule) { |
| 40 | + try { |
| 41 | + $ruleErrors = $rule->processNode($node, $scope); |
| 42 | + } catch (AnalysedCodeException $e) { |
| 43 | + $errors[] = new Error($e->getMessage(), $file, $node->getLine(), $e, null, null, $e->getTip()); |
| 44 | + continue; |
| 45 | + } catch (IdentifierNotFound $e) { |
| 46 | + $errors[] = new Error(sprintf('Reflection error: %s not found.', $e->getIdentifier()->getName()), $file, $node->getLine(), $e, null, null, 'Learn more at https://phpstan.org/user-guide/discovering-symbols'); |
| 47 | + continue; |
| 48 | + } catch (UnableToCompileNode | CircularReference $e) { |
| 49 | + $errors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + foreach ($ruleErrors as $ruleError) { |
| 54 | + $errors[] = $ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getLine()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return $errors; |
| 59 | +} |
| 60 | + |
| 61 | +function clearTemp($tmpDir): void |
| 62 | +{ |
| 63 | + $files = new RecursiveIteratorIterator( |
| 64 | + new RecursiveDirectoryIterator($tmpDir, RecursiveDirectoryIterator::SKIP_DOTS), |
| 65 | + RecursiveIteratorIterator::CHILD_FIRST |
| 66 | + ); |
| 67 | + |
| 68 | + foreach ($files as $fileinfo) { |
| 69 | + $todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink'); |
| 70 | + $todo($fileinfo->getRealPath()); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +return function(array $event) use ($phpstanVersion, $phpstanDrupalVersion, $drupalCoreVersion) { |
| 75 | + $tmpDir = sys_get_temp_dir() . '/phpstan-runner'; |
| 76 | + if (!is_dir($tmpDir)) { |
| 77 | + mkdir($tmpDir); |
| 78 | + } |
| 79 | + clearTemp($tmpDir); |
| 80 | + $code = $event['code']; |
| 81 | + $level = $event['level']; |
| 82 | + $codePath = sys_get_temp_dir() . '/phpstan-runner/tmp.php'; |
| 83 | + file_put_contents($codePath, $code); |
| 84 | + |
| 85 | + $rootDir = __DIR__; |
| 86 | + $configFiles = [ |
| 87 | + $rootDir . '/playground.neon', |
| 88 | + $rootDir . '/vendor/mglaman/phpstan-drupal/extension.neon', |
| 89 | + $rootDir . '/vendor/mglaman/phpstan-drupal/rules.neon', |
| 90 | + $rootDir . '/vendor/phpstan/phpstan-deprecation-rules/rules.neon', |
| 91 | + ]; |
| 92 | + foreach ([ |
| 93 | + 'strictRules' => $rootDir . '/vendor/phpstan/phpstan-strict-rules/rules.neon', |
| 94 | + 'bleedingEdge' => 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/conf/bleedingEdge.neon', |
| 95 | + ] as $key => $file) { |
| 96 | + if (!isset($event[$key]) || !$event[$key]) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + $configFiles[] = $file; |
| 101 | + } |
| 102 | + $finalConfigFile = $tmpDir . '/run-phpstan-tmp.neon'; |
| 103 | + $neon = \Nette\Neon\Neon::encode([ |
| 104 | + 'includes' => $configFiles, |
| 105 | + 'parameters' => [ |
| 106 | + 'inferPrivatePropertyTypeFromConstructor' => true, |
| 107 | + 'treatPhpDocTypesAsCertain' => $event['treatPhpDocTypesAsCertain'] ?? true, |
| 108 | + 'phpVersion' => $event['phpVersion'] ?? 80000, |
| 109 | + 'featureToggles' => [ |
| 110 | + 'disableRuntimeReflectionProvider' => true, |
| 111 | + ], |
| 112 | + 'drupal' => [ |
| 113 | + 'drupal_root' => \Composer\InstalledVersions::getInstallPath('drupal/core'), |
| 114 | + ], |
| 115 | + ], |
| 116 | + 'services' => [ |
| 117 | + 'currentPhpVersionSimpleParser!' => [ |
| 118 | + 'factory' => '@currentPhpVersionRichParser', |
| 119 | + ], |
| 120 | + ], |
| 121 | + ]); |
| 122 | + file_put_contents($finalConfigFile, $neon); |
| 123 | + |
| 124 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/ReflectionUnionType.php'; |
| 125 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/ReflectionIntersectionType.php'; |
| 126 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/ReflectionAttribute.php'; |
| 127 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Attribute.php'; |
| 128 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Enum/UnitEnum.php'; |
| 129 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Enum/BackedEnum.php'; |
| 130 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Enum/ReflectionEnum.php'; |
| 131 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Enum/ReflectionEnumUnitCase.php'; |
| 132 | + require_once 'phar://' . $rootDir . '/vendor/phpstan/phpstan/phpstan.phar/stubs/runtime/Enum/ReflectionEnumBackedCase.php'; |
| 133 | + |
| 134 | + $containerFactory = new \PHPStan\DependencyInjection\ContainerFactory($tmpDir); |
| 135 | + $container = $containerFactory->create($tmpDir, [sprintf('%s/config.level%s.neon', $containerFactory->getConfigDirectory(), $level), $finalConfigFile], [$codePath]); |
| 136 | + |
| 137 | + // Note: this is the big change from the parent script in phpstan-src. |
| 138 | + foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) { |
| 139 | + try { |
| 140 | + (static function (string $bootstrapFileFromArray) use ($container): void { |
| 141 | + require_once $bootstrapFileFromArray; |
| 142 | + })($bootstrapFileFromArray); |
| 143 | + } catch (Throwable $e) { |
| 144 | + $error = sprintf('%s thrown in %s on line %d while loading bootstrap file %s: %s', get_class($e), $e->getFile(), $e->getLine(), $file, $e->getMessage()); |
| 145 | + return ['result' => [$error], 'versions' => [ |
| 146 | + 'phpstan' => $phpstanVersion, |
| 147 | + 'phpstan-drupal' => $phpstanDrupalVersion, |
| 148 | + 'drupal' => $drupalCoreVersion, |
| 149 | + ]]; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** @var \PHPStan\Analyser\Analyser $analyser */ |
| 154 | + $analyser = $container->getByType(\PHPStan\Analyser\Analyser::class); |
| 155 | + $analyserResult = $analyser->analyse([$codePath], null, null, false, [$codePath]); |
| 156 | + $hasInternalErrors = count($analyserResult->getInternalErrors()) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit(); |
| 157 | + $results = $analyserResult->getErrors(); |
| 158 | + |
| 159 | + if (!$hasInternalErrors) { |
| 160 | + foreach (getCollectedDataErrors($container, $analyserResult->getCollectedData()) as $error) { |
| 161 | + $results[] = $error; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + error_clear_last(); |
| 166 | + |
| 167 | + $errors = []; |
| 168 | + $tipFormatter = new OutputFormatter(false); |
| 169 | + foreach ($results as $result) { |
| 170 | + $error = [ |
| 171 | + 'message' => $result->getMessage(), |
| 172 | + 'line' => $result->getLine(), |
| 173 | + 'ignorable' => $result->canBeIgnored(), |
| 174 | + ]; |
| 175 | + if ($result->getTip() !== null) { |
| 176 | + $error['tip'] = $tipFormatter->format($result->getTip()); |
| 177 | + } |
| 178 | + if ($result->getIdentifier() !== null) { |
| 179 | + $error['identifier'] = $result->getIdentifier(); |
| 180 | + } |
| 181 | + $errors[] = $error; |
| 182 | + } |
| 183 | + |
| 184 | + return ['result' => $errors, 'versions' => [ |
| 185 | + 'phpstan' => $phpstanVersion, |
| 186 | + 'phpstan-drupal' => $phpstanDrupalVersion, |
| 187 | + 'drupal' => $drupalCoreVersion, |
| 188 | + ]]; |
| 189 | +}; |
0 commit comments