-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorMessageFormatter.php
48 lines (41 loc) · 1.41 KB
/
ErrorMessageFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace DaveLiddament\PhpstanRuleTestHelper;
use DaveLiddament\PhpstanRuleTestHelper\Internal\ErrorMessageParseException;
abstract class ErrorMessageFormatter
{
/**
* This method returns the error message for an error in a fixture file.
*
* `$errorContext` is everything after `\\ERROR` in the fixture file
*
* @throws ErrorMessageParseException
*/
abstract public function getErrorMessage(string $errorContext): string;
/**
* Helper method for splitting the error context into parts.
*
* @param non-empty-string $separator
* @param int|null $expectedNumberOfParts if this is specified, then the number of parts of context must match this number
*
* @throws ErrorMessageParseException
*
* @return list<string>
*/
final protected function getErrorMessageAsParts(
string $errorContext,
?int $expectedNumberOfParts = null,
string $separator = '|',
): array {
$parts = explode($separator, $errorContext);
if ((null !== $expectedNumberOfParts) && (count($parts) !== $expectedNumberOfParts)) {
$message = sprintf(
'Expected %d parts in error message but got %d.',
$expectedNumberOfParts,
count($parts),
);
throw new ErrorMessageParseException($message);
}
return $parts;
}
}