Completed
Push — feature/issue-48 ( c28322...8aa0d0 )
by Mikaël
30:45
created

Struct::addStructPropertiesToAnnotationBlockUses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
6
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
7
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
8
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Property as PropertyContainer;
10
use WsdlToPhp\PackageGenerator\Container\PhpElement\Constant as ConstantContainer;
11
use WsdlToPhp\PackageGenerator\Container\Model\StructAttribute as StructAttributeContainer;
12
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
13
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
14
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
15
use WsdlToPhp\PhpGenerator\Element\PhpProperty;
16
use WsdlToPhp\PhpGenerator\Element\PhpConstant;
17
use WsdlToPhp\PhpGenerator\Element\PhpFunctionParameter;
18
19
class Struct extends AbstractModelFile
20
{
21
    /**
22
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
23
     */
24 88
    protected function getClassConstants(ConstantContainer $constants)
25
    {
26 88
    }
27
    /**
28
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
29
     */
30
    protected function getConstantAnnotationBlock(PhpConstant $constant)
31
    {
32
    }
33
    /**
34
     * @param bool $includeInheritanceAttributes include the attributes of parent class, default parent attributes are not included. If true, then the array is an associative array containing and index "attribute" for the StructAttribute object and an index "model" for the Struct object.
35
     * @param bool $requiredFirst places the required attributes first, then the not required in order to have the _contrust method with the required attribute at first
36
     * @return StructAttributeContainer
37
     */
38 88
    protected function getModelAttributes($includeInheritanceAttributes = false, $requiredFirst = true)
39
    {
40 88
        return $this->getModel()->getAttributes($includeInheritanceAttributes, $requiredFirst);
41
    }
42
    /**
43
     * @param PropertyContainer
44
     */
45 116
    protected function getClassProperties(PropertyContainer $properties)
46
    {
47 116
        if ($this->getModel()->getAttributes()->count() > 0) {
48 88
            foreach ($this->getModelAttributes() as $attribute) {
49 88
                $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
50 66
            }
51 66
        }
52 116
    }
53
    /**
54
     * @return PhpAnnotationBlock
55
     */
56 88
    protected function getPropertyAnnotationBlock(PhpProperty $property)
57
    {
58 88
        $annotationBlock = new PhpAnnotationBlock();
59 88
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
60 88
        if (($attribute = $this->getModel()->getAttribute($property->getName())) instanceof StructAttributeModel) {
61 88
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
62 88
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
63 66
        }
64 88
        return $annotationBlock;
65
    }
66
    /**
67
     * @param MethodContainer
68
     */
69 88
    protected function getClassMethods(MethodContainer $methods)
70
    {
71 66
        $this
72 88
            ->addStructMethodConstruct($methods)
73 88
            ->addStructMethodsSetAndGet($methods)
74 88
            ->addStructMethodSetState($methods);
75 88
    }
76
    /**
77
     * @param MethodContainer $methods
78
     * @return Struct
79
     */
80 88
    protected function addStructMethodConstruct(MethodContainer $methods)
81
    {
82 88
        $method = new PhpMethod(self::METHOD_CONSTRUCT, $this->getStructMethodParametersValues());
83 88
        $this->addStructMethodConstructBody($method);
84 88
        $methods->add($method);
85 88
        return $this;
86
    }
87
    /**
88
     * @param PhpMethod $method
89
     * @return Struct
90
     */
91 88
    protected function addStructMethodConstructBody(PhpMethod $method)
92
    {
93 88
        $count = $this->getModelAttributes()->count();
94 88
        foreach ($this->getModelAttributes() as $index=>$attribute) {
95 88
            if ($index === 0) {
96 88
                $method->addChild('$this');
97 66
            }
98 88
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
99 66
        }
100 88
        return $this;
101
    }
102
    /**
103
     * @param PhpMethod $method
104
     * @param StructAttributeModel $attribute
105
     * @param bool $isLast
106
     * @return Struct
107
     */
108 88
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
109
    {
110 88
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($attribute->getCleanName()), $isLast ? ';' : ''), 1));
111 88
        return $this;
112
    }
113
    /**
114
     * @return PhpFunctionParameter[]
115
     */
116 88
    protected function getStructMethodParametersValues()
117
    {
118 88
        $parametersValues = array();
119 88
        foreach ($this->getModelAttributes() as $attribute) {
120 88
            $parametersValues[] = $this->getStructMethodParameter($attribute, true);
121 66
        }
122 88
        return $parametersValues;
123
    }
124
    /**
125
     * @param StructAttributeModel $attribute
126
     * @param bool $lowCaseFirstLetter
127
     * @param mixed $defaultValue
128
     * @return PhpFunctionParameter
129
     */
130 88
    protected function getStructMethodParameter(StructAttributeModel $attribute, $lowCaseFirstLetter = false, $defaultValue = null)
131
    {
132
        try {
133 88
            return new PhpFunctionParameter($lowCaseFirstLetter ? lcfirst($attribute->getCleanName()) : $attribute->getCleanName(), isset($defaultValue) ? $defaultValue : $attribute->getDefaultValue(), $this->getStructMethodParameterType($attribute));
134
        } catch (\InvalidArgumentException $exception) {
135
            throw new \InvalidArgumentException(sprintf('Unable to create function parameter for struct "%s" with type "%s" for attribute "%s"', $this->getModel()->getName(), var_export($this->getStructMethodParameterType($attribute), true), $attribute->getName()), __LINE__, $exception);
136
        }
137
    }
138
    /**
139
     * @param StructAttributeModel $attribute
140
     * @param bool $returnArrayType
141
     * @return string|null
142
     */
143 88
    protected function getStructMethodParameterType(StructAttributeModel $attribute, $returnArrayType = true)
144
    {
145 88
        return self::getValidType($this->getStructAttributeTypeHint($attribute, $returnArrayType), null);
146
    }
147
    /**
148
     * @param MethodContainer $methods
149
     * @return Struct
150
     */
151 88
    protected function addStructMethodsSetAndGet(MethodContainer $methods)
152
    {
153 88
        foreach ($this->getModelAttributes() as $attribute) {
154 66
            $this
155 88
                ->addStructMethodGet($methods, $attribute)
156 88
                ->addStructMethodSet($methods, $attribute)
157 88
                ->addStructMethodAddTo($methods, $attribute);
158 66
        }
159 88
        return $this;
160
    }
161
    /**
162
     * @param MethodContainer $methods
163
     * @param StructAttributeModel $attribute
164
     * @return Struct
165
     */
166 88
    protected function addStructMethodAddTo(MethodContainer $methods, StructAttributeModel $attribute)
167
    {
168 88
        if ($attribute->isArray()) {
169 44
            $method = new PhpMethod(sprintf('addTo%s', ucfirst($attribute->getCleanName())), array(
170 44
                new PhpFunctionParameter('item', PhpFunctionParameter::NO_VALUE, $this->getStructMethodParameterType($attribute, false)),
171 33
            ));
172 44
            $this->addStructMethodAddToBody($method, $attribute);
173 44
            $methods->add($method);
174 33
        }
175 88
        return $this;
176
    }
177
    /**
178
     * @param PhpMethod $method
179
     * @param StructAttributeModel $attribute
180
     * @return Struct
181
     */
182 44
    protected function addStructMethodAddToBody(PhpMethod $method, StructAttributeModel $attribute)
183
    {
184 44
        if ($this->getRestrictionFromStructAttribute($attribute) instanceof StructModel) {
185 16
            $this->addStructMethodSetBodyForRestriction($method, $attribute, 'item');
186 12
        } else {
187 40
            $this->addStructMethodSetBodyForAnyType($method, $attribute);
188
        }
189
        $method
190 44
            ->addChild(sprintf('$this->%s[] = $item;', $attribute->getCleanName()))
191 44
            ->addChild('return $this;');
192 44
        return $this;
193
    }
194
    /**
195
     * @param PhpMethod $method
196
     * @param StructAttributeModel $attribute
197
     * @return Struct
198
     */
199 40
    protected function addStructMethodSetBodyForAnyType(PhpMethod $method, StructAttributeModel $attribute)
200
    {
201
        $method
202 40
            ->addChild(sprintf('if (!%s) {', $this->getStructMethodSetBodyForArrayItemSanityCheck($attribute, 'item')))
203 40
                ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The %s property can only contain items of %s, "%%s" given\', is_object($item) ? get_class($item) : gettype($item)), __LINE__);', $attribute->getCleanName(), $this->getStructAttributeType($attribute, true)), 1))
204 40
            ->addChild('}');
205 40
        return $this;
206
    }
207
    /**
208
     * @param MethodContainer $methods
209
     * @param StructAttributeModel $attribute
210
     * @return Struct
211
     */
212 88
    protected function addStructMethodSet(MethodContainer $methods, StructAttributeModel $attribute)
213
    {
214 88
        $method = new PhpMethod($attribute->getSetterName(), array(
215 88
            $this->getStructMethodParameter($attribute, true, null),
216 66
        ));
217 88
        $this->addStructMethodSetBody($method, $attribute);
218 88
        $methods->add($method);
219 88
        return $this;
220
    }
221
    /**
222
     * @param PhpMethod $method
223
     * @param StructAttributeModel $attribute
224
     * @return Struct
225
     */
226 88
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
227
    {
228 88
        if ($attribute->isArray()) {
229 44
            $this->addStructMethodSetBodyForArray($method, $attribute);
230 33
        } else {
231 60
            $this->addStructMethodSetBodyForRestriction($method, $attribute);
232
        }
233 66
        return $this
234 88
            ->addStructMethodSetBodyAssignment($method, $attribute)
235 88
            ->addStructMethodSetBodyReturn($method);
236
    }
237
    /**
238
     * @param PhpMethod $method
239
     * @param StructAttributeModel $attribute
240
     * @return Struct
241
     */
242 88
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute)
243
    {
244 88
        $parameterName = lcfirst($attribute->getCleanName());
245 88
        if ($attribute->getRemovableFromRequest()) {
246
            $method
247 20
                ->addChild(sprintf('if (is_null($%s)) {', $parameterName))
248 20
                    ->addChild($method->getIndentedString(sprintf('unset($this->%1$s%2$s);', $parameterName, $attribute->nameIsClean() ? '' : sprintf(', $this->{\'%s\'}', addslashes($attribute->getName()))), 1))
249 20
                ->addChild('} else {', $parameterName)
0 ignored issues
show
Unused Code introduced by
The call to AbstractElement::addChild() has too many arguments starting with $parameterName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
250 20
                    ->addChild($method->getIndentedString($this->getStructMethodSetBodyAssignment($attribute, $parameterName), 1))
251 20
                ->addChild('}', $parameterName);
0 ignored issues
show
Unused Code introduced by
The call to AbstractElement::addChild() has too many arguments starting with $parameterName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
252
253 15
        } else {
254 84
            $method->addChild($this->getStructMethodSetBodyAssignment($attribute, $parameterName));
255
        }
256 88
        return $this;
257
    }
258
    /**
259
     * @param PhpMethod $method
260
     * @return Struct
261
     */
262 88
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
263
    {
264 88
        $method->addChild('return $this;');
265 88
        return $this;
266
    }
267
    /**
268
     * @param PhpMethod $method
269
     * @param StructAttributeModel $attribute
270
     * @param string $parameterName
271
     * @return Struct
272
     */
273 68
    protected function addStructMethodSetBodyForRestriction(PhpMethod $method, StructAttributeModel $attribute, $parameterName = null)
274
    {
275 68
        if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel) {
276 48
            $parameterName = empty($parameterName) ? lcfirst($attribute->getCleanName()) : $parameterName;
277
            $method
278 48
                ->addChild(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $parameterName))
279 48
                    ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value "%%s" is invalid, please use one of: %%s\', $%s, implode(\', \', %s::%s())), __LINE__);', $parameterName, $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1))
280 48
                ->addChild('}');
281 36
        }
282 68
        return $this;
283
    }
284
    /**
285
     * @param PhpMethod $method
286
     * @param StructAttributeModel $attribute
287
     * @return Struct
288
     */
289 44
    protected function addStructMethodSetBodyForArray(PhpMethod $method, StructAttributeModel $attribute)
290
    {
291 44
        if ($attribute->isArray()) {
292 44
            $model = $this->getRestrictionFromStructAttribute($attribute);
293 44
            $parameterName = lcfirst($attribute->getCleanName());
294 44
            $itemName = sprintf('%s%sItem', lcfirst($this->getModel()->getCleanName(false)), ucfirst($attribute->getCleanName()));
295 44
            if ($model instanceof StructModel) {
296
                $method
297 16
                    ->addChild('$invalidValues = array();')
298 16
                    ->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName))
299 16
                        ->addChild($method->getIndentedString(sprintf('if (!%s::%s($%s)) {', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID, $itemName), 1))
300 16
                            ->addChild($method->getIndentedString(sprintf('$invalidValues[] = var_export($%s);', $itemName), 2))
301 16
                        ->addChild($method->getIndentedString('}', 1))
302 16
                    ->addChild('}')
303 16
                    ->addChild('if (!empty($invalidValues)) {')
304 16
                        ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'Value(s) "%%s" is/are invalid, please use one of: %%s\', implode(\', \', $invalidValues), implode(\', \', %s::%s())), __LINE__);', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES), 1))
305 16
                    ->addChild('}');
306 12
            } else {
307
                $method
308 40
                    ->addChild(sprintf('foreach ($%s as $%s) {', $parameterName, $itemName))
309 40
                        ->addChild($method->getIndentedString(sprintf('if (!%s) {', $this->getStructMethodSetBodyForArrayItemSanityCheck($attribute, $itemName)), 1))
310 40
                            ->addChild($method->getIndentedString(sprintf('throw new \InvalidArgumentException(sprintf(\'The %s property can only contain items of %s, "%%s" given\', is_object($%s) ? get_class($%s) : gettype($%s)), __LINE__);', $attribute->getCleanName(), $this->getStructAttributeType($attribute, true), $itemName, $itemName, $itemName), 2))
311 40
                        ->addChild($method->getIndentedString('}', 1))
312 40
                    ->addChild('}');
313
            }
314 33
        }
315 44
        return $this;
316
    }
317
    /**
318
     * The second case which used PHP native functions is volontary limited by the native functions provided by PHP,
319
     * and the possible types defined in xsd_types.yml
320
     * @param StructAttributeModel $attribute
321
     * @param string $itemName
322
     */
323 40
    protected function getStructMethodSetBodyForArrayItemSanityCheck(StructAttributeModel $attribute, $itemName)
324
    {
325 40
        $model = $this->getModelFromStructAttribute($attribute);
326 40
        $sanityCheck = 'false';
327 40
        if ($model instanceof StructModel && $model->getIsStruct()) {
328 28
            $sanityCheck = sprintf('$%s instanceof %s', $itemName, $this->getStructAttributeType($attribute, true));
329 21
        } else {
330 24
            switch (self::getPhpType($attribute->getType())) {
331 18
                case 'int':
332 16
                    $sanityCheck = 'is_int($%s)';
333 16
                    break;
334 12
                case 'bool':
335
                    $sanityCheck = 'is_bool($%s)';
336
                    break;
337 12
                case 'float':
338
                    $sanityCheck = 'is_float($%s)';
339
                    break;
340 12
                case 'string':
341 16
                    $sanityCheck = 'is_string($%s)';
342 16
                    break;
343 18
            }
344 24
            $sanityCheck = sprintf($sanityCheck, $itemName);
345
        }
346 40
        return $sanityCheck;
347
    }
348
    /**
349
     * @param StructAttributeModel $attribute
350
     * @param string $parameterName
351
     * @return string
352
     */
353 88
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute, $parameterName)
354
    {
355 88
        if ($attribute->nameIsClean()) {
356 88
            $assignment = sprintf('$this->%s = $%s;', $attribute->getName(), $parameterName);
357 66
        } else {
358
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = $%s;', $attribute->getCleanName(), addslashes($attribute->getName()), $parameterName);
359
        }
360 88
        return $assignment;
361
    }
362
    /**
363
     * @param PhpMethod $method
364
     * @param StructAttributeModel $attribute
365
     * @param string $thisAccess
366
     * @return Struct
367
     */
368 88
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
369
    {
370 66
        return $this
371 88
            ->addStructMethodGetBodyForXml($method, $attribute, $thisAccess)
372 88
            ->addStructMethodGetBodyReturn($method, $attribute, $thisAccess);
373
    }
374
    /**
375
     * @param PhpMethod $method
376
     * @param StructAttributeModel $attribute
377
     * @param string $thisAccess
378
     * @return Struct
379
     */
380 88
    protected function addStructMethodGetBodyForXml(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
381
    {
382 88
        if ($attribute->isXml()) {
383
            $method
384 4
                ->addChild(sprintf('if (!empty($this->%1$s) && !($this->%1$s instanceof \DOMDocument)) {', $thisAccess))
385 4
                    ->addChild($method->getIndentedString('$dom = new \DOMDocument(\'1.0\', \'UTF-8\');', 1))
386 4
                    ->addChild($method->getIndentedString('$dom->formatOutput = true;', 1))
387 4
                    ->addChild($method->getIndentedString(sprintf('if ($dom->loadXML($this->%s)) {', $thisAccess), 1))
388 4
                    ->addChild($method->getIndentedString(sprintf('$this->%s($dom);', $attribute->getSetterName()), 2))
389 4
                    ->addChild($method->getIndentedString('}', 1))
390 4
                    ->addChild($method->getIndentedString('unset($dom);', 1))
391 4
                ->addChild('}');
392 3
        }
393 88
        return $this;
394
    }
395
    /**
396
     * @param PhpMethod $method
397
     * @param StructAttributeModel $attribute
398
     * @param string $thisAccess
399
     * @return Struct
400
     */
401 88
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
402
    {
403 88
        $return = sprintf('return $this->%s;', $thisAccess);
404 88
        if ($attribute->isXml()) {
405 4
            if ($attribute->getRemovableFromRequest()) {
406
                $return = sprintf('return ($asString && isset($this->%1$s) && ($this->%1$s instanceof \DOMDocument) && $this->%1$s->hasChildNodes()) ? $this->%1$s->saveXML($this->%1$s->childNodes->item(0)) : (isset($this->%1$s) ? $this->%1$s : null);', $thisAccess);
407
            } else {
408 4
                $return = sprintf('return ($asString && ($this->%1$s instanceof \DOMDocument) && $this->%1$s->hasChildNodes()) ? $this->%1$s->saveXML($this->%1$s->childNodes->item(0)) : $this->%1$s;', $thisAccess);
409
            }
410 88
        } elseif ($attribute->getRemovableFromRequest()) {
411 20
            $return = sprintf('return isset($this->%1$s) ? $this->%1$s : null;', $thisAccess);
412 15
        }
413 88
        $method->addChild($return);
414 88
        return $this;
415
    }
416
    /**
417
     * @param MethodContainer $methods
418
     * @param StructAttributeModel $attribute
419
     * @return Struct
420
     */
421 88
    protected function addStructMethodGet(MethodContainer $methods, StructAttributeModel $attribute)
422
    {
423 88
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
424 88
        if ($attribute->nameIsClean()) {
425 88
            $thisAccess = sprintf('%s', $attribute->getName());
426 66
        } else {
427
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
428
        }
429 88
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
430 88
        $methods->add($method);
431 88
        return $this;
432
    }
433
    /**
434
     * @param StructAttributeModel $attribute
435
     * @return PhpFunctionParameter[]
436
     */
437 88
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
438
    {
439 88
        $parameters = array();
440 88
        if ($attribute->isXml()) {
441 4
            $parameters[] = new PhpFunctionParameter('asString', true);
442 3
        }
443 88
        return $parameters;
444
    }
445
    /**
446
     * @param MethodContainer $methods
447
     * @return Struct
448
     */
449 88
    protected function addStructMethodSetState(MethodContainer $methods)
450
    {
451 88
        $method = new PhpMethod(self::METHOD_SET_STATE, array(
452 88
            new PhpFunctionParameter('array', PhpFunctionParameter::NO_VALUE, 'array'),
453 88
        ), PhpMethod::ACCESS_PUBLIC, false, true);
454 88
        $method->addChild(sprintf('return parent::__set_state($array);'));
455 88
        $methods->add($method);
456 88
        return $this;
457
    }
458
    /**
459
     * @return PhpAnnotationBlock|null
460
     */
461 88
    protected function getMethodAnnotationBlock(PhpMethod $method)
462
    {
463 88
        return $this->getStructMethodAnnotationBlock($method);
464
    }
465
    /**
466
     * @param PhpMethod $method
467
     * @return PhpAnnotationBlock|null
468
     */
469 88
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
470
    {
471 88
        $annotationBlock = null;
472 88
        switch ($method->getName()) {
473 88
            case self::METHOD_CONSTRUCT:
474 68
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
475 68
                break;
476 88
            case self::METHOD_SET_STATE:
477 68
                $annotationBlock = $this->getStructMethodSetStateAnnotationBlock();
478 68
                break;
479 88
            case strpos($method->getName(), 'get') === 0:
480 88
            case strpos($method->getName(), 'set') === 0:
481 88
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
482 88
                break;
483 44
            case strpos($method->getName(), 'addTo') === 0:
484 44
                $annotationBlock = $this->getStructMethodsAddToAnnotationBlock($method);
485 44
                break;
486 66
        }
487 88
        return $annotationBlock;
488
    }
489
    /**
490
     * @return PhpAnnotationBlock
491
     */
492 88
    protected function getStructMethodConstructAnnotationBlock()
493
    {
494 88
        $annotationBlock = new PhpAnnotationBlock(array(
495 88
            sprintf('Constructor method for %s', $this->getModel()->getName()),
496 66
        ));
497 88
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
498 88
        return $annotationBlock;
499
    }
500
    /**
501
     * @return PhpAnnotationBlock
502
     */
503 88
    protected function getStructMethodSetStateAnnotationBlock()
504
    {
505 88
        return new PhpAnnotationBlock(array(
506 88
            'Method called when an object has been exported with var_export() functions',
507 88
            'It allows to return an object instantiated with the values',
508 88
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
509 88
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
510 88
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $array the exported values'),
511 88
            new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)),
512 66
        ));
513
    }
514
    /**
515
     * @param PhpMethod $method
516
     * @return PhpAnnotationBlock
517
     */
518 88
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
519
    {
520 88
        $parameters = $method->getParameters();
521 88
        $setOrGet = strtolower(substr($method->getName(), 0, 3));
522 88
        $parameter = array_shift($parameters);
523
        /**
524
         * Only set parameter must be based on a potential PhpFunctionParameter
525
         */
526 88
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
527 88
            $parameterName = ucfirst($parameter->getName());
528 66
        } else {
529 88
            $parameterName = substr($method->getName(), 3);
530
        }
531 88
        $attribute = $this->getModel()->getAttribute($parameterName);
532 88
        if (!$attribute instanceof StructAttributeModel) {
533 48
            $parameterName = lcfirst($parameterName);
534 48
            $attribute = $this->getModel()->getAttribute($parameterName);
535 36
        }
536 88
        $setValueAnnotation = '%s %s value';
537 88
        $annotationBlock = new PhpAnnotationBlock();
538 88
        if ($attribute instanceof StructAttributeModel) {
539 88
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
540 88
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
541 66
        } elseif (empty($attribute)) {
542 4
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
543 4
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
544 3
        }
545 88
        return $annotationBlock;
546
    }
547
    /**
548
     * @param string $setOrGet
549
     * @param PhpAnnotationBlock $annotationBlock
550
     * @param StructAttributeModel $attribute
551
     * @return Struct
552
     */
553 88
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
554
    {
555
        switch ($setOrGet) {
556 88
            case 'set':
557 88
                if ($attribute->getRemovableFromRequest()) {
558 20
                    $annotationBlock->addChild('This property is removable from request (nillable=true+minOccurs=0), therefore if the value assigned to this property is null, it is removed from this object');
559 15
                }
560 88
                if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel && !$this->getModel()->isArray()) {
561
                    $annotationBlock
562 44
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
563 44
                        ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
564 44
                        ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
565 88
                } elseif ($attribute->isArray()) {
566 44
                    $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
567 33
                }
568 88
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
569 88
                break;
570 66
            case 'get':
571 88
                if ($attribute->getRemovableFromRequest()) {
572 20
                    $annotationBlock->addChild('An additional test has been added (isset) before returning the property value as this property may have been unset before, due to the fact that this property is removable from the request (nillable=true+minOccurs=0)');
573 15
                }
574 66
                $this
575 88
                    ->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute)
576 88
                    ->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute), $attribute);
577 88
                break;
578
        }
579 88
        return $this;
580
    }
581
    /**
582
     * @param string $setOrGet
583
     * @param PhpAnnotationBlock $annotationBlock
584
     * @param string $attributeName
585
     * @return Struct
586
     */
587 4
    protected function addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, PhpAnnotationBlock $annotationBlock, $attributeName)
588
    {
589
        switch ($setOrGet) {
590 4
            case 'set':
591
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, lcfirst($attributeName), lcfirst($attributeName));
592
                break;
593 3
            case 'get':
594 4
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, lcfirst($attributeName));
595 4
                break;
596
        }
597 4
        return $this;
598
    }
599
    /**
600
     * @param PhpAnnotationBlock $annotationBlock
601
     * @param string $type
602
     * @param string $name
603
     * @return Struct
604
     */
605 88
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
606
    {
607
        $annotationBlock
608 88
            ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))
609 88
            ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
610 88
        return $this;
611
    }
612
    /**
613
     * @param PhpAnnotationBlock $annotationBlock
614
     * @param string $attributeType
615
     * @param StructAttributeModel $attribute
616
     * @return Struct
617
     */
618 88
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attributeType, StructAttributeModel $attribute = null)
0 ignored issues
show
Unused Code introduced by
The parameter $attribute is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
619
    {
620 88
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attributeType));
621 88
        return $this;
622
    }
623
    /**
624
     * @param PhpAnnotationBlock $annotationBlock
625
     * @param StructAttributeModel $attribute
626
     * @return Struct
627
     */
628 88
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
629
    {
630 88
        if ($attribute->isXml()) {
631
            $annotationBlock
632 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::loadXML()'))
633 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::hasChildNodes()'))
634 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::saveXML()'))
635 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMNode::item()'))
636 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(true), $attribute->getSetterName())))
637 4
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'bool $asString true: returns XML string, false: returns \DOMDocument'));
638 3
        }
639 88
        return $this;
640
    }
641
    /**
642
     * @param PhpAnnotationBlock $annotationBlock
643
     * @return Struct
644
     */
645 88
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
646
    {
647 66
        return $this
648 88
            ->addStructPropertiesToAnnotationBlockUses($annotationBlock)
649 88
            ->addStructPropertiesToAnnotationBlockParams($annotationBlock);
650
    }
651
    /**
652
     * @param PhpAnnotationBlock $annotationBlock
653
     * @return Struct
654
     */
655 88
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
656
    {
657 88
        foreach ($this->getModelAttributes() as $attribute) {
658 88
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
659 66
        }
660 88
        return $this;
661
    }
662
    /**
663
     * @param PhpAnnotationBlock $annotationBlock
664
     * @return Struct
665
     */
666 88
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
667
    {
668 88
        foreach ($this->getModelAttributes() as $attribute) {
669 88
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
670 66
        }
671 88
        return $this;
672
    }
673
    /**
674
     * @param PhpMethod $method
675
     * @return PhpAnnotationBlock
676
     */
677 44
    protected function getStructMethodsAddToAnnotationBlock(PhpMethod $method)
678
    {
679 44
        $attributeName = str_replace('addTo', '', $method->getName());
680 44
        $attribute = $this->getModel()->getAttribute($attributeName);
681 44
        if (!$attribute instanceof StructAttributeModel) {
682 20
            $attribute = $this->getModel()->getAttribute(lcfirst($attributeName));
683 15
        }
684 44
        $model = $this->getRestrictionFromStructAttribute($attribute);
685 44
        $annotationBlock = new PhpAnnotationBlock();
686 44
        if ($attribute instanceof StructAttributeModel) {
687 44
            $annotationBlock->addChild(sprintf('Add item to %s value', $attribute->getCleanName()));
688 44
            if ($model instanceof StructModel) {
689
                $annotationBlock
690 16
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
691 16
                    ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)));
692 12
            }
693
            $annotationBlock
694 44
                ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'))
695 44
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeTypeSetAnnotation($attribute, false))))
696 44
                ->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
697 33
        }
698 44
        return $annotationBlock;
699
    }
700
    /**
701
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
702
     * @return StructModel
703
     */
704 136
    public function getModel()
705
    {
706 136
        return parent::getModel();
707
    }
708
    /**
709
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
710
     * @throws \InvalidaArgumentException
711
     * @param AbstractModel $model
712
     * @return StructArray
713
     */
714 136
    public function setModel(AbstractModel $model)
715
    {
716 136
        if (!$model instanceof StructModel) {
717 4
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
718
        }
719 132
        return parent::setModel($model);
720
    }
721
}
722