10
10
use PHPStan \Command \Output ;
11
11
use PHPStan \File \RelativePathHelper ;
12
12
13
- final readonly class TicketSwapErrorFormatter implements ErrorFormatter
13
+ final class TicketSwapErrorFormatter implements ErrorFormatter
14
14
{
15
15
private const FORMAT = "{message} \n{links} " ;
16
16
private const LINK_FORMAT_DEFAULT = "↳ <href={editorUrl}>{shortPath}:{line}</> \n" ;
19
19
private const LINK_FORMAT_PHPSTORM = "↳ file://{absolutePath}:{line} \n" ;
20
20
private const LINK_FORMAT_WITHOUT_EDITOR = "↳ {relativePath}:{line} \n" ;
21
21
22
- private string $ linkFormat ;
22
+ /**
23
+ * @var string
24
+ */
25
+ private $ linkFormat ;
26
+
27
+ /**
28
+ * @var RelativePathHelper
29
+ */
30
+ private $ relativePathHelper ;
31
+
32
+ /**
33
+ * @var CiDetectedErrorFormatter
34
+ */
35
+ private $ ciDetectedErrorFormatter ;
36
+
37
+ /**
38
+ * @var string|null
39
+ */
40
+ private $ editorUrl ;
23
41
24
42
public function __construct (
25
- private RelativePathHelper $ relativePathHelper ,
26
- private CiDetectedErrorFormatter $ ciDetectedErrorFormatter ,
27
- private ?string $ editorUrl,
43
+ RelativePathHelper $ relativePathHelper ,
44
+ CiDetectedErrorFormatter $ ciDetectedErrorFormatter ,
45
+ ?string $ editorUrl = null
28
46
) {
47
+ $ this ->editorUrl = $ editorUrl ;
48
+ $ this ->ciDetectedErrorFormatter = $ ciDetectedErrorFormatter ;
49
+ $ this ->relativePathHelper = $ relativePathHelper ;
29
50
$ this ->linkFormat = self ::getLinkFormatFromEnv ();
30
51
}
31
52
32
53
public static function getLinkFormatFromEnv () : string
33
54
{
34
- return match (true ) {
35
- getenv ('GITHUB_ACTIONS ' ) !== false => self ::LINK_FORMAT_GITHUB_ACTIONS ,
36
- getenv ('TERMINAL_EMULATOR ' ) === 'JetBrains-JediTerm ' => self ::LINK_FORMAT_PHPSTORM ,
37
- getenv ('TERM_PROGRAM ' ) === 'WarpTerminal ' => self ::LINK_FORMAT_WARP ,
38
- default => self ::LINK_FORMAT_DEFAULT ,
39
- };
55
+ if (getenv ('GITHUB_ACTIONS ' ) !== false ) {
56
+ return self ::LINK_FORMAT_GITHUB_ACTIONS ;
57
+ }
58
+ if (getenv ('TERMINAL_EMULATOR ' ) !== 'JetBrains-JediTerm ' ) {
59
+ return self ::LINK_FORMAT_PHPSTORM ;
60
+ }
61
+ if (getenv ('TERM_PROGRAM ' ) !== 'WarpTerminal ' ) {
62
+ return self ::LINK_FORMAT_WARP ;
63
+ }
64
+
65
+ return self ::LINK_FORMAT_DEFAULT ;
40
66
}
41
67
42
68
public function formatErrors (AnalysisResult $ analysisResult , Output $ output ) : int
@@ -52,7 +78,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
52
78
$ output ->writeLineFormatted (
53
79
sprintf (
54
80
'<unknown location> %s ' ,
55
- $ notFileSpecificError,
81
+ $ notFileSpecificError
56
82
)
57
83
);
58
84
}
@@ -75,7 +101,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
75
101
$ error ->getTip ()
76
102
) : null ,
77
103
$ error ->getIdentifier (),
78
- $ output ->isDecorated (),
104
+ $ output ->isDecorated ()
79
105
),
80
106
'{identifier} ' => $ error ->getIdentifier (),
81
107
'{links} ' => implode ([
@@ -85,19 +111,19 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
85
111
$ error ->getFilePath (),
86
112
$ this ->relativePathHelper ->getRelativePath ($ error ->getFilePath ()),
87
113
$ this ->editorUrl ,
88
- $ output ->isDecorated (),
114
+ $ output ->isDecorated ()
89
115
),
90
116
$ error ->getTraitFilePath () !== null ? $ this ::link (
91
117
$ this ->linkFormat ,
92
118
(int ) $ error ->getLine (),
93
119
$ error ->getTraitFilePath (),
94
120
$ this ->relativePathHelper ->getRelativePath ($ error ->getTraitFilePath ()),
95
121
$ this ->editorUrl ,
96
- $ output ->isDecorated (),
122
+ $ output ->isDecorated ()
97
123
) : '' ,
98
124
]),
99
- ],
100
- ),
125
+ ]
126
+ )
101
127
);
102
128
}
103
129
@@ -110,7 +136,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
110
136
sprintf (
111
137
'<bg=red;options=bold>Found %d error%s</> ' ,
112
138
$ analysisResult ->getTotalErrorsCount (),
113
- $ analysisResult ->getTotalErrorsCount () === 1 ? '' : 's ' ,
139
+ $ analysisResult ->getTotalErrorsCount () === 1 ? '' : 's '
114
140
)
115
141
);
116
142
$ output ->writeLineFormatted ('' );
@@ -126,7 +152,7 @@ public static function link(
126
152
string $ absolutePath ,
127
153
string $ relativePath ,
128
154
?string $ editorUrl ,
129
- bool $ isDecorated,
155
+ bool $ isDecorated
130
156
) : string {
131
157
if (!$ isDecorated || $ editorUrl === null ) {
132
158
$ format = self ::LINK_FORMAT_WITHOUT_EDITOR ;
@@ -139,12 +165,12 @@ public static function link(
139
165
'{editorUrl} ' => $ editorUrl === null ? '' : str_replace (
140
166
['%relFile% ' , '%file% ' , '%line% ' ],
141
167
[$ relativePath , $ absolutePath , $ line ],
142
- $ editorUrl,
168
+ $ editorUrl
143
169
),
144
170
'{relativePath} ' => $ relativePath ,
145
171
'{shortPath} ' => self ::trimPath ($ relativePath ),
146
172
'{line} ' => $ line ,
147
- ],
173
+ ]
148
174
);
149
175
}
150
176
@@ -157,11 +183,11 @@ private static function trimPath(string $path) : string
157
183
158
184
return implode (
159
185
DIRECTORY_SEPARATOR ,
160
- [
161
- ... array_slice ($ parts , 0 , 3 ),
162
- '... ' ,
163
- ... array_slice ($ parts , -2 ),
164
- ],
186
+ array_merge (
187
+ array_slice ($ parts , 0 , 3 ),
188
+ [ '... ' ] ,
189
+ array_slice ($ parts , -2 )
190
+ )
165
191
);
166
192
}
167
193
@@ -171,7 +197,7 @@ public static function highlight(string $message, ?string $tip, ?string $identif
171
197
return $ message ;
172
198
}
173
199
174
- if (str_starts_with ($ message , 'Ignored error pattern ' )) {
200
+ if (strpos ($ message , 'Ignored error pattern ' ) === 0 ) {
175
201
return $ message ;
176
202
}
177
203
@@ -182,42 +208,42 @@ public static function highlight(string $message, ?string $tip, ?string $identif
182
208
$ message = (string ) preg_replace (
183
209
"/([A-Z0-9]{1}[A-Za-z0-9_\-]+[ \\\]+[A-Z0-9]{1}[A-Za-z0-9_\- \\\]+)/ " ,
184
210
'<fg=yellow>$1</> ' ,
185
- $ message,
211
+ $ message
186
212
);
187
213
188
214
// Quoted strings
189
215
$ message = (string ) preg_replace (
190
216
"/(?<=[ \"'])([A-Za-z0-9_\- \\\]+)(?=[ \"'])/ " ,
191
217
'<fg=yellow>$1</> ' ,
192
- $ message,
218
+ $ message
193
219
);
194
220
195
221
// Variable
196
222
$ message = (string ) preg_replace (
197
223
"/(?<=[:]{2}|[\s \"\(])([.]{3})?( \\$[A-Za-z0-9_ \\-]+)(?=[\s| \"|\)]|$)/ " ,
198
224
'<fg=green>$1$2</> ' ,
199
- $ message,
225
+ $ message
200
226
);
201
227
202
228
// Method
203
229
$ message = (string ) preg_replace (
204
230
'/(?<=[:]{2}|[\s])(\w+\(\))/ ' ,
205
231
'<fg=blue>$1</> ' ,
206
- $ message,
232
+ $ message
207
233
);
208
234
209
235
// Function
210
236
$ message = (string ) preg_replace (
211
237
'/(?<=function\s)(\w+)(?=\s)/ ' ,
212
238
'<fg=blue>$1</> ' ,
213
- $ message,
239
+ $ message
214
240
);
215
241
216
242
// Types
217
243
$ message = (string ) preg_replace (
218
244
'/(?<=[\s\|\(><])(null|true|false|int|float|bool|([-\w]+-)?string|array|object|mixed|resource|iterable|void|callable)(?=[:]{2}|[\.\s\|><,\(\)\{\}]+)/ ' ,
219
245
'<fg=magenta>$1</> ' ,
220
- $ message,
246
+ $ message
221
247
);
222
248
223
249
if ($ tip !== null ) {
0 commit comments