Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ParameterGenerator.php
001 <?php
002 /**
003 * Zend Framework (http://framework.zend.com/)
004 *
005 * @link http://github.com/zendframework/zf2 for the canonical source repository
006 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
007 * @license http://framework.zend.com/license/new-bsd New BSD License
008 */
009
010 namespace Zend\Code\Generator;
011
012 use ReflectionParameter;
013 use Zend\Code\Reflection\ParameterReflection;
014
015 use function is_string;
016 use function method_exists;
017 use function str_replace;
018 use function strtolower;
019
020 class ParameterGenerator extends AbstractGenerator
021 {
022 /**
023 * @var string
024 */
025 protected $name;
026
027 /**
028 * @var TypeGenerator|null
029 */
030 protected $type;
031
032 /**
033 * @var ValueGenerator
034 */
035 protected $defaultValue;
036
037 /**
038 * @var int
039 */
040 protected $position;
041
042 /**
043 * @var bool
044 */
045 protected $passedByReference = false;
046
047 /**
048 * @var bool
049 */
050 private $variadic = false;
051
052 /**
053 * @var bool
054 */
055 private $omitDefaultValue = false;
056
057 /**
058 * @param ParameterReflection $reflectionParameter
059 * @return ParameterGenerator
060 */
061 public static function fromReflection(ParameterReflection $reflectionParameter)
062 {
063 $param = new ParameterGenerator();
064
065 $param->setName($reflectionParameter->getName());
066
067 if ($type = self::extractFQCNTypeFromReflectionType($reflectionParameter)) {
068 $param->setType($type);
069 }
070
071 $param->setPosition($reflectionParameter->getPosition());
072
073 $variadic = method_exists($reflectionParameter, 'isVariadic') && $reflectionParameter->isVariadic();
074
075 $param->setVariadic($variadic);
076
077 if (! $variadic && ($reflectionParameter->isOptional() || $reflectionParameter->isDefaultValueAvailable())) {
078 try {
079 $param->setDefaultValue($reflectionParameter->getDefaultValue());
080 } catch (\ReflectionException $e) {
081 $param->setDefaultValue(null);
082 }
083 }
084
085 $param->setPassedByReference($reflectionParameter->isPassedByReference());
086
087 return $param;
088 }
089
090 /**
091 * Generate from array
092 *
093 * @configkey name string [required] Class Name
094 * @configkey type string
095 * @configkey defaultvalue null|bool|string|int|float|array|ValueGenerator
096 * @configkey passedbyreference bool
097 * @configkey position int
098 * @configkey sourcedirty bool
099 * @configkey indentation string
100 * @configkey sourcecontent string
101 * @configkey omitdefaultvalue bool
102 *
103 * @throws Exception\InvalidArgumentException
104 * @param array $array
105 * @return ParameterGenerator
106 */
107 public static function fromArray(array $array)
108 {
109 if (! isset($array['name'])) {
110 throw new Exception\InvalidArgumentException(
111 'Parameter generator requires that a name is provided for this object'
112 );
113 }
114
115 $param = new static($array['name']);
116 foreach ($array as $name => $value) {
117 // normalize key
118 switch (strtolower(str_replace(['.', '-', '_'], '', $name))) {
119 case 'type':
120 $param->setType($value);
121 break;
122 case 'defaultvalue':
123 $param->setDefaultValue($value);
124 break;
125 case 'passedbyreference':
126 $param->setPassedByReference($value);
127 break;
128 case 'position':
129 $param->setPosition($value);
130 break;
131 case 'sourcedirty':
132 $param->setSourceDirty($value);
133 break;
134 case 'indentation':
135 $param->setIndentation($value);
136 break;
137 case 'sourcecontent':
138 $param->setSourceContent($value);
139 break;
140 case 'omitdefaultvalue':
141 $param->omitDefaultValue($value);
142 break;
143 }
144 }
145
146 return $param;
147 }
148
149 /**
150 * @param string $name
151 * @param string $type
152 * @param mixed $defaultValue
153 * @param int $position
154 * @param bool $passByReference
155 */
156 public function __construct(
157 $name = null,
158 $type = null,
159 $defaultValue = null,
160 $position = null,
161 $passByReference = false
162 ) {
163 if (null !== $name) {
164 $this->setName($name);
165 }
166 if (null !== $type) {
167 $this->setType($type);
168 }
169 if (null !== $defaultValue) {
170 $this->setDefaultValue($defaultValue);
171 }
172 if (null !== $position) {
173 $this->setPosition($position);
174 }
175 if (false !== $passByReference) {
176 $this->setPassedByReference(true);
177 }
178 }
179
180 /**
181 * @param string $type
182 * @return ParameterGenerator
183 */
184 public function setType($type)
185 {
186 $this->type = TypeGenerator::fromTypeString($type);
187
188 return $this;
189 }
190
191 /**
192 * @return string
193 */
194 public function getType()
195 {
196 return $this->type
197 ? (string) $this->type
198 : null;
199 }
200
201 /**
202 * @param string $name
203 * @return ParameterGenerator
204 */
205 public function setName($name)
206 {
207 $this->name = (string) $name;
208 return $this;
209 }
210
211 /**
212 * @return string
213 */
214 public function getName()
215 {
216 return $this->name;
217 }
218
219 /**
220 * Set the default value of the parameter.
221 *
222 * Certain variables are difficult to express
223 *
224 * @param null|bool|string|int|float|array|ValueGenerator $defaultValue
225 * @return ParameterGenerator
226 */
227 public function setDefaultValue($defaultValue)
228 {
229 if (! $defaultValue instanceof ValueGenerator) {
230 $defaultValue = new ValueGenerator($defaultValue);
231 }
232 $this->defaultValue = $defaultValue;
233
234 return $this;
235 }
236
237 /**
238 * @return ValueGenerator
239 */
240 public function getDefaultValue()
241 {
242 return $this->defaultValue;
243 }
244
245 /**
246 * @param int $position
247 * @return ParameterGenerator
248 */
249 public function setPosition($position)
250 {
251 $this->position = (int) $position;
252 return $this;
253 }
254
255 /**
256 * @return int
257 */
258 public function getPosition()
259 {
260 return $this->position;
261 }
262
263 /**
264 * @return bool
265 */
266 public function getPassedByReference()
267 {
268 return $this->passedByReference;
269 }
270
271 /**
272 * @param bool $passedByReference
273 * @return ParameterGenerator
274 */
275 public function setPassedByReference($passedByReference)
276 {
277 $this->passedByReference = (bool) $passedByReference;
278 return $this;
279 }
280
281 /**
282 * @param bool $variadic
283 *
284 * @return ParameterGenerator
285 */
286 public function setVariadic($variadic)
287 {
288 $this->variadic = (bool) $variadic;
289
290 return $this;
291 }
292
293 /**
294 * @return bool
295 */
296 public function getVariadic()
297 {
298 return $this->variadic;
299 }
300
301 /**
302 * @return string
303 */
304 public function generate()
305 {
306 $output = $this->generateTypeHint();
307
308 if (true === $this->passedByReference) {
309 $output .= '&';
310 }
311
312 if ($this->variadic) {
313 $output .= '... ';
314 }
315
316 $output .= '$' . $this->name;
317
318 if ($this->omitDefaultValue) {
319 return $output;
320 }
321
322 if ($this->defaultValue instanceof ValueGenerator) {
323 $output .= ' = ';
324 $this->defaultValue->setOutputMode(ValueGenerator::OUTPUT_SINGLE_LINE);
325 $output .= $this->defaultValue;
326 }
327
328 return $output;
329 }
330
331 /**
332 * @param ParameterReflection $reflectionParameter
333 *
334 * @return null|string
335 */
336 private static function extractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter)
337 {
338 if (! method_exists($reflectionParameter, 'getType')) {
339 return self::prePhp7ExtractFQCNTypeFromReflectionType($reflectionParameter);
340 }
341
342 $type = method_exists($reflectionParameter, 'getType')
343 ? $reflectionParameter->getType()
344 : null;
345
346 if (! $type) {
347 return null;
348 }
349
350 if (! method_exists($type, 'getName')) {
351 return self::expandLiteralParameterType((string) $type, $reflectionParameter);
352 }
353
354 return ($type->allowsNull() ? '?' : '')
355 . self::expandLiteralParameterType($type->getName(), $reflectionParameter);
356 }
357
358 /**
359 * For ancient PHP versions (yes, you should upgrade to 7.0):
360 *
361 * @param ParameterReflection $reflectionParameter
362 *
363 * @return string|null
364 */
365 private static function prePhp7ExtractFQCNTypeFromReflectionType(ParameterReflection $reflectionParameter)
366 {
367 if ($reflectionParameter->isCallable()) {
368 return 'callable';
369 }
370
371 if ($reflectionParameter->isArray()) {
372 return 'array';
373 }
374
375 if ($class = $reflectionParameter->getClass()) {
376 return $class->getName();
377 }
378
379 return null;
380 }
381
382 /**
383 * @param string $literalParameterType
384 * @param ReflectionParameter $reflectionParameter
385 *
386 * @return string
387 */
388 private static function expandLiteralParameterType($literalParameterType, ReflectionParameter $reflectionParameter)
389 {
390 if ('self' === strtolower($literalParameterType)) {
391 return $reflectionParameter->getDeclaringClass()->getName();
392 }
393
394 if ('parent' === strtolower($literalParameterType)) {
395 return $reflectionParameter->getDeclaringClass()->getParentClass()->getName();
396 }
397
398 return $literalParameterType;
399 }
400
401 /**
402 * @return string
403 */
404 private function generateTypeHint()
405 {
406 if (null === $this->type) {
407 return '';
408 }
409
410 return $this->type->generate() . ' ';
411 }
412
413 /**
414 * @param bool $omit
415 * @return ParameterGenerator
416 */
417 public function omitDefaultValue(bool $omit = true)
418 {
419 $this->omitDefaultValue = $omit;
420
421 return $this;
422 }
423 }
424