1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpParser; |
4
|
|
|
|
5
|
|
|
use PhpParser\Builder; |
6
|
|
|
use PhpParser\Node\Stmt\Use_; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The following methods use reserved keywords, so their implementation is defined with an underscore and made available |
10
|
|
|
* with the reserved name through __call() magic. |
11
|
|
|
* |
12
|
|
|
* @method Builder\Namespace_ namespace(string $name) Creates a namespace builder. |
13
|
|
|
* @method Builder\Class_ class(string $name) Creates a class builder. |
14
|
|
|
* @method Builder\Interface_ interface(string $name) Creates an interface builder. |
15
|
|
|
* @method Builder\Trait_ trait(string $name) Creates a trait builder. |
16
|
|
|
* @method Builder\Function_ function(string $name) Creates a function builder. |
17
|
|
|
* @method Builder\Use_ use(string $name) Creates a namespace/class use builder. |
18
|
|
|
*/ |
19
|
|
|
class BuilderFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Creates a namespace builder. |
23
|
|
|
* |
24
|
|
|
* @param null|string|Node\Name $name Name of the namespace |
25
|
|
|
* |
26
|
|
|
* @return Builder\Namespace_ The created namespace builder |
27
|
|
|
*/ |
28
|
|
|
protected function _namespace($name) { |
29
|
|
|
return new Builder\Namespace_($name); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Creates a class builder. |
34
|
|
|
* |
35
|
|
|
* @param string $name Name of the class |
36
|
|
|
* |
37
|
|
|
* @return Builder\Class_ The created class builder |
38
|
|
|
*/ |
39
|
|
|
protected function _class($name) { |
40
|
|
|
return new Builder\Class_($name); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates an interface builder. |
45
|
|
|
* |
46
|
|
|
* @param string $name Name of the interface |
47
|
|
|
* |
48
|
|
|
* @return Builder\Interface_ The created interface builder |
49
|
|
|
*/ |
50
|
|
|
protected function _interface($name) { |
51
|
|
|
return new Builder\Interface_($name); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a trait builder. |
56
|
|
|
* |
57
|
|
|
* @param string $name Name of the trait |
58
|
|
|
* |
59
|
|
|
* @return Builder\Trait_ The created trait builder |
60
|
|
|
*/ |
61
|
|
|
protected function _trait($name) { |
62
|
|
|
return new Builder\Trait_($name); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Creates a method builder. |
67
|
|
|
* |
68
|
|
|
* @param string $name Name of the method |
69
|
|
|
* |
70
|
|
|
* @return Builder\Method The created method builder |
71
|
|
|
*/ |
72
|
|
|
public function method($name) { |
73
|
|
|
return new Builder\Method($name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a parameter builder. |
78
|
|
|
* |
79
|
|
|
* @param string $name Name of the parameter |
80
|
|
|
* |
81
|
|
|
* @return Builder\Param The created parameter builder |
82
|
|
|
*/ |
83
|
|
|
public function param($name) { |
84
|
|
|
return new Builder\Param($name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Creates a property builder. |
89
|
|
|
* |
90
|
|
|
* @param string $name Name of the property |
91
|
|
|
* |
92
|
|
|
* @return Builder\Property The created property builder |
93
|
|
|
*/ |
94
|
|
|
public function property($name) { |
95
|
|
|
return new Builder\Property($name); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Creates a function builder. |
100
|
|
|
* |
101
|
|
|
* @param string $name Name of the function |
102
|
|
|
* |
103
|
|
|
* @return Builder\Function_ The created function builder |
104
|
|
|
*/ |
105
|
|
|
protected function _function($name) { |
106
|
|
|
return new Builder\Function_($name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates a namespace/class use builder. |
111
|
|
|
* |
112
|
|
|
* @param string|Node\Name Name to alias |
113
|
|
|
* |
114
|
|
|
* @return Builder\Use_ The create use builder |
115
|
|
|
*/ |
116
|
|
|
protected function _use($name) { |
117
|
|
|
return new Builder\Use_($name, Use_::TYPE_NORMAL); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function __call($name, array $args) { |
|
|
|
|
121
|
|
|
if (method_exists($this, '_' . $name)) { |
122
|
|
|
return call_user_func_array(array($this, '_' . $name), $args); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
throw new \LogicException(sprintf('Method "%s" does not exist', $name)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.