Completed
Pull Request — master (#44)
by Angelo
02:00
created

StubHelper::validateName()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 2
dl 0
loc 22
ccs 15
cts 15
cp 1
crap 4
rs 8.9197
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Stub;
5
6
use Moka\Exception\InvalidArgumentException;
7
8
/**
9
 * Class StubHelper
10
 * @package Moka\Stub
11
 */
12
class StubHelper
13
{
14
    private const PREFIXES = [
15
        'static' => '::',
16
        'property' => '\\$'
17
    ];
18
19
    private const NAME_TEMPLATE = '/^%s/';
20
21
    /**
22
     * http://php.net/manual/en/language.variables.basics.php
23
     * http://php.net/manual/en/functions.user-defined.php
24
     */
25
    private const REGEX_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
26
27
    /**
28
     * @param string $name
29
     * @return bool
30
     *
31
     * @throws InvalidArgumentException
32
     */
33 2
    public static function isStaticName(string $name): bool
34
    {
35 2
        return self::isName(
36 2
            $name,
37 2
            'static',
38 2
            $name
39
        );
40
    }
41
42
    /**
43
     * @param string $name
44
     * @return void
45
     *
46
     * @throws InvalidArgumentException
47
     */
48 2
    public static function validateStaticName(string $name): void
49
    {
50 2
        self::validateName($name, 'static');
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return bool
56
     *
57
     * @throws InvalidArgumentException
58
     */
59 91
    public static function isPropertyName(string $name): bool
60
    {
61 91
        return self::isName(
62 91
            $name,
63 91
            'property',
64 91
            self::doStripName($name, ['static'])
65
        );
66
    }
67
68
    /**
69
     * @param string $name
70
     * @param string $memberType
71
     * @param string $subject
72
     * @return bool
73
     * @throws InvalidArgumentException
74
     */
75 93
    private static function isName(string $name, string $memberType, string $subject): bool
76
    {
77 93
        self::validateName(self::doStripName($name));
78
79 92
        return (bool)preg_match(
80 92
            sprintf(
81 92
                self::NAME_TEMPLATE,
82 92
                static::PREFIXES[$memberType]
83
            ),
84 92
            $subject
85
        );
86
    }
87
88
    /**
89
     * @param string $name
90
     * @return void
91
     *
92
     * @throws InvalidArgumentException
93
     */
94 87
    public static function validatePropertyName(string $name): void
95
    {
96 87
        self::validateName($name, 'property');
97
    }
98
99
    /**
100
     * @param string $name
101
     * @return bool
102
     *
103
     * @throws InvalidArgumentException
104
     */
105 4
    public static function isMethodName(string $name): bool
106
    {
107 4
        return !static::isPropertyName($name);
108
    }
109
110
    /**
111
     * @param string $name
112
     * @return void
113
     *
114
     * @throws InvalidArgumentException
115
     */
116 87
    public static function validateMethodName(string $name): void
117
    {
118 87
        self::validateName(self::doStripName($name, ['static']));
119
    }
120
121
    /**
122
     * @param string $name
123
     * @return string
124
     *
125
     * @throws InvalidArgumentException
126
     */
127 94
    public static function stripName(string $name): string
128
    {
129 94
        self::validateName(self::doStripName($name));
130
131 89
        return self::doStripName($name);
132
    }
133
134
    /**
135
     * @param string $name
136
     * @param array|null $prefixes
137
     * @return string
138
     */
139 104
    private static function doStripName(string $name, array $prefixes = null): string
140
    {
141 104
        $prefixes = null !== $prefixes
142 96
            ? array_intersect(array_keys(static::PREFIXES), $prefixes)
143 104
            : array_keys(static::PREFIXES);
144
145 104
        return array_reduce($prefixes, function (string $name, string $prefix) {
146 104
            return preg_replace(
147 104
                sprintf('/^%s/', static::PREFIXES[$prefix]),
148 104
                '',
149 104
                $name
150
            );
151 104
        }, $name);
152
    }
153
154
    /**
155
     * @param string $name
156
     * @param string|null $type
157
     * @return void
158
     *
159
     * @throws InvalidArgumentException
160
     */
161 104
    private static function validateName(string $name, string $type = null): void
162
    {
163 104
        $methodName = sprintf('is%sName', $type);
164 104
        $nameIsValid = isset(static::PREFIXES[$type])
165 89
            ? static::$methodName($name)
166 104
            : preg_match(static::REGEX_NAME, $name);
167
168 104
        if (!$nameIsValid) {
169 12
            $message = isset(static::PREFIXES[$type])
170 3
                ? sprintf(
171 3
                    'Name must be prefixed by "%s", "%s" given',
172 3
                    stripcslashes(static::PREFIXES[$type]),
173 3
                    $name
174
                )
175 9
                : sprintf(
176 9
                    'Name must be a valid variable or function name, "%s" given',
177 12
                    $name
178
                );
179
180 12
            throw new InvalidArgumentException($message);
181
        }
182
    }
183
}
184