Skip to content

Commit 7532a89

Browse files
authored
Widen PHP support (#10)
* widen php support * expand action matrix
1 parent f93b096 commit 7532a89

File tree

4 files changed

+74
-36
lines changed

4 files changed

+74
-36
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ concurrency:
1010

1111
jobs:
1212
phpstan:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
php:
17+
- '7.2'
18+
- '7.3'
19+
- '7.4'
20+
- '8.0'
21+
- '8.1'
22+
- '8.2'
23+
- '8.3'
24+
1325
runs-on: ubuntu-latest
1426

1527
steps:
@@ -19,7 +31,7 @@ jobs:
1931
- name: Set up PHP
2032
uses: shivammathur/setup-php@v2
2133
with:
22-
php-version: '8.2'
34+
php-version: ${{ matrix.php }}
2335

2436
- name: Install Composer packages
2537
uses: ramsey/composer-install@v3

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"keywords": ["phpstan", "dev"],
55
"type": "phpstan-extension",
66
"require": {
7-
"php": "^8.2",
7+
"php": "^7.2|^8.0",
88
"phpstan/phpstan": "^1.12.4"
99
},
1010
"license": "MIT",

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ parameters:
44
level: 8
55
paths:
66
- src
7-
phpVersion: 80200
7+
phpVersion: 70200
88
errorFormat: ticketswap
99
editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%'

src/TicketSwapErrorFormatter.php

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPStan\Command\Output;
1111
use PHPStan\File\RelativePathHelper;
1212

13-
final readonly class TicketSwapErrorFormatter implements ErrorFormatter
13+
final class TicketSwapErrorFormatter implements ErrorFormatter
1414
{
1515
private const FORMAT = "{message}\n{links}";
1616
private const LINK_FORMAT_DEFAULT = "↳ <href={editorUrl}>{shortPath}:{line}</>\n";
@@ -19,24 +19,50 @@
1919
private const LINK_FORMAT_PHPSTORM = "↳ file://{absolutePath}:{line}\n";
2020
private const LINK_FORMAT_WITHOUT_EDITOR = "↳ {relativePath}:{line}\n";
2121

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;
2341

2442
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
2846
) {
47+
$this->editorUrl = $editorUrl;
48+
$this->ciDetectedErrorFormatter = $ciDetectedErrorFormatter;
49+
$this->relativePathHelper = $relativePathHelper;
2950
$this->linkFormat = self::getLinkFormatFromEnv();
3051
}
3152

3253
public static function getLinkFormatFromEnv() : string
3354
{
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;
4066
}
4167

4268
public function formatErrors(AnalysisResult $analysisResult, Output $output) : int
@@ -52,7 +78,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
5278
$output->writeLineFormatted(
5379
sprintf(
5480
'<unknown location> %s',
55-
$notFileSpecificError,
81+
$notFileSpecificError
5682
)
5783
);
5884
}
@@ -75,7 +101,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
75101
$error->getTip()
76102
) : null,
77103
$error->getIdentifier(),
78-
$output->isDecorated(),
104+
$output->isDecorated()
79105
),
80106
'{identifier}' => $error->getIdentifier(),
81107
'{links}' => implode([
@@ -85,19 +111,19 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
85111
$error->getFilePath(),
86112
$this->relativePathHelper->getRelativePath($error->getFilePath()),
87113
$this->editorUrl,
88-
$output->isDecorated(),
114+
$output->isDecorated()
89115
),
90116
$error->getTraitFilePath() !== null ? $this::link(
91117
$this->linkFormat,
92118
(int) $error->getLine(),
93119
$error->getTraitFilePath(),
94120
$this->relativePathHelper->getRelativePath($error->getTraitFilePath()),
95121
$this->editorUrl,
96-
$output->isDecorated(),
122+
$output->isDecorated()
97123
) : '',
98124
]),
99-
],
100-
),
125+
]
126+
)
101127
);
102128
}
103129

@@ -110,7 +136,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output) : i
110136
sprintf(
111137
'<bg=red;options=bold>Found %d error%s</>',
112138
$analysisResult->getTotalErrorsCount(),
113-
$analysisResult->getTotalErrorsCount() === 1 ? '' : 's',
139+
$analysisResult->getTotalErrorsCount() === 1 ? '' : 's'
114140
)
115141
);
116142
$output->writeLineFormatted('');
@@ -126,7 +152,7 @@ public static function link(
126152
string $absolutePath,
127153
string $relativePath,
128154
?string $editorUrl,
129-
bool $isDecorated,
155+
bool $isDecorated
130156
) : string {
131157
if (!$isDecorated || $editorUrl === null) {
132158
$format = self::LINK_FORMAT_WITHOUT_EDITOR;
@@ -139,12 +165,12 @@ public static function link(
139165
'{editorUrl}' => $editorUrl === null ? '' : str_replace(
140166
['%relFile%', '%file%', '%line%'],
141167
[$relativePath, $absolutePath, $line],
142-
$editorUrl,
168+
$editorUrl
143169
),
144170
'{relativePath}' => $relativePath,
145171
'{shortPath}' => self::trimPath($relativePath),
146172
'{line}' => $line,
147-
],
173+
]
148174
);
149175
}
150176

@@ -157,11 +183,11 @@ private static function trimPath(string $path) : string
157183

158184
return implode(
159185
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+
)
165191
);
166192
}
167193

@@ -171,7 +197,7 @@ public static function highlight(string $message, ?string $tip, ?string $identif
171197
return $message;
172198
}
173199

174-
if (str_starts_with($message, 'Ignored error pattern')) {
200+
if (strpos($message, 'Ignored error pattern') === 0) {
175201
return $message;
176202
}
177203

@@ -182,42 +208,42 @@ public static function highlight(string $message, ?string $tip, ?string $identif
182208
$message = (string) preg_replace(
183209
"/([A-Z0-9]{1}[A-Za-z0-9_\-]+[\\\]+[A-Z0-9]{1}[A-Za-z0-9_\-\\\]+)/",
184210
'<fg=yellow>$1</>',
185-
$message,
211+
$message
186212
);
187213

188214
// Quoted strings
189215
$message = (string) preg_replace(
190216
"/(?<=[\"'])([A-Za-z0-9_\-\\\]+)(?=[\"'])/",
191217
'<fg=yellow>$1</>',
192-
$message,
218+
$message
193219
);
194220

195221
// Variable
196222
$message = (string) preg_replace(
197223
"/(?<=[:]{2}|[\s\"\(])([.]{3})?(\\$[A-Za-z0-9_\\-]+)(?=[\s|\"|\)]|$)/",
198224
'<fg=green>$1$2</>',
199-
$message,
225+
$message
200226
);
201227

202228
// Method
203229
$message = (string) preg_replace(
204230
'/(?<=[:]{2}|[\s])(\w+\(\))/',
205231
'<fg=blue>$1</>',
206-
$message,
232+
$message
207233
);
208234

209235
// Function
210236
$message = (string) preg_replace(
211237
'/(?<=function\s)(\w+)(?=\s)/',
212238
'<fg=blue>$1</>',
213-
$message,
239+
$message
214240
);
215241

216242
// Types
217243
$message = (string) preg_replace(
218244
'/(?<=[\s\|\(><])(null|true|false|int|float|bool|([-\w]+-)?string|array|object|mixed|resource|iterable|void|callable)(?=[:]{2}|[\.\s\|><,\(\)\{\}]+)/',
219245
'<fg=magenta>$1</>',
220-
$message,
246+
$message
221247
);
222248

223249
if ($tip !== null) {

0 commit comments

Comments
 (0)