Conditions | 4 |
Paths | 6 |
Total Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 4 |
Changes | 0 |
1 | <?php |
||
15 | function validateName(string $name, string $type = null): void |
||
16 | { |
||
17 | 105 | $functionName = sprintf( |
|
18 | 105 | '%s\is%sName', |
|
19 | 105 | __NAMESPACE__, |
|
20 | 105 | ucfirst((string)$type) |
|
21 | ); |
||
22 | |||
23 | 105 | $isAPrefix = array_key_exists($type, PREFIXES); |
|
24 | 105 | $nameIsValid = $isAPrefix |
|
25 | 90 | ? $functionName($name) |
|
26 | 105 | : preg_match(REGEX_NAME, $name); |
|
27 | |||
28 | 105 | if (!$nameIsValid) { |
|
29 | 12 | $message = $isAPrefix |
|
30 | 3 | ? sprintf( |
|
31 | 3 | 'Name must be prefixed by "%s", "%s" given', |
|
32 | 3 | stripcslashes(PREFIXES[$type]), |
|
33 | $name |
||
34 | ) |
||
35 | 9 | : sprintf( |
|
36 | 12 | 'Name must be a valid variable or function name, "%s" given', |
|
37 | $name |
||
38 | ); |
||
39 | |||
40 | 12 | throw new InvalidArgumentException($message); |
|
41 | } |
||
42 | } |
||
43 |