Completed
Push — feature/issue-158 ( 8b9ab6...25acf9 )
by Mikaël
23:05 queued 36s
created

Struct   F

Complexity

Total Complexity 95

Size/Duplication

Total Lines 606
Duplicated Lines 0 %

Test Coverage

Coverage 98.12%

Importance

Changes 0
Metric Value
eloc 243
dl 0
loc 606
ccs 313
cts 319
cp 0.9812
rs 2
c 0
b 0
f 0
wmc 95

42 Methods

Rating   Name   Duplication   Size   Complexity  
A getStructMethodParameter() 0 7 4
A getClassProperties() 0 5 3
A getModelAttributes() 0 3 1
A getStructMethodParametersValues() 0 7 2
A addStructMethodConstructBodyForAttribute() 0 5 2
A getStructMethodParameterType() 0 3 1
A getConstantAnnotationBlock() 0 2 1
A getPropertyAnnotationBlock() 0 13 3
A getClassConstants() 0 2 1
A addStructMethodAddTo() 0 10 2
A addStructMethodConstruct() 0 6 1
A getClassMethods() 0 3 1
A addStructMethodConstructBody() 0 10 3
A addStructMethodsSetAndGet() 0 6 2
A addStructPropertiesToAnnotationBlockParams() 0 6 2
A getModel() 0 3 1
A addStructMethodsSetAndGetAnnotationBlockFromScalar() 0 11 3
A addStructPropertiesToAnnotationBlockUses() 0 6 2
A getStructMethodsAddToAnnotationBlock() 0 17 4
A addStructMethodAddToBody() 0 10 2
A addStructMethodGet() 0 11 2
A addStructMethodGetBody() 0 3 1
A getStructMethodSetStateAnnotationBlock() 0 9 1
A addStructMethodSetBodyReturn() 0 4 1
A addStructMethodSetState() 0 8 1
A addStructMethodSet() 0 8 1
A addStructMethodsGetAnnotationBlockFromXmlAttribute() 0 12 2
A addStructMethodGetBodyReturn() 0 14 4
A addStructMethodSetBody() 0 8 2
A getStructMethodAnnotationBlock() 0 19 6
A getMethodAnnotationBlock() 0 3 1
B addStructMethodsSetAndGetAnnotationBlockFromStructAttribute() 0 27 8
A addStructPropertiesToAnnotationBlock() 0 3 1
A addStructMethodsGetAnnotationBlock() 0 4 1
A getStructMethodGetParameters() 0 7 2
A getStructMethodConstructAnnotationBlock() 0 7 1
A getStructMethodSetBodyAssignment() 0 14 3
A addStructMethodSetBodyAssignment() 0 14 3
A setModel() 0 6 2
A addStructMethodGetBodyForXml() 0 13 2
B getStructMethodsSetAndGetAnnotationBlock() 0 38 8
A addStructMethodsSetAnnotationBlock() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Struct often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Struct, and based on these observations, apply Extract Interface, too.

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
use WsdlToPhp\PackageGenerator\File\Validation\Rules;
19
20
class Struct extends AbstractModelFile
21
{
22
    /**
23
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getClassConstants()
24
     */
25 198
    protected function getClassConstants(ConstantContainer $constants)
26
    {
27 198
    }
28
    /**
29
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getConstantAnnotationBlock()
30
     */
31
    protected function getConstantAnnotationBlock(PhpConstant $constant)
32
    {
33
    }
34
    /**
35
     * @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.
36
     * @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
37
     * @return StructAttributeContainer
38
     */
39 198
    protected function getModelAttributes($includeInheritanceAttributes = false, $requiredFirst = true)
40
    {
41 198
        return $this->getModel()->getAttributes($includeInheritanceAttributes, $requiredFirst);
42
    }
43
    /**
44
     * @param PropertyContainer $properties
45
     */
46 240
    protected function getClassProperties(PropertyContainer $properties)
47
    {
48 240
        if ($this->getModel()->getAttributes()->count() > 0) {
49 198
            foreach ($this->getModelAttributes() as $attribute) {
50 198
                $properties->add(new PhpProperty($attribute->getCleanName(), PhpProperty::NO_VALUE));
51 99
            }
52 99
        }
53 240
    }
54
    /**
55
     * @return PhpAnnotationBlock $property
56
     */
57 198
    protected function getPropertyAnnotationBlock(PhpProperty $property)
58
    {
59 198
        $annotationBlock = new PhpAnnotationBlock();
60 198
        $annotationBlock->addChild(sprintf('The %s', $property->getName()));
61 198
        $attribute = $this->getModel()->getAttribute($property->getName());
62 198
        if (!$attribute instanceof StructAttributeModel) {
63 6
            $attribute = $this->getModel()->getAttributeByCleanName($property->getName());
64 3
        }
65 198
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
66 198
            $this->defineModelAnnotationsFromWsdl($annotationBlock, $attribute);
67 198
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_VAR, $this->getStructAttributeTypeSetAnnotation($attribute, true)));
68 99
        }
69 198
        return $annotationBlock;
70
    }
71
    /**
72
     * @param MethodContainer $methods
73
     */
74 198
    protected function getClassMethods(MethodContainer $methods)
75
    {
76 198
        $this->addStructMethodConstruct($methods)->addStructMethodsSetAndGet($methods)->addStructMethodSetState($methods);
77 198
    }
78
    /**
79
     * @param MethodContainer $methods
80
     * @return Struct
81
     */
82 198
    protected function addStructMethodConstruct(MethodContainer $methods)
83
    {
84 198
        $method = new PhpMethod(self::METHOD_CONSTRUCT, $this->getStructMethodParametersValues());
85 198
        $this->addStructMethodConstructBody($method);
86 198
        $methods->add($method);
87 198
        return $this;
88
    }
89
    /**
90
     * @param PhpMethod $method
91
     * @return Struct
92
     */
93 198
    protected function addStructMethodConstructBody(PhpMethod $method)
94
    {
95 198
        $count = $this->getModelAttributes()->count();
96 198
        foreach ($this->getModelAttributes() as $index => $attribute) {
97 198
            if ($index === 0) {
98 198
                $method->addChild('$this');
99 99
            }
100 198
            $this->addStructMethodConstructBodyForAttribute($method, $attribute, $count - 1 === $index);
101 99
        }
102 198
        return $this;
103
    }
104
    /**
105
     * @param PhpMethod $method
106
     * @param StructAttributeModel $attribute
107
     * @param bool $isLast
108
     * @return Struct
109
     */
110 198
    protected function addStructMethodConstructBodyForAttribute(PhpMethod $method, StructAttributeModel $attribute, $isLast)
111
    {
112 198
        $uniqueString = $attribute->getUniqueString($attribute->getCleanName(), 'method');
113 198
        $method->addChild($method->getIndentedString(sprintf('->%s($%s)%s', $attribute->getSetterName(), lcfirst($uniqueString), $isLast ? ';' : ''), 1));
114 198
        return $this;
115
    }
116
    /**
117
     * @return PhpFunctionParameter[]
118
     */
119 198
    protected function getStructMethodParametersValues()
120
    {
121 198
        $parametersValues = [];
122 198
        foreach ($this->getModelAttributes() as $attribute) {
123 198
            $parametersValues[] = $this->getStructMethodParameter($attribute, true);
124 99
        }
125 198
        return $parametersValues;
126
    }
127
    /**
128
     * @param StructAttributeModel $attribute
129
     * @param bool $lowCaseFirstLetter
130
     * @param mixed $defaultValue
131
     * @return PhpFunctionParameter
132
     */
133 198
    protected function getStructMethodParameter(StructAttributeModel $attribute, $lowCaseFirstLetter = false, $defaultValue = null)
134
    {
135
        try {
136 198
            $uniqueString = $attribute->getUniqueString($attribute->getCleanName(), 'method');
137 198
            return new PhpFunctionParameter($lowCaseFirstLetter ? lcfirst($uniqueString) : $uniqueString, isset($defaultValue) ? $defaultValue : $attribute->getDefaultValue(), $this->getStructMethodParameterType($attribute));
138
        } catch (\InvalidArgumentException $exception) {
139
            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);
140
        }
141
    }
142
    /**
143
     * @param StructAttributeModel $attribute
144
     * @param bool $returnArrayType
145
     * @return string|null
146
     */
147 198
    protected function getStructMethodParameterType(StructAttributeModel $attribute, $returnArrayType = true)
148
    {
149 198
        return self::getValidType($this->getStructAttributeTypeHint($attribute, $returnArrayType), $this->getGenerator()->getOptionXsdTypesPath(), null);
150
    }
151
    /**
152
     * @param MethodContainer $methods
153
     * @return Struct
154
     */
155 198
    protected function addStructMethodsSetAndGet(MethodContainer $methods)
156
    {
157 198
        foreach ($this->getModelAttributes() as $attribute) {
158 198
            $this->addStructMethodGet($methods, $attribute)->addStructMethodSet($methods, $attribute)->addStructMethodAddTo($methods, $attribute);
159 99
        }
160 198
        return $this;
161
    }
162
    /**
163
     * @param MethodContainer $methods
164
     * @param StructAttributeModel $attribute
165
     * @return Struct
166
     */
167 198
    protected function addStructMethodAddTo(MethodContainer $methods, StructAttributeModel $attribute)
168
    {
169 198
        if ($attribute->isArray()) {
170 114
            $method = new PhpMethod(sprintf('addTo%s', ucfirst($attribute->getCleanName())), [
171 114
                new PhpFunctionParameter('item', PhpFunctionParameter::NO_VALUE, $this->getStructMethodParameterType($attribute, false)),
172 57
            ]);
173 114
            $this->addStructMethodAddToBody($method, $attribute);
174 114
            $methods->add($method);
175 57
        }
176 198
        return $this;
177
    }
178
    /**
179
     * @param PhpMethod $method
180
     * @param StructAttributeModel $attribute
181
     * @return Struct
182
     */
183 114
    protected function addStructMethodAddToBody(PhpMethod $method, StructAttributeModel $attribute)
184
    {
185 114
        if ($this->getGenerator()->getOptionValidation()) {
186 114
            $rules = new Rules($this, $method, $attribute);
187 114
            $rules->applyRules('item', true);
188 57
        }
189
        $method
190 114
            ->addChild(sprintf('$this->%s[] = $item;', $attribute->getCleanName()))
191 114
            ->addChild('return $this;');
192 114
        return $this;
193
    }
194
    /**
195
     * @param MethodContainer $methods
196
     * @param StructAttributeModel $attribute
197
     * @return Struct
198
     */
199 198
    protected function addStructMethodSet(MethodContainer $methods, StructAttributeModel $attribute)
200
    {
201 198
        $method = new PhpMethod($attribute->getSetterName(), [
202 198
            $this->getStructMethodParameter($attribute, true, null),
203 99
        ]);
204 198
        $this->addStructMethodSetBody($method, $attribute);
205 198
        $methods->add($method);
206 198
        return $this;
207
    }
208
    /**
209
     * @param PhpMethod $method
210
     * @param StructAttributeModel $attribute
211
     * @return Struct
212
     */
213 198
    protected function addStructMethodSetBody(PhpMethod $method, StructAttributeModel $attribute)
214
    {
215 198
        if ($this->getGenerator()->getOptionValidation()) {
216 192
            $uniqueString = $attribute->getUniqueString($attribute->getCleanName());
217 192
            $rules = new Rules($this, $method, $attribute);
218 192
            $rules->applyRules(lcfirst($uniqueString));
219 96
        }
220 198
        return $this->addStructMethodSetBodyAssignment($method, $attribute)->addStructMethodSetBodyReturn($method);
221
    }
222
    /**
223
     * @param PhpMethod $method
224
     * @param StructAttributeModel $attribute
225
     * @return Struct
226
     */
227 198
    protected function addStructMethodSetBodyAssignment(PhpMethod $method, StructAttributeModel $attribute)
228
    {
229 198
        $uniqueString = $attribute->getUniqueString($attribute->getCleanName());
230 198
        $parameterName = lcfirst($uniqueString);
231 198
        if ($attribute->getRemovableFromRequest()) {
232 30
            $method->addChild(sprintf('if (is_null($%1$s) || (is_array($%1$s) && empty($%1$s))) {', $parameterName))
233 30
                ->addChild($method->getIndentedString(sprintf('unset($this->%1$s%2$s);', $attribute->getCleanName(), $attribute->nameIsClean() ? '' : sprintf(', $this->{\'%s\'}', addslashes($attribute->getName()))), 1))
234 30
                ->addChild('} else {')
235 30
                ->addChild($method->getIndentedString($this->getStructMethodSetBodyAssignment($attribute, $parameterName), 1))
236 30
                ->addChild('}');
237 15
        } else {
238 192
            $method->addChild($this->getStructMethodSetBodyAssignment($attribute, $parameterName));
239
        }
240 198
        return $this;
241
    }
242
    /**
243
     * @param PhpMethod $method
244
     * @return Struct
245
     */
246 198
    protected function addStructMethodSetBodyReturn(PhpMethod $method)
247
    {
248 198
        $method->addChild('return $this;');
249 198
        return $this;
250
    }
251
    /**
252
     * @param StructAttributeModel $attribute
253
     * @param string $parameterName
254
     * @return string
255
     */
256 198
    protected function getStructMethodSetBodyAssignment(StructAttributeModel $attribute, $parameterName)
257
    {
258 198
        $isList = $this->isAttributeAList($attribute);
259 198
        $prefix = '$';
260 198
        if ($isList) {
261 6
            $prefix = '';
262 6
            $parameterName = sprintf('is_array($%1$s) ? implode(\' \', $%1$s) : null', $parameterName);
263 3
        }
264 198
        if ($attribute->nameIsClean()) {
265 198
            $assignment = sprintf('$this->%s = %s%s;', $attribute->getName(), $prefix, $parameterName);
266 99
        } else {
267 6
            $assignment = sprintf('$this->%s = $this->{\'%s\'} = %s%s;', $attribute->getCleanName(), addslashes($attribute->getName()), $prefix, $parameterName);
268
        }
269 198
        return $assignment;
270
    }
271
    /**
272
     * @param PhpMethod $method
273
     * @param StructAttributeModel $attribute
274
     * @param string $thisAccess
275
     * @return Struct
276
     */
277 198
    protected function addStructMethodGetBody(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
278
    {
279 198
        return $this->addStructMethodGetBodyForXml($method, $attribute, $thisAccess)->addStructMethodGetBodyReturn($method, $attribute, $thisAccess);
280
    }
281
    /**
282
     * @param PhpMethod $method
283
     * @param StructAttributeModel $attribute
284
     * @param string $thisAccess
285
     * @return Struct
286
     */
287 198
    protected function addStructMethodGetBodyForXml(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
288
    {
289 198
        if ($attribute->isXml()) {
290 6
            $method->addChild(sprintf('if (!empty($this->%1$s) && !($this->%1$s instanceof \DOMDocument)) {', $thisAccess))
291 6
                ->addChild($method->getIndentedString('$dom = new \DOMDocument(\'1.0\', \'UTF-8\');', 1))
292 6
                ->addChild($method->getIndentedString('$dom->formatOutput = true;', 1))
293 6
                ->addChild($method->getIndentedString(sprintf('if ($dom->loadXML($this->%s)) {', $thisAccess), 1))
294 6
                ->addChild($method->getIndentedString(sprintf('$this->%s($dom);', $attribute->getSetterName()), 2))
295 6
                ->addChild($method->getIndentedString('}', 1))
296 6
                ->addChild($method->getIndentedString('unset($dom);', 1))
297 6
                ->addChild('}');
298 3
        }
299 198
        return $this;
300
    }
301
    /**
302
     * @param PhpMethod $method
303
     * @param StructAttributeModel $attribute
304
     * @param string $thisAccess
305
     * @return Struct
306
     */
307 198
    protected function addStructMethodGetBodyReturn(PhpMethod $method, StructAttributeModel $attribute, $thisAccess)
308
    {
309 198
        $return = sprintf('return $this->%s;', $thisAccess);
310 198
        if ($attribute->isXml()) {
311 6
            if ($attribute->getRemovableFromRequest()) {
312
                $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);
313
            } else {
314 6
                $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);
315
            }
316 198
        } elseif ($attribute->getRemovableFromRequest()) {
317 30
            $return = sprintf('return isset($this->%1$s) ? $this->%1$s : null;', $thisAccess);
318 15
        }
319 198
        $method->addChild($return);
320 198
        return $this;
321
    }
322
    /**
323
     * @param MethodContainer $methods
324
     * @param StructAttributeModel $attribute
325
     * @return Struct
326
     */
327 198
    protected function addStructMethodGet(MethodContainer $methods, StructAttributeModel $attribute)
328
    {
329 198
        $method = new PhpMethod($attribute->getGetterName(), $this->getStructMethodGetParameters($attribute));
330 198
        if ($attribute->nameIsClean()) {
331 198
            $thisAccess = sprintf('%s', $attribute->getName());
332 99
        } else {
333 6
            $thisAccess = sprintf('{\'%s\'}', addslashes($attribute->getName()));
334
        }
335 198
        $this->addStructMethodGetBody($method, $attribute, $thisAccess);
336 198
        $methods->add($method);
337 198
        return $this;
338
    }
339
    /**
340
     * @param StructAttributeModel $attribute
341
     * @return PhpFunctionParameter[]
342
     */
343 198
    protected function getStructMethodGetParameters(StructAttributeModel $attribute)
344
    {
345 198
        $parameters = [];
346 198
        if ($attribute->isXml()) {
347 6
            $parameters[] = new PhpFunctionParameter('asString', true);
348 3
        }
349 198
        return $parameters;
350
    }
351
    /**
352
     * @param MethodContainer $methods
353
     * @return Struct
354
     */
355 198
    protected function addStructMethodSetState(MethodContainer $methods)
356
    {
357 198
        $method = new PhpMethod(self::METHOD_SET_STATE, [
358 198
            new PhpFunctionParameter('array', PhpFunctionParameter::NO_VALUE, 'array'),
359 198
        ], PhpMethod::ACCESS_PUBLIC, false, true);
360 198
        $method->addChild(sprintf('return parent::__set_state($array);'));
361 198
        $methods->add($method);
362 198
        return $this;
363
    }
364
    /**
365
     * @return PhpAnnotationBlock|null
366
     */
367 198
    protected function getMethodAnnotationBlock(PhpMethod $method)
368
    {
369 198
        return $this->getStructMethodAnnotationBlock($method);
370
    }
371
    /**
372
     * @param PhpMethod $method
373
     * @return PhpAnnotationBlock|null
374
     */
375 198
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
376
    {
377 198
        $annotationBlock = null;
378 198
        switch ($method->getName()) {
379 198
            case self::METHOD_CONSTRUCT:
380 198
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
381 198
                break;
382 198
            case self::METHOD_SET_STATE:
383 198
                $annotationBlock = $this->getStructMethodSetStateAnnotationBlock();
384 198
                break;
385 198
            case strpos($method->getName(), 'get') === 0:
386 198
            case strpos($method->getName(), 'set') === 0:
387 198
                $annotationBlock = $this->getStructMethodsSetAndGetAnnotationBlock($method);
388 198
                break;
389 114
            case strpos($method->getName(), 'addTo') === 0:
390 114
                $annotationBlock = $this->getStructMethodsAddToAnnotationBlock($method);
391 114
                break;
392 99
        }
393 198
        return $annotationBlock;
394
    }
395
    /**
396
     * @return PhpAnnotationBlock
397
     */
398 198
    protected function getStructMethodConstructAnnotationBlock()
399
    {
400 198
        $annotationBlock = new PhpAnnotationBlock([
401 198
            sprintf('Constructor method for %s', $this->getModel()->getName()),
402 99
        ]);
403 198
        $this->addStructPropertiesToAnnotationBlock($annotationBlock);
404 198
        return $annotationBlock;
405
    }
406
    /**
407
     * @return PhpAnnotationBlock
408
     */
409 198
    protected function getStructMethodSetStateAnnotationBlock()
410
    {
411 198
        return new PhpAnnotationBlock([
412 198
            'Method called when an object has been exported with var_export() functions',
413 198
            'It allows to return an object instantiated with the values',
414 198
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
415 198
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::__set_state()', $this->getModel()->getExtends(true))),
416 198
            new PhpAnnotation(self::ANNOTATION_PARAM, 'array $array the exported values'),
417 198
            new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)),
418 99
        ]);
419
    }
420
    /**
421
     * @param PhpMethod $method
422
     * @return PhpAnnotationBlock
423
     */
424 198
    protected function getStructMethodsSetAndGetAnnotationBlock(PhpMethod $method)
425
    {
426 198
        $parameters = $method->getParameters();
427 198
        $setOrGet = strtolower(substr($method->getName(), 0, 3));
428 198
        $parameter = array_shift($parameters);
429
        /**
430
         * Only set parameter must be based on a potential PhpFunctionParameter
431
         */
432 198
        if ($parameter instanceof PhpFunctionParameter && $setOrGet === 'set') {
433 198
            $parameterName = ucfirst($parameter->getName());
434 99
        } else {
435 198
            $parameterName = substr($method->getName(), 3);
436
        }
437
        /**
438
         * Since properties can be duplicated with different case, we assume that _\d+ is replaceable by an empty string as methods are "duplicated" with this suffix
439
         */
440 198
        $parameterName = preg_replace('/(_\d+)/', '', $parameterName);
441 198
        $attribute = $this->getModel()->getAttribute($parameterName);
442 198
        if (!$attribute instanceof StructAttributeModel) {
443 90
            $attribute = $this->getModel()->getAttributeByCleanName($parameterName);
444 45
        }
445 198
        if (!$attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
446 90
            $parameterName = lcfirst($parameterName);
447 90
            $attribute = $this->getModel()->getAttribute($parameterName);
448 90
            if (!$attribute instanceof StructAttributeModel) {
449 12
                $attribute = $this->getModel()->getAttributeByCleanName($parameterName);
450 6
            }
451 45
        }
452 198
        $setValueAnnotation = '%s %s value';
453 198
        $annotationBlock = new PhpAnnotationBlock();
454 198
        if ($attribute instanceof StructAttributeModel) {
0 ignored issues
show
introduced by
$attribute is always a sub-type of WsdlToPhp\PackageGenerator\Model\StructAttribute.
Loading history...
455 198
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), $parameterName));
456 198
            $this->addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, $annotationBlock, $attribute);
457 99
        } elseif (empty($attribute)) {
458 6
            $annotationBlock->addChild(sprintf($setValueAnnotation, ucfirst($setOrGet), lcfirst($parameterName)));
459 6
            $this->addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, $annotationBlock, $parameterName);
460 3
        }
461 198
        return $annotationBlock;
462
    }
463
    /**
464
     * @param string $setOrGet
465
     * @param PhpAnnotationBlock $annotationBlock
466
     * @param StructAttributeModel $attribute
467
     * @return Struct
468
     */
469 198
    protected function addStructMethodsSetAndGetAnnotationBlockFromStructAttribute($setOrGet, PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
470
    {
471 33
        switch ($setOrGet) {
472 198
            case 'set':
473 198
                if ($attribute->getRemovableFromRequest()) {
474 30
                    $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');
475 15
                }
476 198
                if ($this->getGenerator()->getOptionValidation()) {
477 192
                    if (($model = $this->getRestrictionFromStructAttribute($attribute)) instanceof StructModel) {
478
                        $annotationBlock
479 96
                            ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))
480 96
                            ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)))
481 96
                            ->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
482 189
                    } elseif ($attribute->isArray()) {
483 108
                        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'));
484 54
                    }
485 96
                }
486 198
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()));
487 198
                break;
488 198
            case 'get':
489 198
                if ($attribute->getRemovableFromRequest()) {
490 30
                    $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)');
491 15
                }
492 198
                $this->addStructMethodsGetAnnotationBlockFromXmlAttribute($annotationBlock, $attribute)->addStructMethodsGetAnnotationBlock($annotationBlock, $this->getStructAttributeTypeGetAnnotation($attribute));
493 198
                break;
494
        }
495 198
        return $this;
496
    }
497
    /**
498
     * @param string $setOrGet
499
     * @param PhpAnnotationBlock $annotationBlock
500
     * @param string $attributeName
501
     * @return Struct
502
     */
503 6
    protected function addStructMethodsSetAndGetAnnotationBlockFromScalar($setOrGet, PhpAnnotationBlock $annotationBlock, $attributeName)
504
    {
505 1
        switch ($setOrGet) {
506 6
            case 'set':
507 6
                $this->addStructMethodsSetAnnotationBlock($annotationBlock, lcfirst($attributeName), lcfirst($attributeName));
508 6
                break;
509 6
            case 'get':
510 6
                $this->addStructMethodsGetAnnotationBlock($annotationBlock, lcfirst($attributeName));
511 6
                break;
512
        }
513 6
        return $this;
514
    }
515
    /**
516
     * @param PhpAnnotationBlock $annotationBlock
517
     * @param string $type
518
     * @param string $name
519
     * @return Struct
520
     */
521 198
    protected function addStructMethodsSetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $type, $name)
522
    {
523 198
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $type, $name)))->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
524 198
        return $this;
525
    }
526
    /**
527
     * @param PhpAnnotationBlock $annotationBlock
528
     * @param string $attributeType
529
     * @return Struct
530
     */
531 198
    protected function addStructMethodsGetAnnotationBlock(PhpAnnotationBlock $annotationBlock, $attributeType)
532
    {
533 198
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $attributeType));
534 198
        return $this;
535
    }
536
    /**
537
     * @param PhpAnnotationBlock $annotationBlock
538
     * @param StructAttributeModel $attribute
539
     * @return Struct
540
     */
541 198
    protected function addStructMethodsGetAnnotationBlockFromXmlAttribute(PhpAnnotationBlock $annotationBlock, StructAttributeModel $attribute)
542
    {
543 198
        if ($attribute->isXml()) {
544 6
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::loadXML()'))
545 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::hasChildNodes()'))
546 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMDocument::saveXML()'))
547 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, '\DOMNode::item()'))
548 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()
549 6
                ->getPackagedName(true), $attribute->getSetterName())))
550 6
                ->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, 'bool $asString true: returns XML string, false: returns \DOMDocument'));
551 3
        }
552 198
        return $this;
553
    }
554
    /**
555
     * @param PhpAnnotationBlock $annotationBlock
556
     * @return Struct
557
     */
558 198
    protected function addStructPropertiesToAnnotationBlock(PhpAnnotationBlock $annotationBlock)
559
    {
560 198
        return $this->addStructPropertiesToAnnotationBlockUses($annotationBlock)->addStructPropertiesToAnnotationBlockParams($annotationBlock);
561
    }
562
    /**
563
     * @param PhpAnnotationBlock $annotationBlock
564
     * @return Struct
565
     */
566 198
    protected function addStructPropertiesToAnnotationBlockUses(PhpAnnotationBlock $annotationBlock)
567
    {
568 198
        foreach ($this->getModelAttributes() as $attribute) {
569 198
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $this->getModel()->getPackagedName(), $attribute->getSetterName())));
570 99
        }
571 198
        return $this;
572
    }
573
    /**
574
     * @param PhpAnnotationBlock $annotationBlock
575
     * @return Struct
576
     */
577 198
    protected function addStructPropertiesToAnnotationBlockParams(PhpAnnotationBlock $annotationBlock)
578
    {
579 198
        foreach ($this->getModelAttributes() as $attribute) {
580 198
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $%s', $this->getStructAttributeTypeSetAnnotation($attribute), lcfirst($attribute->getCleanName()))));
581 99
        }
582 198
        return $this;
583
    }
584
    /**
585
     * @param PhpMethod $method
586
     * @return PhpAnnotationBlock
587
     */
588 114
    protected function getStructMethodsAddToAnnotationBlock(PhpMethod $method)
589
    {
590 114
        $attributeName = str_replace('addTo', '', $method->getName());
591 114
        $attribute = $this->getModel()->getAttribute($attributeName);
592 114
        if (!$attribute instanceof StructAttributeModel) {
593 48
            $attribute = $this->getModel()->getAttribute(lcfirst($attributeName));
594 24
        }
595 114
        $model = $this->getRestrictionFromStructAttribute($attribute);
596 114
        $annotationBlock = new PhpAnnotationBlock();
597 114
        if ($attribute instanceof StructAttributeModel) {
598 114
            $annotationBlock->addChild(sprintf('Add item to %s value', $attribute->getCleanName()));
599 114
            if ($model instanceof StructModel) {
600 30
                $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_VALUE_IS_VALID)))->addChild(new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::%s()', $model->getPackagedName(true), StructEnum::METHOD_GET_VALID_VALUES)));
601 15
            }
602 114
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'))->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeTypeSetAnnotation($attribute, false))))->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getModel()->getPackagedName(true)));
603 57
        }
604 114
        return $annotationBlock;
605
    }
606
    /**
607
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::getModel()
608
     * @return StructModel
609
     */
610 318
    public function getModel()
611
    {
612 318
        return parent::getModel();
613
    }
614
    /**
615
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
616
     * @throws \InvalidArgumentException
617
     * @param AbstractModel $model
618
     * @return StructArray
619
     */
620 270
    public function setModel(AbstractModel $model)
621
    {
622 270
        if (!$model instanceof StructModel) {
623 6
            throw new \InvalidArgumentException('Model must be an instance of a Struct', __LINE__);
624
        }
625 264
        return parent::setModel($model);
626
    }
627
}
628