1 | <?php |
||
19 | trait ParametersPart { |
||
20 | |||
21 | /** @var PhpParameter[] */ |
||
22 | private $parameters = []; |
||
23 | |||
24 | 44 | private function initParameters() { |
|
25 | // $this->parameters = new ArrayList(); |
||
26 | 44 | } |
|
27 | |||
28 | /** |
||
29 | * Sets a collection of parameters |
||
30 | * |
||
31 | * Note: clears all parameters before setting the new ones |
||
32 | * |
||
33 | * @param PhpParameter[] $parameters |
||
34 | * @return $this |
||
35 | */ |
||
36 | 1 | public function setParameters(array $parameters) { |
|
37 | 1 | $this->parameters = []; |
|
38 | 1 | foreach ($parameters as $parameter) { |
|
39 | 1 | $this->addParameter($parameter); |
|
40 | } |
||
41 | |||
42 | 1 | return $this; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Adds a parameter |
||
47 | * |
||
48 | * @param PhpParameter $parameter |
||
49 | * @return $this |
||
50 | */ |
||
51 | 20 | public function addParameter(PhpParameter $parameter) { |
|
52 | 20 | $this->parameters[] = $parameter; |
|
53 | |||
54 | 20 | return $this; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Checks whether a parameter exists |
||
59 | * |
||
60 | * @param string $name parameter name |
||
61 | * @return bool `true` if a parameter exists and `false` if not |
||
62 | */ |
||
63 | 2 | public function hasParameter(string $name): bool { |
|
64 | 2 | foreach ($this->parameters as $param) { |
|
65 | 2 | if ($param->getName() == $name) { |
|
66 | 2 | return true; |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 2 | return false; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * A quick way to add a parameter which is created from the given parameters |
||
75 | * |
||
76 | * @param string $name |
||
77 | * @param null|Map|string[]|PhpTypeInterface[] $types |
||
78 | * @param mixed $defaultValue omit the argument to define no default value |
||
79 | * |
||
80 | * @return $this |
||
81 | */ |
||
82 | 1 | public function addSimpleParameter(string $name, $types = null, $defaultValue = null) { |
|
83 | 1 | $parameter = new PhpParameter($name); |
|
84 | 1 | $parameter->setTypes($types); |
|
85 | |||
86 | 1 | if (2 < func_num_args()) { |
|
87 | 1 | $parameter->setValue($defaultValue); |
|
88 | } |
||
89 | |||
90 | 1 | $this->addParameter($parameter); |
|
91 | 1 | return $this; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * A quick way to add a parameter with description which is created from the given parameters |
||
96 | * |
||
97 | * @param string $name |
||
98 | * @param string[]|PhpTypeInterface[] $types |
||
99 | * @param null|string $typeDescription |
||
100 | * @param mixed $defaultValue omit the argument to define no default value |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | 1 | public function addSimpleDescParameter(string $name, $types = null, string $typeDescription = null, $defaultValue = null) { |
|
117 | |||
118 | /** |
||
119 | * Returns a parameter by index or name |
||
120 | * |
||
121 | * @param string|int $nameOrIndex |
||
122 | * @throws \InvalidArgumentException |
||
123 | * @return PhpParameter |
||
124 | */ |
||
125 | 9 | public function getParameter($nameOrIndex): PhpParameter { |
|
139 | |||
140 | /** |
||
141 | * Replaces a parameter at a given position |
||
142 | * |
||
143 | * @param int $position |
||
144 | * @param PhpParameter $parameter |
||
145 | * @throws \InvalidArgumentException |
||
146 | * @return $this |
||
147 | */ |
||
148 | 2 | public function replaceParameter(int $position, PhpParameter $parameter) { |
|
154 | |||
155 | /** |
||
156 | * Remove a parameter at a given position |
||
157 | * |
||
158 | * @param int|string|PhpParameter $param |
||
159 | * @return $this |
||
160 | */ |
||
161 | 2 | public function removeParameter($param) { |
|
172 | |||
173 | 2 | private function removeParameterByPosition($position) { |
|
178 | |||
179 | 1 | private function removeParameterByName($name) { |
|
191 | |||
192 | 4 | private function checkPosition($position) { |
|
197 | |||
198 | /** |
||
199 | * Returns a collection of parameters |
||
200 | * |
||
201 | * @return PhpParameter[] |
||
202 | */ |
||
203 | 30 | public function getParameters() { |
|
206 | |||
207 | /** |
||
208 | * Returns the docblock |
||
209 | * |
||
210 | * @return Docblock |
||
211 | */ |
||
212 | abstract protected function getDocblock(): Docblock; |
||
213 | |||
214 | /** |
||
215 | * Generates docblock for params |
||
216 | */ |
||
217 | 12 | protected function generateParamDocblock(array $noTypeHint = []) { |
|
242 | |||
243 | } |
||
244 |