Skip to content

Commit b85a961

Browse files
committed
PHPStan Pro - show errors from CollectedData
1 parent 43b410c commit b85a961

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/Command/FixerWorkerCommand.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
namespace PHPStan\Command;
44

55
use Nette\Utils\Json;
6+
use PHPStan\AnalysedCodeException;
7+
use PHPStan\Analyser\Error;
68
use PHPStan\Analyser\IgnoredErrorHelper;
79
use PHPStan\Analyser\ResultCache\ResultCacheManagerFactory;
10+
use PHPStan\Analyser\RuleErrorTransformer;
11+
use PHPStan\Analyser\ScopeContext;
12+
use PHPStan\Analyser\ScopeFactory;
13+
use PHPStan\BetterReflection\NodeCompiler\Exception\UnableToCompileNode;
14+
use PHPStan\BetterReflection\Reflection\Exception\CircularReference;
15+
use PHPStan\BetterReflection\Reflection\Exception\NotAClassReflection;
16+
use PHPStan\BetterReflection\Reflection\Exception\NotAnInterfaceReflection;
17+
use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
18+
use PHPStan\Collectors\CollectedData;
19+
use PHPStan\DependencyInjection\Container;
20+
use PHPStan\Node\CollectedDataNode;
21+
use PHPStan\Rules\Registry as RuleRegistry;
822
use PHPStan\ShouldNotHappenException;
923
use Symfony\Component\Console\Command\Command;
1024
use Symfony\Component\Console\Input\InputArgument;
@@ -15,6 +29,7 @@
1529
use function is_array;
1630
use function is_bool;
1731
use function is_string;
32+
use function sprintf;
1833

1934
class FixerWorkerCommand extends Command
2035
{
@@ -124,12 +139,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124139
is_string($saveResultCache) ? $saveResultCache : $saveResultCache === null,
125140
)->getAnalyserResult();
126141

142+
$hasInternalErrors = count($result->getInternalErrors()) > 0 || $result->hasReachedInternalErrorsCountLimit();
127143
$intermediateErrors = $ignoredErrorHelperResult->process(
128144
$result->getErrors(),
129145
$isOnlyFiles,
130146
$inceptionFiles,
131-
count($result->getInternalErrors()) > 0 || $result->hasReachedInternalErrorsCountLimit(),
147+
$hasInternalErrors,
132148
);
149+
if (!$hasInternalErrors) {
150+
foreach ($this->getCollectedDataErrors($container, $result->getCollectedData()) as $error) {
151+
$intermediateErrors[] = $error;
152+
}
153+
}
154+
133155
$finalFileSpecificErrors = [];
134156
$finalNotFileSpecificErrors = [];
135157
foreach ($intermediateErrors as $intermediateError) {
@@ -149,4 +171,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
149171
return 0;
150172
}
151173

174+
/**
175+
* @param CollectedData[] $collectedData
176+
* @return Error[]
177+
*/
178+
private function getCollectedDataErrors(Container $container, array $collectedData): array
179+
{
180+
$nodeType = CollectedDataNode::class;
181+
$node = new CollectedDataNode($collectedData);
182+
$file = 'N/A';
183+
$scope = $container->getByType(ScopeFactory::class)->create(ScopeContext::create($file));
184+
$ruleRegistry = $container->getByType(RuleRegistry::class);
185+
$ruleErrorTransformer = $container->getByType(RuleErrorTransformer::class);
186+
187+
$errors = [];
188+
foreach ($ruleRegistry->getRules($nodeType) as $rule) {
189+
try {
190+
$ruleErrors = $rule->processNode($node, $scope);
191+
} catch (AnalysedCodeException $e) {
192+
$errors[] = new Error($e->getMessage(), $file, $node->getLine(), $e, null, null, $e->getTip());
193+
continue;
194+
} catch (IdentifierNotFound $e) {
195+
$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');
196+
continue;
197+
} catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection | CircularReference $e) {
198+
$errors[] = new Error(sprintf('Reflection error: %s', $e->getMessage()), $file, $node->getLine(), $e);
199+
continue;
200+
}
201+
202+
foreach ($ruleErrors as $ruleError) {
203+
$errors[] = $ruleErrorTransformer->transform($ruleError, $scope, $nodeType, $node->getLine());
204+
}
205+
}
206+
207+
return $errors;
208+
}
209+
152210
}

0 commit comments

Comments
 (0)