Skip to content

Commit 4793547

Browse files
committed
Some imports, adjustments and fixes
Ref: phpstan/phpstan#4122
1 parent c620c74 commit 4793547

File tree

11 files changed

+792
-5
lines changed

11 files changed

+792
-5
lines changed

src/AnalyserResult.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/*
4+
* (c) Copyright (c) 2016-2020 Ondřej Mirtes <[email protected]>
5+
*
6+
* This source file is subject to the MIT license.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
declare(strict_types = 1);
27+
28+
namespace CodeLts\CliTools;
29+
30+
class AnalyserResult
31+
{
32+
33+
/** @var \CodeLts\CliTools\Error[] */
34+
private array $unorderedErrors;
35+
36+
/** @var \CodeLts\CliTools\Error[] */
37+
private array $errors;
38+
39+
/** @var string[] */
40+
private array $internalErrors;
41+
42+
/**
43+
* @param \CodeLts\CliTools\Error[] $errors
44+
* @param string[] $internalErrors
45+
*/
46+
public function __construct(
47+
array $errors,
48+
array $internalErrors
49+
)
50+
{
51+
$this->unorderedErrors = $errors;
52+
53+
usort(
54+
$errors,
55+
static function (Error $a, Error $b): int {
56+
return [
57+
$a->getFile(),
58+
$a->getLine(),
59+
$a->getMessage(),
60+
] <=> [
61+
$b->getFile(),
62+
$b->getLine(),
63+
$b->getMessage(),
64+
];
65+
}
66+
);
67+
68+
$this->errors = $errors;
69+
$this->internalErrors = $internalErrors;
70+
}
71+
72+
/**
73+
* @return \CodeLts\CliTools\Error[]
74+
*/
75+
public function getUnorderedErrors(): array
76+
{
77+
return $this->unorderedErrors;
78+
}
79+
80+
/**
81+
* @return \CodeLts\CliTools\Error[]
82+
*/
83+
public function getErrors(): array
84+
{
85+
return $this->errors;
86+
}
87+
88+
/**
89+
* @return string[]
90+
*/
91+
public function getInternalErrors(): array
92+
{
93+
return $this->internalErrors;
94+
}
95+
96+
}

src/AnalysisResult.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/*
4+
* (c) Copyright (c) 2016-2020 Ondřej Mirtes <[email protected]>
5+
*
6+
* This source file is subject to the MIT license.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
declare(strict_types = 1);
27+
28+
namespace CodeLts\CliTools;
29+
30+
use CodeLts\CliTools\Error;
31+
32+
class AnalysisResult
33+
{
34+
35+
/** @var \CodeLts\CliTools\Error[] sorted by their file name, line number and message */
36+
private array $fileSpecificErrors;
37+
38+
/** @var string[] */
39+
private array $notFileSpecificErrors;
40+
41+
/** @var string[] */
42+
private array $internalErrors;
43+
44+
/** @var string[] */
45+
private array $warnings;
46+
47+
private bool $defaultLevelUsed;
48+
49+
private ?string $projectConfigFile;
50+
51+
private bool $savedResultCache;
52+
53+
/**
54+
* @param \CodeLts\CliTools\Error[] $fileSpecificErrors
55+
* @param string[] $notFileSpecificErrors
56+
* @param string[] $internalErrors
57+
* @param string[] $warnings
58+
* @param bool $defaultLevelUsed
59+
* @param string|null $projectConfigFile
60+
* @param bool $savedResultCache
61+
*/
62+
public function __construct(
63+
array $fileSpecificErrors,
64+
array $notFileSpecificErrors,
65+
array $internalErrors,
66+
array $warnings,
67+
bool $defaultLevelUsed,
68+
?string $projectConfigFile,
69+
bool $savedResultCache
70+
)
71+
{
72+
usort(
73+
$fileSpecificErrors,
74+
static function (Error $a, Error $b): int {
75+
return [
76+
$a->getFile(),
77+
$a->getLine(),
78+
$a->getMessage(),
79+
] <=> [
80+
$b->getFile(),
81+
$b->getLine(),
82+
$b->getMessage(),
83+
];
84+
}
85+
);
86+
87+
$this->fileSpecificErrors = $fileSpecificErrors;
88+
$this->notFileSpecificErrors = $notFileSpecificErrors;
89+
$this->internalErrors = $internalErrors;
90+
$this->warnings = $warnings;
91+
$this->defaultLevelUsed = $defaultLevelUsed;
92+
$this->projectConfigFile = $projectConfigFile;
93+
$this->savedResultCache = $savedResultCache;
94+
}
95+
96+
public function hasErrors(): bool
97+
{
98+
return $this->getTotalErrorsCount() > 0;
99+
}
100+
101+
public function getTotalErrorsCount(): int
102+
{
103+
return count($this->fileSpecificErrors) + count($this->notFileSpecificErrors);
104+
}
105+
106+
/**
107+
* @return \CodeLts\CliTools\Error[] sorted by their file name, line number and message
108+
*/
109+
public function getFileSpecificErrors(): array
110+
{
111+
return $this->fileSpecificErrors;
112+
}
113+
114+
/**
115+
* @return string[]
116+
*/
117+
public function getNotFileSpecificErrors(): array
118+
{
119+
return $this->notFileSpecificErrors;
120+
}
121+
122+
/**
123+
* @return string[]
124+
*/
125+
public function getInternalErrors(): array
126+
{
127+
return $this->internalErrors;
128+
}
129+
130+
/**
131+
* @return string[]
132+
*/
133+
public function getWarnings(): array
134+
{
135+
return $this->warnings;
136+
}
137+
138+
public function hasWarnings(): bool
139+
{
140+
return count($this->warnings) > 0;
141+
}
142+
143+
public function isDefaultLevelUsed(): bool
144+
{
145+
return $this->defaultLevelUsed;
146+
}
147+
148+
public function getProjectConfigFile(): ?string
149+
{
150+
return $this->projectConfigFile;
151+
}
152+
153+
public function hasInternalErrors(): bool
154+
{
155+
return count($this->internalErrors) > 0;
156+
}
157+
158+
public function isResultCacheSaved(): bool
159+
{
160+
return $this->savedResultCache;
161+
}
162+
163+
}

src/Error.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace CodeLts\CliTools;
4+
5+
class Error
6+
{
7+
/** @var string */
8+
protected $message;
9+
/** @var string|null */
10+
protected $file;
11+
/** @var int */
12+
protected $line;
13+
/** @var int */
14+
protected $severity;
15+
/** @var string|null */
16+
protected $tip = null;
17+
18+
public const LEVEL_EMERGENCY = 0;
19+
public const LEVEL_ALERT = 1;
20+
public const LEVEL_CRITICAL = 2;
21+
public const LEVEL_ERROR = 3;
22+
public const LEVEL_WARNING = 4;
23+
public const LEVEL_NOTICE = 5;
24+
public const LEVEL_INFO = 6;
25+
public const LEVEL_DEBUG = 7;
26+
27+
28+
public function __construct(
29+
string $message, ?string $file, int $line,
30+
int $severity = Error::LEVEL_ERROR, ?string $tip = null)
31+
{
32+
$this->message = $message;
33+
$this->file = $file;
34+
$this->line = $line;
35+
$this->severity = $severity;
36+
$this->tip = $tip;
37+
}
38+
39+
public function getMessage(): string
40+
{
41+
return $this->message;
42+
}
43+
44+
public function getFile(): ?string
45+
{
46+
return $this->file;
47+
}
48+
49+
public function getLine(): int
50+
{
51+
return $this->line;
52+
}
53+
54+
public function canBeIgnored(): bool
55+
{
56+
return $this->severity < Error::LEVEL_WARNING;
57+
}
58+
59+
public function getTip(): ?string
60+
{
61+
return $this->tip;
62+
}
63+
64+
}

src/ErrorFormatter/GitlabErrorFormatter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
namespace CodeLts\CliTools\ErrorFormatter;
2929

30-
use Nette\Utils\Json;
3130
use CodeLts\CliTools\AnalysisResult;
3231
use CodeLts\CliTools\Output;
3332
use CodeLts\CliTools\File\RelativePathHelper;
@@ -93,7 +92,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
9392
];
9493
}
9594

96-
$json = Json::encode($errorsArray, Json::PRETTY);
95+
$json = json_encode($errorsArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
9796

9897
$output->writeRaw($json);
9998

src/ErrorFormatter/JsonErrorFormatter.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
namespace CodeLts\CliTools\ErrorFormatter;
2929

30-
use Nette\Utils\Json;
3130
use CodeLts\CliTools\AnalysisResult;
3231
use CodeLts\CliTools\Output;
3332

@@ -76,7 +75,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
7675
$errorsArray['errors'][] = $notFileSpecificError;
7776
}
7877

79-
$json = Json::encode($errorsArray, $this->pretty ? Json::PRETTY : 0);
78+
$json = json_encode($errorsArray, $this->pretty ? JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT : JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
8079

8180
$output->writeRaw($json);
8281

src/ErrorFormatter/TableErrorFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function formatErrors(
7777
return 0;
7878
}
7979

80-
/** @var array<string, \PHPStan\Analyser\Error[]> $fileErrors */
80+
/** @var array<string, \CodeLts\CliTools\Error[]> $fileErrors */
8181
$fileErrors = [];
8282
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
8383
if (!isset($fileErrors[$fileSpecificError->getFile()])) {

0 commit comments

Comments
 (0)