Skip to content

Commit

Permalink
Use SimpleRelativePathHelper for %relFile% substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Jun 15, 2022
1 parent 6e9999c commit 7d030de
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions conf/config.neon
Expand Up @@ -1949,6 +1949,7 @@ services:
errorFormatter.table:
class: PHPStan\Command\ErrorFormatter\TableErrorFormatter
arguments:
simpleRelativePathHelper: @simpleRelativePathHelper
showTipsOfTheDay: %tipsOfTheDay%
editorUrl: %editorUrl%

Expand Down
4 changes: 3 additions & 1 deletion src/Command/ErrorFormatter/TableErrorFormatter.php
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use Symfony\Component\Console\Formatter\OutputFormatter;
use function array_map;
use function count;
Expand All @@ -19,6 +20,7 @@ class TableErrorFormatter implements ErrorFormatter

public function __construct(
private RelativePathHelper $relativePathHelper,
private SimpleRelativePathHelper $simpleRelativePathHelper,
private CiDetectedErrorFormatter $ciDetectedErrorFormatter,
private bool $showTipsOfTheDay,
private ?string $editorUrl,
Expand Down Expand Up @@ -80,7 +82,7 @@ public function formatErrors(
$editorFile = $error->getTraitFilePath() ?? $error->getFilePath();
$url = str_replace(
['%file%', '%relFile%', '%line%'],
[$editorFile, $this->relativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
[$editorFile, $this->simpleRelativePathHelper->getRelativePath($editorFile), (string) $error->getLine()],
$this->editorUrl,
);
$message .= "\n✏️ <href=" . OutputFormatter::escape($url) . '>' . $this->relativePathHelper->getRelativePath($editorFile) . '</>';
Expand Down
15 changes: 11 additions & 4 deletions tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Command\Symfony\SymfonyOutput;
use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -67,10 +68,16 @@ private function runPath(string $path, int $expectedStatusCode): string
$memoryLimitFile = self::getContainer()->getParameter('memoryLimitFile');

$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), __DIR__, [], DIRECTORY_SEPARATOR);
$errorFormatter = new TableErrorFormatter($relativePathHelper, new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
), false, null);
$errorFormatter = new TableErrorFormatter(
$relativePathHelper,
new SimpleRelativePathHelper(__DIR__),
new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
),
false,
null,
);
$analysisResult = $analyserApplication->analyse(
[$path],
true,
Expand Down
41 changes: 21 additions & 20 deletions tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Command\AnalysisResult;
use PHPStan\File\FuzzyRelativePathHelper;
use PHPStan\File\NullRelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;
use function putenv;
use function sprintf;
Expand Down Expand Up @@ -164,11 +165,7 @@ public function testFormatErrors(
if (PHP_VERSION_ID >= 80100) {
self::markTestSkipped('Skipped on PHP 8.1 because of different result');
}
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new TableErrorFormatter($relativePathHelper, new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
), false, null);
$formatter = $this->createErrorFormatter(null);

$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
Expand All @@ -180,11 +177,7 @@ public function testFormatErrors(

public function testEditorUrlWithTrait(): void
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new TableErrorFormatter($relativePathHelper, new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
), false, 'editor://%file%/%line%');
$formatter = $this->createErrorFormatter('editor://%file%/%line%');
$error = new Error('Test', 'Foo.php (in context of trait)', 12, true, 'Foo.php', 'Bar.php');
$formatter->formatErrors(new AnalysisResult([$error], [], [], [], false, null, true), $this->getOutput());

Expand All @@ -193,11 +186,7 @@ public function testEditorUrlWithTrait(): void

public function testEditorUrlWithRelativePath(): void
{
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new TableErrorFormatter($relativePathHelper, new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
), false, 'editor://custom/path/%relFile%/%line%');
$formatter = $this->createErrorFormatter('editor://custom/path/%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));

Expand All @@ -207,11 +196,7 @@ public function testEditorUrlWithRelativePath(): void
public function testBug6727(): void
{
putenv('COLUMNS=30');
$relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/');
$formatter = new TableErrorFormatter($relativePathHelper, new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
), false, null);
$formatter = $this->createErrorFormatter(null);
$formatter->formatErrors(
new AnalysisResult(
[
Expand All @@ -233,4 +218,20 @@ public function testBug6727(): void
self::expectNotToPerformAssertions();
}

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

return new TableErrorFormatter(
$relativePathHelper,
new SimpleRelativePathHelper(self::DIRECTORY_PATH),
new CiDetectedErrorFormatter(
new GithubErrorFormatter($relativePathHelper),
new TeamcityErrorFormatter($relativePathHelper),
),
false,
$editorUrl,
);
}

}

0 comments on commit 7d030de

Please sign in to comment.