1010use PHPStan \Command \Output ;
1111use PHPStan \File \RelativePathHelper ;
1212
13- final class TicketSwapErrorFormatter implements ErrorFormatter
13+ final readonly class TicketSwapErrorFormatter implements ErrorFormatter
1414{
1515 private const FORMAT = "{message} \n{links} " ;
1616 private const LINK_FORMAT_DEFAULT = "↳ <href={editorUrl}>{shortPath}:{line}</> \n" ;
@@ -19,50 +19,24 @@ final class TicketSwapErrorFormatter implements ErrorFormatter
1919 private const LINK_FORMAT_PHPSTORM = "↳ file://{absolutePath}:{line} \n" ;
2020 private const LINK_FORMAT_WITHOUT_EDITOR = "↳ {relativePath}:{line} \n" ;
2121
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 ;
22+ private string $ linkFormat ;
4123
4224 public function __construct (
43- RelativePathHelper $ relativePathHelper ,
44- CiDetectedErrorFormatter $ ciDetectedErrorFormatter ,
45- ?string $ editorUrl = null
25+ private RelativePathHelper $ relativePathHelper ,
26+ private CiDetectedErrorFormatter $ ciDetectedErrorFormatter ,
27+ private ?string $ editorUrl,
4628 ) {
47- $ this ->editorUrl = $ editorUrl ;
48- $ this ->ciDetectedErrorFormatter = $ ciDetectedErrorFormatter ;
49- $ this ->relativePathHelper = $ relativePathHelper ;
5029 $ this ->linkFormat = self ::getLinkFormatFromEnv ();
5130 }
5231
5332 public static function getLinkFormatFromEnv () : string
5433 {
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 ;
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+ };
6640 }
6741
6842 public function formatErrors (AnalysisResult $ analysisResult , Output $ output ) : int
@@ -78,7 +52,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
7852 $ output ->writeLineFormatted (
7953 sprintf (
8054 '<unknown location> %s ' ,
81- $ notFileSpecificError
55+ $ notFileSpecificError,
8256 )
8357 );
8458 }
@@ -101,7 +75,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
10175 $ error ->getTip ()
10276 ) : null ,
10377 $ error ->getIdentifier (),
104- $ output ->isDecorated ()
78+ $ output ->isDecorated (),
10579 ),
10680 '{identifier} ' => $ error ->getIdentifier (),
10781 '{links} ' => implode ([
@@ -111,19 +85,19 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
11185 $ error ->getFilePath (),
11286 $ this ->relativePathHelper ->getRelativePath ($ error ->getFilePath ()),
11387 $ this ->editorUrl ,
114- $ output ->isDecorated ()
88+ $ output ->isDecorated (),
11589 ),
11690 $ error ->getTraitFilePath () !== null ? $ this ::link (
11791 $ this ->linkFormat ,
11892 (int ) $ error ->getLine (),
11993 $ error ->getTraitFilePath (),
12094 $ this ->relativePathHelper ->getRelativePath ($ error ->getTraitFilePath ()),
12195 $ this ->editorUrl ,
122- $ output ->isDecorated ()
96+ $ output ->isDecorated (),
12397 ) : '' ,
12498 ]),
125- ]
126- )
99+ ],
100+ ),
127101 );
128102 }
129103
@@ -136,7 +110,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
136110 sprintf (
137111 '<bg=red;options=bold>Found %d error%s</> ' ,
138112 $ analysisResult ->getTotalErrorsCount (),
139- $ analysisResult ->getTotalErrorsCount () === 1 ? '' : 's '
113+ $ analysisResult ->getTotalErrorsCount () === 1 ? '' : 's ' ,
140114 )
141115 );
142116 $ output ->writeLineFormatted ('' );
@@ -152,7 +126,7 @@ public static function link(
152126 string $ absolutePath ,
153127 string $ relativePath ,
154128 ?string $ editorUrl ,
155- bool $ isDecorated
129+ bool $ isDecorated,
156130 ) : string {
157131 if (!$ isDecorated || $ editorUrl === null ) {
158132 $ format = self ::LINK_FORMAT_WITHOUT_EDITOR ;
@@ -165,12 +139,12 @@ public static function link(
165139 '{editorUrl} ' => $ editorUrl === null ? '' : str_replace (
166140 ['%relFile% ' , '%file% ' , '%line% ' ],
167141 [$ relativePath , $ absolutePath , $ line ],
168- $ editorUrl
142+ $ editorUrl,
169143 ),
170144 '{relativePath} ' => $ relativePath ,
171145 '{shortPath} ' => self ::trimPath ($ relativePath ),
172146 '{line} ' => $ line ,
173- ]
147+ ],
174148 );
175149 }
176150
@@ -183,11 +157,11 @@ private static function trimPath(string $path) : string
183157
184158 return implode (
185159 DIRECTORY_SEPARATOR ,
186- array_merge (
187- array_slice ($ parts , 0 , 3 ),
188- [ '... ' ] ,
189- array_slice ($ parts , -2 )
190- )
160+ [
161+ ... array_slice ($ parts , 0 , 3 ),
162+ '... ' ,
163+ ... array_slice ($ parts , -2 ),
164+ ],
191165 );
192166 }
193167
@@ -197,7 +171,7 @@ public static function highlight(string $message, ?string $tip, ?string $identif
197171 return $ message ;
198172 }
199173
200- if (strpos ($ message , 'Ignored error pattern ' ) === 0 ) {
174+ if (str_starts_with ($ message , 'Ignored error pattern ' )) {
201175 return $ message ;
202176 }
203177
@@ -208,42 +182,42 @@ public static function highlight(string $message, ?string $tip, ?string $identif
208182 $ message = (string ) preg_replace (
209183 "/([A-Z0-9]{1}[A-Za-z0-9_\-]+[ \\\]+[A-Z0-9]{1}[A-Za-z0-9_\- \\\]+)/ " ,
210184 '<fg=yellow>$1</> ' ,
211- $ message
185+ $ message,
212186 );
213187
214188 // Quoted strings
215189 $ message = (string ) preg_replace (
216190 "/(?<=[ \"'])([A-Za-z0-9_\- \\\]+)(?=[ \"'])/ " ,
217191 '<fg=yellow>$1</> ' ,
218- $ message
192+ $ message,
219193 );
220194
221195 // Variable
222196 $ message = (string ) preg_replace (
223197 "/(?<=[:]{2}|[\s \"\(])([.]{3})?( \\$[A-Za-z0-9_ \\-]+)(?=[\s| \"|\)]|$)/ " ,
224198 '<fg=green>$1$2</> ' ,
225- $ message
199+ $ message,
226200 );
227201
228202 // Method
229203 $ message = (string ) preg_replace (
230204 '/(?<=[:]{2}|[\s])(\w+\(\))/ ' ,
231205 '<fg=blue>$1</> ' ,
232- $ message
206+ $ message,
233207 );
234208
235209 // Function
236210 $ message = (string ) preg_replace (
237211 '/(?<=function\s)(\w+)(?=\s)/ ' ,
238212 '<fg=blue>$1</> ' ,
239- $ message
213+ $ message,
240214 );
241215
242216 // Types
243217 $ message = (string ) preg_replace (
244218 '/(?<=[\s\|\(><])(null|true|false|int|float|bool|([-\w]+-)?string|array|object|mixed|resource|iterable|void|callable)(?=[:]{2}|[\.\s\|><,\(\)\{\}]+)/ ' ,
245219 '<fg=magenta>$1</> ' ,
246- $ message
220+ $ message,
247221 );
248222
249223 if ($ tip !== null ) {
0 commit comments