Completed
Pull Request — master (#38)
by Alberto
01:55
created

StubHelper::isStaticName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 12
loc 12
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
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
    const PREFIXES = [
15
        'static' => '::',
16
        'property' => '\\$'
17
    ];
18
19
    /**
20
     * http://php.net/manual/en/language.variables.basics.php
21
     * http://php.net/manual/en/functions.user-defined.php
22
     */
23
    const REGEX_NAME = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/';
24
25
    /**
26
     * @param string $name
27
     * @return bool
28
     *
29
     * @throws InvalidArgumentException
30
     */
31 2 View Code Duplication
    public static function isStaticName(string $name): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33 2
        self::validateName(self::doStripName($name));
34
35 2
        return (bool)preg_match(
36 2
            sprintf(
37 2
                '/^%s/',
38 2
                static::PREFIXES['static']
39
            ),
40 2
            $name
41
        );
42
    }
43
44
    /**
45
     * @param string $name
46
     * @return void
47
     *
48
     * @throws InvalidArgumentException
49
     */
50 2
    public static function validateStaticName(string $name)
51
    {
52 2
        self::validateName($name, 'static');
53
    }
54
55
    /**
56
     * @param string $name
57
     * @return bool
58
     *
59
     * @throws InvalidArgumentException
60
     */
61 118 View Code Duplication
    public static function isPropertyName(string $name): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63 118
        self::validateName(self::doStripName($name));
64
65 117
        return (bool)preg_match(
66 117
            sprintf(
67 117
                '/^%s/',
68 117
                static::PREFIXES['property']
69
            ),
70 117
            self::doStripName($name, ['static'])
71
        );
72
    }
73
74
    /**
75
     * @param string $name
76
     * @return void
77
     *
78
     * @throws InvalidArgumentException
79
     */
80 114
    public static function validatePropertyName(string $name)
81
    {
82 114
        self::validateName($name, 'property');
83
    }
84
85
    /**
86
     * @param string $name
87
     * @return bool
88
     *
89
     * @throws InvalidArgumentException
90
     */
91 4
    public static function isMethodName(string $name): bool
92
    {
93 4
        return !static::isPropertyName($name);
94
    }
95
96
    /**
97
     * @param string $name
98
     * @return void
99
     *
100
     * @throws InvalidArgumentException
101
     */
102 114
    public static function validateMethodName(string $name)
103
    {
104 114
        self::validateName(self::doStripName($name, ['static']));
105
    }
106
107
    /**
108
     * @param string $name
109
     * @return string
110
     *
111
     * @throws InvalidArgumentException
112
     */
113 121
    public static function stripName(string $name): string
114
    {
115 121
        self::validateName(self::doStripName($name));
116
117 116
        return self::doStripName($name);
118
    }
119
120
    /**
121
     * @param string $name
122
     * @param array|null $prefixes
123
     * @return string
124
     */
125 131
    private static function doStripName(string $name, array $prefixes = null): string
126
    {
127 131
        $prefixes = null !== $prefixes
128 122
            ? array_intersect(array_keys(static::PREFIXES), $prefixes)
129 131
            : array_keys(static::PREFIXES);
130
131 131
        return array_reduce($prefixes, function (string $name, string $prefix) {
132 131
            return preg_replace(
133 131
                sprintf('/^%s/', static::PREFIXES[$prefix]),
134 131
                '',
135 131
                $name
136
            );
137 131
        }, $name);
138
    }
139
140
    /**
141
     * @param string $name
142
     * @param string|null $type
143
     * @return void
144
     *
145
     * @throws InvalidArgumentException
146
     */
147 131
    private static function validateName(string $name, string $type = null)
148
    {
149 131
        $methodName = sprintf('is%sName', $type);
150 131
        $nameIsValid = isset(static::PREFIXES[$type])
151 116
            ? static::$methodName($name)
152 131
            : preg_match(static::REGEX_NAME, $name);
153
154 131
        if (!$nameIsValid) {
155 12
            $message = isset(static::PREFIXES[$type])
156 3
                ? sprintf(
157 3
                    'Name must be prefixed by "%s", "%s" given',
158 3
                    stripcslashes(static::PREFIXES[$type]),
159 3
                    $name
160
                )
161 9
                : sprintf(
162 9
                    'Name must be a valid variable or function name, "%s" given',
163 12
                    $name
164
                );
165
166 12
            throw new InvalidArgumentException($message);
167
        }
168
    }
169
}
170