Skip to content

Commit

Permalink
TableErrorFormatter: allow editor url title configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored and ondrejmirtes committed Dec 1, 2022
1 parent 0710109 commit 6bde609
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
3 changes: 3 additions & 0 deletions conf/config.neon
Expand Up @@ -206,6 +206,7 @@ parameters:
- ZEND_THREAD_SAFE
customRulesetUsed: null
editorUrl: null
editorUrlTitle: null
errorFormat: null
__validate: true

Expand Down Expand Up @@ -385,6 +386,7 @@ parametersSchema:
scanDirectories: listOf(string())
fixerTmpDir: string()
editorUrl: schema(string(), nullable())
editorUrlTitle: schema(string(), nullable())
errorFormat: schema(string(), nullable())

# irrelevant Nette parameters
Expand Down Expand Up @@ -2018,6 +2020,7 @@ services:
simpleRelativePathHelper: @simpleRelativePathHelper
showTipsOfTheDay: %tipsOfTheDay%
editorUrl: %editorUrl%
editorUrlTitle: %editorUrlTitle%

errorFormatter.checkstyle:
class: PHPStan\Command\ErrorFormatter\CheckstyleErrorFormatter
Expand Down
14 changes: 13 additions & 1 deletion src/Command/ErrorFormatter/TableErrorFormatter.php
Expand Up @@ -25,6 +25,7 @@ public function __construct(
private CiDetectedErrorFormatter $ciDetectedErrorFormatter,
private bool $showTipsOfTheDay,
private ?string $editorUrl,
private ?string $editorUrlTitle,
)
{
}
Expand Down Expand Up @@ -86,7 +87,18 @@ public function formatErrors(
[$editorFile, $this->simpleRelativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
$this->editorUrl,
);
$message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $this->relativePathHelper->getRelativePath($editorFile) . '</>';

if (is_string($this->editorUrlTitle)) {
$title = str_replace(
['%file%', '%relFile%', '%line%'],
[$editorFile, $this->simpleRelativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
$this->editorUrlTitle,
);
} else {
$title = $this->relativePathHelper->getRelativePath($editorFile);
}

$message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $title . '</>';
}
$rows[] = [
$this->formatLineNumber($error->getLine()),
Expand Down
Expand Up @@ -77,6 +77,7 @@ private function runPath(string $path, int $expectedStatusCode): string
),
false,
null,
null,
);
$analysisResult = $analyserApplication->analyse(
[$path],
Expand Down
12 changes: 11 additions & 1 deletion tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php
Expand Up @@ -224,6 +224,15 @@ public function testEditorUrlWithRelativePath(): void
$this->assertStringContainsString('editor://custom/path/rel/Foo.php', $this->getOutputContent(true));
}

public function testEditorUrlWithCustomTitle(): void
{
$formatter = $this->createErrorFormatter('editor://any', '%relFile%:%line%');
$error = new Error('Test', 'Foo.php', 12, true, self::DIRECTORY_PATH . '/rel/Foo.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], [], false, null, true), $this->getOutput(true));

$this->assertStringContainsString('rel/Foo.php:12', $this->getOutputContent(true));
}

public function testBug6727(): void
{
putenv('COLUMNS=30');
Expand All @@ -250,7 +259,7 @@ public function testBug6727(): void
self::expectNotToPerformAssertions();
}

private function createErrorFormatter(?string $editorUrl): TableErrorFormatter
private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): TableErrorFormatter
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');

Expand All @@ -263,6 +272,7 @@ private function createErrorFormatter(?string $editorUrl): TableErrorFormatter
),
false,
$editorUrl,
$editorUrlTitle,
);
}

Expand Down

0 comments on commit 6bde609

Please sign in to comment.