Skip to content

Improve editor URL #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
errorFormatter.friendly:
class: Yamadashy\PhpStanFriendlyFormatter\FriendlyErrorFormatter
arguments:
simpleRelativePathHelper: @simpleRelativePathHelper
lineBefore: %friendly.lineBefore%
lineAfter: %friendly.lineAfter%
editorUrl: %friendly.editorUrl%
13 changes: 12 additions & 1 deletion src/ErrorFormat/ErrorWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Yamadashy\PhpStanFriendlyFormatter\CodeHighlight\CodeHighlighter;
use Yamadashy\PhpStanFriendlyFormatter\Config\FriendlyFormatterConfig;

Expand All @@ -14,14 +16,19 @@ class ErrorWriter
/** @var RelativePathHelper */
private $relativePathHelper;

/** @var SimpleRelativePathHelper */
private $simpleRelativePathHelper;

/** @var FriendlyFormatterConfig */
private $config;

public function __construct(
RelativePathHelper $relativePathHelper,
SimpleRelativePathHelper $simpleRelativePathHelper,
FriendlyFormatterConfig $config
) {
$this->relativePathHelper = $relativePathHelper;
$this->simpleRelativePathHelper = $simpleRelativePathHelper;
$this->config = $config;
}

Expand Down Expand Up @@ -72,7 +79,11 @@ public function writeFileSpecificErrors(AnalysisResult $analysisResult, Output $
}

if (\is_string($this->config->editorUrl)) {
$output->writeLineFormatted(' ✏️ '.str_replace(['%file%', '%line%'], [$error->getTraitFilePath() ?? $error->getFilePath(), (string) $error->getLine()], $this->config->editorUrl));
$output->writeLineFormatted(' ✏️ <href='.OutputFormatter::escape(str_replace(
['%file%', '%relFile%', '%line%'],
[$filePath, $this->simpleRelativePathHelper->getRelativePath($filePath), (string) $error->getLine()],
$this->config->editorUrl,
)).'>'.$relativeFilePath.'</>');
}

$output->writeLineFormatted($codeSnippet);
Expand Down
16 changes: 13 additions & 3 deletions src/FriendlyErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use Yamadashy\PhpStanFriendlyFormatter\Config\FriendlyFormatterConfig;
use Yamadashy\PhpStanFriendlyFormatter\ErrorFormat\ErrorWriter;
use Yamadashy\PhpStanFriendlyFormatter\ErrorFormat\SummaryWriter;
Expand All @@ -16,12 +17,21 @@ class FriendlyErrorFormatter implements ErrorFormatter
/** @var RelativePathHelper */
private $relativePathHelper;

/** @var SimpleRelativePathHelper */
private $simpleRelativePathHelper;

/** @var FriendlyFormatterConfig */
private $config;

public function __construct(RelativePathHelper $relativePathHelper, int $lineBefore, int $lineAfter, ?string $editorUrl)
{
public function __construct(
RelativePathHelper $relativePathHelper,
SimpleRelativePathHelper $simpleRelativePathHelper,
int $lineBefore,
int $lineAfter,
?string $editorUrl
) {
$this->relativePathHelper = $relativePathHelper;
$this->simpleRelativePathHelper = $simpleRelativePathHelper;
$this->config = new FriendlyFormatterConfig(
$lineBefore,
$lineAfter,
Expand All @@ -40,7 +50,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in

$output->writeLineFormatted('');

$errorWriter = new ErrorWriter($this->relativePathHelper, $this->config);
$errorWriter = new ErrorWriter($this->relativePathHelper, $this->simpleRelativePathHelper, $this->config);
$errorWriter->writeFileSpecificErrors($analysisResult, $output);
$errorWriter->writeNotFileSpecificErrors($analysisResult, $output);
$errorWriter->writeWarnings($analysisResult, $output);
Expand Down