Skip to content

Commit 6d020d3

Browse files
author
Tomas Pospisil
committed
update code
1 parent e9062d9 commit 6d020d3

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

example/phpstan.html

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
body {
55
background-color: #34495e;
66
color: white;
7-
font-family: sans-serif;
7+
font-family: monospace;
8+
font-size: 16px;
9+
}
10+
11+
b {
12+
color: lightskyblue;
813
}
914

1015
h1 {
@@ -24,23 +29,35 @@
2429
color: yellow;
2530
}
2631

32+
table {
33+
width: 100%;
34+
font-size: 12px;
35+
}
36+
2737
th {
2838
text-align: left;
2939
padding: 25px 0 10px 0;
3040
}
3141

42+
td {
43+
padding: 5px;
44+
}
45+
3246
.section {
3347
font-size: 1.4em;
3448
}
3549

3650
.line {
3751
text-align: right;
38-
vertical-align: top;
39-
padding: 1px 10px 0 0px;
52+
vertical-align: middle;
4053
}
4154

42-
tr:nth-child(even) {
43-
background: #475a73
55+
.error:nth-child(odd) {
56+
background: #374d63
57+
}
58+
59+
.regex {
60+
color: #00000085;
4461
}
4562

4663
#thanks {
@@ -52,7 +69,7 @@
5269
<title>PHPStan analysis result</title>
5370
</head>
5471
<h1>PHPStan analysis result</h1>
55-
<div class="time">2018-11-02 01:46:21</div>
72+
<div class="time">2019-12-10 17:40:27</div>
5673

5774

5875
<div>
@@ -64,29 +81,35 @@ <h1>PHPStan analysis result</h1>
6481
</thead>
6582
<thead>
6683
<tr>
67-
<th colspan="2">C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php (3×)</th>
84+
<th colspan="2">C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php (3×)</th>
6885
</tr>
6986
</thead>
70-
<tr>
87+
<tr class="error">
7188
<td class="line">
72-
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=25'>25</a>
89+
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=25'>25</a>
7390
</td>
7491
<td>
75-
Method <b>noximo\MessedUpClass::doSOmething()</b> should return <b>string</b> but returns <b>int.</b> </td>
92+
Method <b>noximo\MessedUpClass::doSOmething()</b> should return <b>string</b> but returns <b>int.</b> <br>
93+
<small class="regex">- '#Method noximo\\MessedUpClass\:\:doSOmething\(\) should return string but returns int#'</small>
94+
</td>
7695
</tr>
77-
<tr>
96+
<tr class="error">
7897
<td class="line">
79-
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
98+
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
8099
</td>
81100
<td>
82-
PHPDoc tag <b>@param</b> for parameter <b>$problem</b> with type <b>string</b> is incompatible with native type <b>int</b> </td>
101+
PHPDoc tag <b>@param</b> for parameter <b>$problem</b> with type <b>string</b> is incompatible with native type <b>int.</b> <br>
102+
<small class="regex">- '#PHPDoc tag @param for parameter \$problem with type string is incompatible with native type int#'</small>
103+
</td>
83104
</tr>
84-
<tr>
105+
<tr class="error">
85106
<td class="line">
86-
<a href='editor://open/?file=C:\wamp64\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
107+
<a href='editor://open/?file=C:\wamp\www\PHPStanFileOutput\tests\MessedUpClass.php&line=23'>23</a>
87108
</td>
88109
<td>
89-
PHPDoc tag <b>@return</b> with type <b>int</b> is incompatible with native type <b>string</b> </td>
110+
PHPDoc tag <b>@return</b> with type <b>int</b> is incompatible with native type <b>string.</b> <br>
111+
<small class="regex">- '#PHPDoc tag @return with type int is incompatible with native type string#'</small>
112+
</td>
90113
</tr>
91114

92115
</table>

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
services:
22
errorFormatter.fileoutput:
3-
class: noximo\FileOutput(./example/phpstan.html, null)
3+
factory: noximo\FileOutput(./example/phpstan.html, null)

src/FileOutput.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
use Nette\Utils\Strings;
1111
use PHPStan\Command\AnalysisResult;
1212
use PHPStan\Command\ErrorFormatter\ErrorFormatter;
13-
use Symfony\Component\Console\Style\OutputStyle;
13+
use PHPStan\Command\Output;
14+
use Safe\Exceptions\DirException;
1415
use Webmozart\PathUtil\Path;
1516

16-
class FileOutput implements ErrorFormatter
17+
final class FileOutput implements ErrorFormatter
1718
{
1819
/** @var string */
1920
public const ERROR = 'error';
@@ -53,7 +54,7 @@ class FileOutput implements ErrorFormatter
5354

5455
/**
5556
* FileOutput constructor.
56-
* @throws \Safe\Exceptions\DirException
57+
* @throws DirException
5758
*/
5859
public function __construct(string $outputFile, ?ErrorFormatter $defaultFormatterClass = null, ?string $customTemplate = null)
5960
{
@@ -78,28 +79,31 @@ public function __construct(string $outputFile, ?ErrorFormatter $defaultFormatte
7879
* Formats the errors and outputs them to the console.
7980
* @return int Error code.
8081
*/
81-
public function formatErrors(AnalysisResult $analysisResult, OutputStyle $style): int
82+
public function formatErrors(AnalysisResult $analysisResult, Output $output): int
8283
{
8384
if ($this->defaultFormatter !== null) {
84-
$this->defaultFormatter->formatErrors($analysisResult, $style);
85+
$this->defaultFormatter->formatErrors($analysisResult, $output);
8586
}
8687
try {
8788
$this->generateFile($analysisResult);
88-
$style->writeln('Note: Analysis outputted into file ' . $this->outputFile . '.');
89+
$output->getStyle()->note('Note: Analysis outputted into file ' . $this->outputFile . '.');
8990
} catch (IOException $e) {
90-
$style->error('Analysis could not be outputted into file. ' . $e->getMessage());
91+
$output->getStyle()->error('Analysis could not be outputted into file. ' . $e->getMessage());
9192
}
9293

9394
return $analysisResult->hasErrors() ? 1 : 0;
9495
}
9596

9697
/**
9798
* @param mixed[] $data
99+
* @return string
98100
*/
99101
public function getTable(array $data): string
100102
{
101-
ob_start(function (): void {
102-
});
103+
ob_start(
104+
static function (): void {
105+
}
106+
);
103107
require $this->template;
104108

105109
$output = ob_get_clean();
@@ -131,9 +135,12 @@ private function generateFile(AnalysisResult $analysisResult): void
131135
}
132136

133137
foreach ($output[self::FILES] as &$file) {
134-
usort($file, function ($a, $b) {
135-
return -1 * ($a[self::LINE] <=> $b[self::LINE]);
136-
});
138+
usort(
139+
$file,
140+
static function ($a, $b) {
141+
return -1 * ($a[self::LINE] <=> $b[self::LINE]);
142+
}
143+
);
137144
}
138145
unset($file);
139146
}
@@ -144,21 +151,23 @@ private function generateFile(AnalysisResult $analysisResult): void
144151
private static function formatMessage(string $message): string
145152
{
146153
$words = explode(' ', $message);
147-
$words = array_map(function ($word) {
148-
if (Strings::match($word, '/[^a-zA-Z,.]|(string)|(bool)|(boolean)|(int)|(integer)|(float)/')) {
149-
$word = '<b>' . $word . '</b>';
150-
}
154+
$words = array_map(
155+
static function ($word) {
156+
if (Strings::match($word, '/[^a-zA-Z,.]|(string)|(bool)|(boolean)|(int)|(integer)|(float)/')) {
157+
$word = '<b>' . $word . '</b>';
158+
}
151159

152-
return $word;
153-
}, $words);
160+
return $word;
161+
},
162+
$words
163+
);
154164

155165
return implode(' ', $words);
156166
}
157167

158168
/**
159169
* @param string $message
160170
* @return string
161-
* @throws RegexpException
162171
*/
163172
private static function formatRegex(string $message): string
164173
{

0 commit comments

Comments
 (0)