Skip to content

Commit 2a4d410

Browse files
authored
Highlight Class::Constant references in error messages (#32)
1 parent 0e2c613 commit 2a4d410

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/TicketSwapErrorFormatter.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public static function highlight(string $message, ?string $tip, ?string $identif
221221

222222
// Full Qualified Class Names
223223
$message = (string) preg_replace(
224-
"/([\\\]?[A-Z0-9]{1}[A-Za-z0-9_\-]+[\\\]+[A-Z0-9]{1}[A-Za-z0-9_\-\\\]+)/",
224+
"/([\\\]?[A-Z0-9]{1}[A-Za-z0-9_\-]+[\\\]+[A-Z0-9]{1}[A-Za-z0-9_\-\\\]+(?:::[A-Za-z0-9_]+)?)/",
225225
'<fg=yellow>$1</>',
226226
$message,
227227
);
@@ -247,6 +247,13 @@ public static function highlight(string $message, ?string $tip, ?string $identif
247247
$message,
248248
);
249249

250+
// Class reference (e.g. ClassName::CONSTANT, ClassName::class)
251+
$message = (string) preg_replace(
252+
'/(?<=[\s])([A-Z][A-Za-z0-9_]+::[A-Za-z0-9_]+)(?=[\.\s\|><,\(\)\{\}]|$)/',
253+
'<fg=yellow>$1</>',
254+
$message,
255+
);
256+
250257
// Function
251258
$message = (string) preg_replace(
252259
'/(?<=function\s)(\w+)(?=\s)/',

tests/TicketSwapErrorFormatterTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public static function provideHighlight() : iterable
262262
true
263263
];
264264
yield [
265-
"Array has 3 duplicate keys with value '<fg=yellow>App\Activity</>' (<fg=yellow>\App\Activity</>::class).",
265+
"Array has 3 duplicate keys with value '<fg=yellow>App\Activity</>' (<fg=yellow>\App\Activity::class</>).",
266266
"Array has 3 duplicate keys with value 'App\Activity' (\App\Activity::class).",
267267
null,
268268
null,
@@ -282,6 +282,27 @@ public static function provideHighlight() : iterable
282282
null,
283283
true,
284284
];
285+
yield [
286+
'Something <fg=yellow>CommandHandlersHaveSerializableCommandFunctionalTest::SKIPPED</>.',
287+
'Something CommandHandlersHaveSerializableCommandFunctionalTest::SKIPPED.',
288+
null,
289+
null,
290+
true,
291+
];
292+
yield [
293+
'Constant <fg=yellow>App\Models\ExampleModel::SOME_CONSTANT</> is never used.',
294+
'Constant App\Models\ExampleModel::SOME_CONSTANT is never used.',
295+
null,
296+
null,
297+
true,
298+
];
299+
yield [
300+
'Case <fg=yellow>App\Enum\Status::Active</> not handled.',
301+
'Case App\Enum\Status::Active not handled.',
302+
null,
303+
null,
304+
true,
305+
];
285306
}
286307

287308
/**

0 commit comments

Comments
 (0)