Skip to content

Commit

Permalink
Display error tip (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmy committed Feb 14, 2024
1 parent 593c05f commit 9a37531
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use Symfony\Component\Console\Command\Command;

final class BladeTemplateErrorFormatter implements ErrorFormatter
{
private readonly SimpleRelativePathHelper $simpleRelativePathHelper;

public function __construct()
{
public function __construct(
private RelativePathHelper $relativePathHelper,
) {
/** @var string $currentWorkingDirectory */
$currentWorkingDirectory = getcwd();
$this->simpleRelativePathHelper = new SimpleRelativePathHelper($currentWorkingDirectory);
Expand All @@ -27,6 +29,11 @@ public function __construct()
*/
public function formatErrors(AnalysisResult $analysisResult, Output $output): int
{
$projectConfigFile = 'phpstan.neon';
if ($analysisResult->getProjectConfigFile() !== null) {
$projectConfigFile = $this->relativePathHelper->getRelativePath($analysisResult->getProjectConfigFile());
}

$outputStyle = $output->getStyle();

if (! $analysisResult->hasErrors() && ! $analysisResult->hasWarnings()) {
Expand All @@ -51,6 +58,21 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
foreach ($errors as $error) {
$message = $error->getMessage();

if ($error->getTip() !== null) {
$tip = $error->getTip();
$tip = str_replace('%configurationFile%', $projectConfigFile, $tip);

$message .= "\n";
if (str_contains($tip, "\n")) {
$lines = explode("\n", $tip);
foreach ($lines as $line) {
$message .= '💡 ' . ltrim($line, ' •') . "\n";
}
} else {
$message .= '💡 ' . $tip;
}
}

$rows[] = [(string) $error->getLine(), $message];

$errorMetadata = $error->getMetadata();
Expand Down

0 comments on commit 9a37531

Please sign in to comment.