-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
RectorAssert.php
106 lines (88 loc) · 3.01 KB
/
RectorAssert.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Rector\Core\Validation;
use Rector\Core\Util\StringUtils;
use Webmozart\Assert\InvalidArgumentException;
/**
* @see \Rector\Core\Tests\Validation\RectorAssertTest
*/
final class RectorAssert
{
/**
* @see https://stackoverflow.com/a/12011255/1348344
* @see https://regex101.com/r/PYQaPF/1
* @var string
*/
private const CLASS_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*$#';
/**
* @var string
*/
private const NAMESPACE_REGEX = '#^' . self::NAKED_NAMESPACE_REGEX . '$#';
/**
* @see https://stackoverflow.com/a/60470526/1348344
* @see https://regex101.com/r/37aUWA/1
*
* @var string
*/
private const NAKED_NAMESPACE_REGEX = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\\\\]*[a-zA-Z0-9_\x7f-\xff]';
/**
* @see https://www.php.net/manual/en/language.variables.basics.php
* @see https://regex101.com/r/hFw17T/1
*
* @var string
*/
private const PROPERTY_NAME_REGEX = '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#';
/**
* @see https://regex101.com/r/uh5B0S/1
* @see https://www.php.net/manual/en/functions.user-defined.php
*
* @var string
*/
private const METHOD_NAME_REGEX = '#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#';
/**
* @see https://regex101.com/r/uh5B0S/1
* @see https://www.php.net/manual/en/functions.user-defined.php
*
* @var string
*/
private const FUNCTION_NAME_REGEX = '#^(' . self::NAKED_NAMESPACE_REGEX . '\\\\)?([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)$#';
/**
* @see https://www.php.net/manual/en/language.constants.php
* @see https://regex101.com/r/Fu6WHQ/1
*
* @var string
*/
private const CONSTANT_REGEX = '#^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$#';
public static function namespaceName(string $name): void
{
self::elementName($name, self::NAMESPACE_REGEX, 'namespace');
}
public static function constantName(string $name): void
{
self::elementName($name, self::CONSTANT_REGEX, 'constant');
}
public static function className(string $name): void
{
self::elementName($name, self::CLASS_NAME_REGEX, 'class');
}
public static function propertyName(string $name): void
{
self::elementName($name, self::PROPERTY_NAME_REGEX, 'property');
}
public static function methodName(string $name): void
{
self::elementName($name, self::METHOD_NAME_REGEX, 'method');
}
public static function functionName(string $name): void
{
self::elementName($name, self::FUNCTION_NAME_REGEX, 'function');
}
public static function elementName(string $name, string $regex, string $elementType): void
{
if (StringUtils::isMatch($name, $regex)) {
return;
}
$errorMessage = sprintf('"%s" is not a valid %s name', $name, $elementType);
throw new InvalidArgumentException($errorMessage);
}
}