1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Abstract builder package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\AbstractBuilder\Utils; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\AbstractBuilder\AbstractBuilder; |
13
|
|
|
use RunOpenCode\AbstractBuilder\Ast\Metadata\ClassMetadata; |
14
|
|
|
use RunOpenCode\AbstractBuilder\ReflectiveAbstractBuilder; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ClassUtils |
18
|
|
|
* |
19
|
|
|
* @package RunOpenCode\AbstractBuilder\Utils |
20
|
|
|
*/ |
21
|
|
|
final class ClassUtils |
22
|
|
|
{ |
23
|
|
|
private function __construct() { /* noop */ } |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Check if class name is valid. |
27
|
|
|
* |
28
|
|
|
* @param string $fqcn |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
1 |
|
public static function isClassNameValid($fqcn) |
33
|
|
|
{ |
34
|
1 |
|
foreach (explode('\\', ltrim($fqcn, '\\')) as $part) { |
35
|
|
|
|
36
|
1 |
|
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $part)) { |
37
|
1 |
|
return false; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Check if class can have its builder pattern class implemented. |
46
|
|
|
* |
47
|
|
|
* @param ClassMetadata $class |
48
|
|
|
* |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public static function isBuildable(ClassMetadata $class) |
52
|
|
|
{ |
53
|
|
|
if (!$class->hasPublicMethod('__construct')) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (0 === count($class->getPublicMethod('__construct')->getParameters())) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if class is implemented builder class. |
66
|
|
|
* |
67
|
|
|
* @param ClassMetadata $class |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public static function isBuilder(ClassMetadata $class) |
72
|
|
|
{ |
73
|
|
|
if (null === $class->getParent()) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ( |
78
|
|
|
null !== $class->getParent() |
79
|
|
|
&& |
80
|
|
|
( |
81
|
|
|
ReflectiveAbstractBuilder::class === $class->getParent()->getName() |
82
|
|
|
|| |
83
|
|
|
AbstractBuilder::class === $class->getParent()->getName() |
84
|
|
|
) |
85
|
|
|
) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return self::isBuilder($class->getParent()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get namespace of class. |
94
|
|
|
* |
95
|
|
|
* @param $class |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public static function getNamespace($class) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if ($class instanceof ClassMetadata) { |
102
|
|
|
$class = $class->getName(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$parts = explode('\\', $class); |
106
|
|
|
array_pop($parts); |
107
|
|
|
|
108
|
|
|
return implode('\\', $parts); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get short name of class. |
113
|
|
|
* |
114
|
|
|
* @param $class |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public static function getShortName($class) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if ($class instanceof ClassMetadata) { |
121
|
|
|
return $class->getShortName(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$parts = explode('\\', $class); |
125
|
|
|
|
126
|
|
|
return array_pop($parts); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.