validateName.php ➔ validateName()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 2
dl 0
loc 28
ccs 16
cts 16
cp 1
crap 4
rs 9.472
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Stub\Helper;
5
6
use Moka\Exception\InvalidArgumentException;
7
8
/**
9
 * @param string $name
10
 * @param string|null $type
11
 * @return void
12
 *
13
 * @throws InvalidArgumentException
14
 */
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