Skip to content

Commit

Permalink
GitlabErrorFormatter produces relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 8, 2019
1 parent e2308ec commit 3f0efd0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,5 @@ services:

errorFormatter.gitlab:
class: PHPStan\Command\ErrorFormatter\GitlabErrorFormatter
arguments:
relativePathHelper: @simpleRelativePathHelper
11 changes: 10 additions & 1 deletion src/Command/ErrorFormatter/GitlabErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
use Nette\Utils\Json;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
use PHPStan\File\RelativePathHelper;

/**
* @see https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html#implementing-a-custom-tool
*/
class GitlabErrorFormatter implements ErrorFormatter
{

/** @var RelativePathHelper */
private $relativePathHelper;

public function __construct(RelativePathHelper $relativePathHelper)
{
$this->relativePathHelper = $relativePathHelper;
}

public function formatErrors(AnalysisResult $analysisResult, Output $output): int
{
$errorsArray = [];
Expand All @@ -30,7 +39,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
)
),
'location' => [
'path' => $fileSpecificError->getFile(),
'path' => $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()),
'lines' => [
'begin' => $fileSpecificError->getLine(),
],
Expand Down
21 changes: 11 additions & 10 deletions tests/PHPStan/Command/ErrorFormatter/GitlabFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Command\ErrorFormatter;

use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Testing\ErrorFormatterTestCase;

class GitlabFormatterTest extends ErrorFormatterTestCase
Expand All @@ -27,7 +28,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Foo",
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
"location": {
"path": "/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"lines": {
"begin": 4
}
Expand Down Expand Up @@ -65,7 +66,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Bar",
"fingerprint": "d112f1651daa597592156359ef28c9a4b81a8a96dbded1c0f1009f5bbc2bda97",
"location": {
"path": "/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"lines": {
"begin": 2
}
Expand All @@ -75,7 +76,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Foo",
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
"location": {
"path": "/data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"path": "with space/and unicode 😃/project/folder with unicode 😃/file name with \"spaces\" and unicode 😃.php",
"lines": {
"begin": 4
}
Expand All @@ -85,7 +86,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Foo",
"fingerprint": "93c79740ed8c6fbaac2087e54d6f6f67fc0918e3ff77840530f32e19857ef63c",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/foo.php",
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
"begin": 1
}
Expand All @@ -95,7 +96,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Bar",
"fingerprint": "d83022ee5bc7c71b6a4988ec47a377c9998b929d12d86fc71b745ec2b04c81e5",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/foo.php",
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
"begin": 5
}
Expand Down Expand Up @@ -143,7 +144,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Bar",
"fingerprint": "d112f1651daa597592156359ef28c9a4b81a8a96dbded1c0f1009f5bbc2bda97",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/folder with unicode \ud83d\ude03/file name with \"spaces\" and unicode \ud83d\ude03.php",
"path": "with space/and unicode \ud83d\ude03/project/folder with unicode \ud83d\ude03/file name with \"spaces\" and unicode \ud83d\ude03.php",
"lines": {
"begin": 2
}
Expand All @@ -153,7 +154,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Foo",
"fingerprint": "e82b7e1f1d4255352b19ecefa9116a12f129c7edb4351cf2319285eccdb1565e",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/folder with unicode \ud83d\ude03/file name with \"spaces\" and unicode \ud83d\ude03.php",
"path": "with space/and unicode \ud83d\ude03/project/folder with unicode \ud83d\ude03/file name with \"spaces\" and unicode \ud83d\ude03.php",
"lines": {
"begin": 4
}
Expand All @@ -163,7 +164,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Foo",
"fingerprint": "93c79740ed8c6fbaac2087e54d6f6f67fc0918e3ff77840530f32e19857ef63c",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/foo.php",
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
"begin": 1
}
Expand All @@ -173,7 +174,7 @@ public function dataFormatterOutputProvider(): iterable
"description": "Bar",
"fingerprint": "d83022ee5bc7c71b6a4988ec47a377c9998b929d12d86fc71b745ec2b04c81e5",
"location": {
"path": "/data/folder/with space/and unicode \ud83d\ude03/project/foo.php",
"path": "with space/and unicode \ud83d\ude03/project/foo.php",
"lines": {
"begin": 5
}
Expand Down Expand Up @@ -221,7 +222,7 @@ public function testFormatErrors(
string $expected
): void
{
$formatter = new GitlabErrorFormatter();
$formatter = new GitlabErrorFormatter(new SimpleRelativePathHelper('/data/folder'));

$this->assertSame($exitCode, $formatter->formatErrors(
$this->getAnalysisResult($numFileErrors, $numGenericErrors),
Expand Down

0 comments on commit 3f0efd0

Please sign in to comment.