-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refact(core): Refactor FriendlyErrorFormatter for improved structure β¦
β¦and separation of concerns
- Loading branch information
Showing
10 changed files
with
220 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[files] | ||
extend-exclude = [ | ||
".git/", | ||
] | ||
ignore-hidden = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
src/FallbackHighlighter.php β src/CodeHighlight/FallbackHighlighter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yamadashy\PhpStanFriendlyFormatter\Config; | ||
|
||
class FriendlyFormatterConfig | ||
{ | ||
/** @var int */ | ||
public $lineBefore; | ||
|
||
/** @var int */ | ||
public $lineAfter; | ||
|
||
/** @var null|string */ | ||
public $editorUrl; | ||
|
||
public function __construct( | ||
int $lineBefore, | ||
int $lineAfter, | ||
?string $editorUrl | ||
) { | ||
$this->lineBefore = $lineBefore; | ||
$this->lineAfter = $lineAfter; | ||
$this->editorUrl = $editorUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yamadashy\PhpStanFriendlyFormatter\ErrorFormat; | ||
|
||
use PHPStan\Analyser\Error; | ||
use PHPStan\Command\AnalysisResult; | ||
use PHPStan\Command\Output; | ||
use PHPStan\File\RelativePathHelper; | ||
use Yamadashy\PhpStanFriendlyFormatter\CodeHighlight\CodeHighlighter; | ||
use Yamadashy\PhpStanFriendlyFormatter\Config\FriendlyFormatterConfig; | ||
|
||
class ErrorWriter | ||
{ | ||
/** @var RelativePathHelper */ | ||
private $relativePathHelper; | ||
|
||
/** @var FriendlyFormatterConfig */ | ||
private $config; | ||
|
||
public function __construct( | ||
RelativePathHelper $relativePathHelper, | ||
FriendlyFormatterConfig $config | ||
) { | ||
$this->relativePathHelper = $relativePathHelper; | ||
$this->config = $config; | ||
} | ||
|
||
public function writeFileSpecificErrors(AnalysisResult $analysisResult, Output $output): void | ||
{ | ||
$codeHighlighter = new CodeHighlighter(); | ||
$errorsByFile = []; | ||
|
||
foreach ($analysisResult->getFileSpecificErrors() as $error) { | ||
$filePath = $error->getTraitFilePath() ?? $error->getFilePath(); | ||
$relativeFilePath = $this->relativePathHelper->getRelativePath($filePath); | ||
$errorsByFile[$relativeFilePath][] = $error; | ||
} | ||
|
||
foreach ($errorsByFile as $relativeFilePath => $errors) { | ||
$output->writeLineFormatted("β― {$relativeFilePath}"); | ||
$output->writeLineFormatted('--'.str_repeat('-', mb_strlen($relativeFilePath))); | ||
$output->writeLineFormatted(''); | ||
|
||
foreach ($errors as $error) { | ||
$message = $error->getMessage(); | ||
$tip = $this->getFormattedTip($error); | ||
$errorIdentifier = $error->getIdentifier(); | ||
$filePath = $error->getTraitFilePath() ?? $error->getFilePath(); | ||
$line = $error->getLine(); | ||
$fileContent = null; | ||
|
||
if (file_exists($filePath)) { | ||
$fileContent = (string) file_get_contents($filePath); | ||
} | ||
|
||
if (null === $fileContent) { | ||
$codeSnippet = ' <fg=#888><no such file></>'; | ||
} elseif (null === $line) { | ||
$codeSnippet = ' <fg=#888><unknown file line></>'; | ||
} else { | ||
$codeSnippet = $codeHighlighter->highlight($fileContent, $line, $this->config->lineBefore, $this->config->lineAfter); | ||
} | ||
|
||
$output->writeLineFormatted(" <fg=red;options=bold>β</> <fg=default;options=bold>{$message}</>"); | ||
|
||
if (null !== $tip) { | ||
$output->writeLineFormatted(" <fg=default>π‘ {$tip}</>"); | ||
} | ||
|
||
if (null !== $errorIdentifier) { | ||
$output->writeLineFormatted(" <fg=default>πͺͺ {$errorIdentifier}</>"); | ||
} | ||
|
||
if (\is_string($this->config->editorUrl)) { | ||
$output->writeLineFormatted(' βοΈ '.str_replace(['%file%', '%line%'], [$error->getTraitFilePath() ?? $error->getFilePath(), (string) $error->getLine()], $this->config->editorUrl)); | ||
} | ||
|
||
$output->writeLineFormatted($codeSnippet); | ||
$output->writeLineFormatted(''); | ||
} | ||
} | ||
} | ||
|
||
public function writeNotFileSpecificErrors(AnalysisResult $analysisResult, Output $output): void | ||
{ | ||
foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) { | ||
$output->writeLineFormatted(" <fg=red;options=bold>β</> <fg=default;options=bold>{$notFileSpecificError}</>"); | ||
$output->writeLineFormatted(''); | ||
} | ||
} | ||
|
||
public function writeWarnings(AnalysisResult $analysisResult, Output $output): void | ||
{ | ||
foreach ($analysisResult->getWarnings() as $warning) { | ||
$output->writeLineFormatted(" <fg=yellow;options=bold>β </> <fg=default;options=bold>{$warning}</>"); | ||
$output->writeLineFormatted(''); | ||
} | ||
} | ||
|
||
private function getFormattedTip(Error $error): ?string | ||
{ | ||
$tip = $error->getTip(); | ||
|
||
if (null === $tip) { | ||
return null; | ||
} | ||
|
||
return implode("\n ", explode("\n", $tip)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yamadashy\PhpStanFriendlyFormatter\ErrorFormat; | ||
|
||
use PHPStan\Command\AnalysisResult; | ||
use PHPStan\Command\Output; | ||
|
||
class SummaryWriter | ||
{ | ||
public function writeGroupedErrorsSummary(AnalysisResult $analysisResult, Output $output): void | ||
{ | ||
/** @var array<string, int> $errorCounter */ | ||
$errorCounter = []; | ||
|
||
foreach ($analysisResult->getFileSpecificErrors() as $error) { | ||
$identifier = $error->getIdentifier() ?? '<no-identifier>'; | ||
if (!\array_key_exists($identifier, $errorCounter)) { | ||
$errorCounter[$identifier] = 0; | ||
} | ||
++$errorCounter[$identifier]; | ||
} | ||
|
||
arsort($errorCounter); | ||
|
||
$output->writeLineFormatted('π Error Identifier Summary:'); | ||
$output->writeLineFormatted('ββββββββββββββββββββββββββββ'); | ||
|
||
foreach ($errorCounter as $identifier => $count) { | ||
$output->writeLineFormatted(\sprintf(' %d %s', $count, $identifier)); | ||
} | ||
} | ||
} |
Oops, something went wrong.