Skip to content
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

TableErrorFormatter: allow editor url title configuration #2035

Merged
merged 1 commit into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions conf/config.neon
Expand Up @@ -205,6 +205,7 @@ parameters:
- ZEND_THREAD_SAFE
customRulesetUsed: null
editorUrl: null
editorUrlTitle: null
errorFormat: null
__validate: true

Expand Down Expand Up @@ -384,6 +385,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 @@ -2012,6 +2014,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