|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace ShipMonk\PHPStan\DeadCode\Formatter; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use PHPStan\Analyser\Error; |
| 7 | +use PHPStan\Command\AnalysisResult; |
| 8 | +use PHPStan\Command\ErrorFormatter\ErrorFormatter; |
| 9 | +use PHPStan\Command\Output; |
| 10 | +use PHPStan\DependencyInjection\Container; |
| 11 | +use PHPStan\DependencyInjection\MissingServiceException; |
| 12 | +use function strpos; |
| 13 | +use function substr; |
| 14 | + |
| 15 | +/** |
| 16 | + * This formatter solves the following issue https://github.com/phpstan/phpstan/issues/12328 |
| 17 | + */ |
| 18 | +final class FilterOutUnmatchedInlineIgnoresFormatter implements ErrorFormatter |
| 19 | +{ |
| 20 | + |
| 21 | + private ErrorFormatter $originalFormatter; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var list<string> |
| 25 | + */ |
| 26 | + private array $identifiers; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param list<string> $identifiers |
| 30 | + */ |
| 31 | + public function __construct( |
| 32 | + Container $container, |
| 33 | + string $wrappedFormatter, |
| 34 | + array $identifiers |
| 35 | + ) |
| 36 | + { |
| 37 | + try { |
| 38 | + /** @var ErrorFormatter $formatter */ |
| 39 | + $formatter = $container->getService('errorFormatter.' . $wrappedFormatter); |
| 40 | + $this->originalFormatter = $formatter; |
| 41 | + } catch (MissingServiceException $e) { |
| 42 | + throw new LogicException('Invalid error formatter given: ' . $wrappedFormatter, 0, $e); |
| 43 | + } |
| 44 | + $this->identifiers = $identifiers; |
| 45 | + } |
| 46 | + |
| 47 | + public function formatErrors( |
| 48 | + AnalysisResult $analysisResult, |
| 49 | + Output $output |
| 50 | + ): int |
| 51 | + { |
| 52 | + if (!$this->isPartialAnalysis()) { |
| 53 | + return $this->originalFormatter->formatErrors($analysisResult, $output); |
| 54 | + } |
| 55 | + |
| 56 | + $modifiedAnalysisResult = new AnalysisResult( // @phpstan-ignore phpstanApi.constructor |
| 57 | + $this->filterErrors($analysisResult->getFileSpecificErrors()), |
| 58 | + $analysisResult->getNotFileSpecificErrors(), |
| 59 | + $analysisResult->getInternalErrorObjects(), |
| 60 | + $analysisResult->getWarnings(), |
| 61 | + $analysisResult->getCollectedData(), |
| 62 | + $analysisResult->isDefaultLevelUsed(), |
| 63 | + $analysisResult->getProjectConfigFile(), |
| 64 | + $analysisResult->isResultCacheSaved(), |
| 65 | + $analysisResult->getPeakMemoryUsageBytes(), |
| 66 | + $analysisResult->isResultCacheUsed(), |
| 67 | + $analysisResult->getChangedProjectExtensionFilesOutsideOfAnalysedPaths(), |
| 68 | + ); |
| 69 | + |
| 70 | + return $this->originalFormatter->formatErrors($modifiedAnalysisResult, $output); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param list<Error> $errors |
| 75 | + * @return list<Error> |
| 76 | + */ |
| 77 | + private function filterErrors(array $errors): array |
| 78 | + { |
| 79 | + $result = []; |
| 80 | + foreach ($errors as $error) { |
| 81 | + if ( |
| 82 | + $error->getIdentifier() === 'ignore.unmatchedIdentifier' |
| 83 | + && $this->isDeadCodeIdentifier($error->getMessage()) |
| 84 | + ) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $result[] = $error; |
| 89 | + } |
| 90 | + |
| 91 | + return $result; |
| 92 | + } |
| 93 | + |
| 94 | + private function isDeadCodeIdentifier(string $message): bool |
| 95 | + { |
| 96 | + foreach ($this->identifiers as $identifier) { |
| 97 | + if (strpos($message, $identifier) !== false) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + private function isPartialAnalysis(): bool |
| 105 | + { |
| 106 | + /** @var array<string> $argv */ |
| 107 | + $argv = $_SERVER['argv'] ?? []; |
| 108 | + foreach ($argv as $arg) { |
| 109 | + if (substr($arg, -4) === '.php') { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments