From 7d030de8f88358a185b58786f53c7e795a94087d Mon Sep 17 00:00:00 2001 From: Grzegorz Korba Date: Thu, 16 Jun 2022 00:12:41 +0200 Subject: [PATCH] Use `SimpleRelativePathHelper` for `%relFile%` substitution --- conf/config.neon | 1 + .../ErrorFormatter/TableErrorFormatter.php | 4 +- .../AnalyseApplicationIntegrationTest.php | 15 +++++-- .../TableErrorFormatterTest.php | 41 ++++++++++--------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/conf/config.neon b/conf/config.neon index 4859022f35..3123a59314 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1949,6 +1949,7 @@ services: errorFormatter.table: class: PHPStan\Command\ErrorFormatter\TableErrorFormatter arguments: + simpleRelativePathHelper: @simpleRelativePathHelper showTipsOfTheDay: %tipsOfTheDay% editorUrl: %editorUrl% diff --git a/src/Command/ErrorFormatter/TableErrorFormatter.php b/src/Command/ErrorFormatter/TableErrorFormatter.php index a2275ddf08..c4a489b23a 100644 --- a/src/Command/ErrorFormatter/TableErrorFormatter.php +++ b/src/Command/ErrorFormatter/TableErrorFormatter.php @@ -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; @@ -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, @@ -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✏️ ' . $this->relativePathHelper->getRelativePath($editorFile) . ''; diff --git a/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php b/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php index 3c60d6176c..256d969238 100644 --- a/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php +++ b/tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php @@ -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; @@ -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, diff --git a/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php b/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php index a26bbef140..96c5d063b7 100644 --- a/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php +++ b/tests/PHPStan/Command/ErrorFormatter/TableErrorFormatterTest.php @@ -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; @@ -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), @@ -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()); @@ -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)); @@ -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( [ @@ -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, + ); + } + }