Completed
Push — 1.x ( 257780...f31d1d )
by Mikaël
42:33 queued 33:38
created

StructArray::getStructMethodAnnotationBlock()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 36
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 34
cts 34
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 33
nc 10
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File;
4
5
use WsdlToPhp\PackageGenerator\Model\AbstractModel;
6
use WsdlToPhp\PackageGenerator\Model\Struct as StructModel;
7
use WsdlToPhp\PhpGenerator\Element\PhpAnnotation;
8
use WsdlToPhp\PhpGenerator\Element\PhpAnnotationBlock;
9
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
10
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
11
12
class StructArray extends Struct
13
{
14
    /**
15
     * @var string
16
     */
17
    const METHOD_GET_ATTRIBUTE_NAME = 'getAttributeName';
18
    /**
19
     * @var string
20
     */
21
    const METHOD_CURRENT = 'current';
22
    /**
23
     * @var string
24
     */
25
    const METHOD_ITEM = 'item';
26
    /**
27
     * @var string
28
     */
29
    const METHOD_FIRST = 'first';
30
    /**
31
     * @var string
32
     */
33
    const METHOD_LAST = 'last';
34
    /**
35
     * @var string
36
     */
37
    const METHOD_OFFSET_GET = 'offsetGet';
38
    /**
39
     * @var string
40
     */
41
    const METHOD_ADD = 'add';
42
    /**
43
     * @see \WsdlToPhp\PackageGenerator\File\Struct::addStructMethodsSetAndGet()
44
     */
45 28
    public function addStructMethodsSetAndGet(MethodContainer $methods)
46
    {
47 28
        parent::addStructMethodsSetAndGet($methods);
48 28
        $this
49 28
            ->addArrayMethodCurrent($methods)
50 28
            ->addArrayMethodItem($methods)
51 28
            ->addArrayMethodFirst($methods)
52 28
            ->addArrayMethodLast($methods)
53 28
            ->addArrayMethodOffsetGet($methods)
54 28
            ->addArrayMethodAdd($methods)
55 28
            ->addArrayMethodGetAttributeName($methods);
56 28
        return $this;
57
    }
58
    /**
59
     * @param MethodContainer $methods
60
     * @return StructArray
61
     */
62 28
    protected function addArrayMethodCurrent(MethodContainer $methods)
63
    {
64 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_CURRENT, $this->getArrayMethodBody(self::METHOD_CURRENT));
65
    }
66
    /**
67
     * @param MethodContainer $methods
68
     * @return StructArray
69
     */
70 28
    protected function addArrayMethodItem(MethodContainer $methods)
71
    {
72 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_ITEM, $this->getArrayMethodBody(self::METHOD_ITEM, '$index'), array(
73 28
            'index',
74 28
        ));
75
    }
76
    /**
77
     * @param MethodContainer $methods
78
     * @return StructArray
79
     */
80 28
    protected function addArrayMethodFirst(MethodContainer $methods)
81
    {
82 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_FIRST, $this->getArrayMethodBody(self::METHOD_FIRST));
83
    }
84
    /**
85
     * @param MethodContainer $methods
86
     * @return StructArray
87
     */
88 28
    protected function addArrayMethodLast(MethodContainer $methods)
89
    {
90 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_LAST, $this->getArrayMethodBody(self::METHOD_LAST));
91
    }
92
    /**
93
     * @param MethodContainer $methods
94
     * @return StructArray
95
     */
96 28
    protected function addArrayMethodOffsetGet(MethodContainer $methods)
97
    {
98 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_OFFSET_GET, $this->getArrayMethodBody(self::METHOD_OFFSET_GET, '$offset'), array(
99 28
            'offset',
100 28
        ));
101
    }
102
    /**
103
     * @param MethodContainer $methods
104
     * @return StructArray
105
     */
106 28
    protected function addArrayMethodGetAttributeName(MethodContainer $methods)
107
    {
108 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_GET_ATTRIBUTE_NAME, sprintf('return \'%s\';', $this->getModel()->getAttributes()->offsetGet(0)->getName()));
109
    }
110
    /**
111
     * @param MethodContainer $methods
112
     * @return StructArray
113
     */
114 28
    protected function addArrayMethodAdd(MethodContainer $methods)
115
    {
116 28
        if ($this->getRestrictionFromStructAttribute() instanceof StructModel) {
117 12
            $method = new PhpMethod(self::METHOD_ADD, array(
118 12
                'item',
119 12
            ));
120 12
            $this->addStructMethodSetBodyForRestriction($method, $this->getModel()->getAttributes()->offsetGet(0), 'item');
121 12
            $method->addChild('return parent::add($item);');
122 12
            $methods->add($method);
123 12
        }
124 28
        return $this;
125
    }
126
    /**
127
     * @param MethodContainer $methods
128
     * @param string $name
129
     * @param string $body
130
     * @param string[] $methodParameters
131
     * @return StructArray
132
     */
133 28
    protected function addArrayMethodGenericMethod(MethodContainer $methods, $name, $body, array $methodParameters = array())
134
    {
135 28
        $method = new PhpMethod($name, $methodParameters);
136 28
        $method->addChild($body);
137 28
        $methods->add($method);
138 28
        return $this;
139
    }
140
    /**
141
     * @return PhpAnnotationBlock
142
     */
143 28
    protected function getArrayMethodGetAttributeNameAnnotationBlock()
144
    {
145 28
        return new PhpAnnotationBlock(array(
146 28
            'Returns the attribute name',
147 28
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::%s()', $this->getModel()->getExtends(true), self::METHOD_GET_ATTRIBUTE_NAME)),
148 28
            new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('string %s', $this->getModel()->getAttributes()->offsetGet(0)->getName())),
149 28
        ));
150
    }
151
    /**
152
     * @return PhpAnnotationBlock
153
     */
154 28
    protected function getArrayMethodCurrentAnnotationBlock()
155
    {
156 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_CURRENT, 'Returns the current element');
157
    }
158
    /**
159
     * @return PhpAnnotationBlock
160
     */
161 28
    protected function getArrayMethodFirstAnnotationBlock()
162
    {
163 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_FIRST, 'Returns the first element');
164
    }
165
    /**
166
     * @return PhpAnnotationBlock
167
     */
168 28
    protected function getArrayMethodLastAnnotationBlock()
169
    {
170 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_LAST, 'Returns the last element');
171
    }
172
    /**
173
     * @return PhpAnnotationBlock
174
     */
175 28
    protected function getArrayMethodItemAnnotationBlock()
176
    {
177 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_ITEM, 'Returns the indexed element', 'int $index');
178
    }
179
    /**
180
     * @return PhpAnnotationBlock
181
     */
182 28
    protected function getArrayMethodOffsetGetAnnotationBlock()
183
    {
184 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_OFFSET_GET, 'Returns the element at the offset', 'int $offset');
185
    }
186
    /**
187
     * @return PhpAnnotationBlock
188
     */
189 12
    protected function getArrayMethodAddAnnotationBlock()
190
    {
191 12
        return new PhpAnnotationBlock(array(
192 12
            'Add element to array',
193 12
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::add()', $this->getModel()->getExtends(true))),
194 12
            new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'),
195 12
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::valueIsValid()', $this->getModelFromStructAttribute()->getPackagedName(true))),
196 12
            new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeType())),
197 12
            new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('%s', $this->getModel()->getPackagedName(true))),
198 12
        ));
199
    }
200
    /**
201
     * @param string $name
202
     * @param string $description
203
     * @param string $param
204
     * @return PhpAnnotationBlock
205
     */
206 28
    protected function getArrayMethodGenericAnnotationBlock($name, $description, $param = null)
207
    {
208 28
        $annotationBlock = new PhpAnnotationBlock(array(
209 28
            $description,
210 28
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::%s()', $this->getModel()->getExtends(true), $name)),
211 28
        ));
212 28
        if (!empty($param)) {
213 28
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, $param));
214 28
        }
215 28
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getStructAttributeTypeGetAnnotation(null, false)));
216 28
        return $annotationBlock;
217
    }
218
    /**
219
     * @param PhpMethod $method
220
     * @return PhpAnnotationBlock|null
221
     */
222 28
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
223
    {
224 28
        switch ($method->getName()) {
225 28
            case self::METHOD_CONSTRUCT:
226 28
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
227 28
                break;
228 28
            case self::METHOD_SET_STATE:
229 28
                $annotationBlock = $this->getStructMethodSetStateAnnotationBlock();
230 28
                break;
231 28
            case self::METHOD_GET_ATTRIBUTE_NAME:
232 28
                $annotationBlock = $this->getArrayMethodGetAttributeNameAnnotationBlock();
233 28
                break;
234 28
            case self::METHOD_CURRENT:
235 28
                $annotationBlock = $this->getArrayMethodCurrentAnnotationBlock();
236 28
                break;
237 28
            case self::METHOD_FIRST:
238 28
                $annotationBlock = $this->getArrayMethodFirstAnnotationBlock();
239 28
                break;
240 28
            case self::METHOD_ITEM:
241 28
                $annotationBlock = $this->getArrayMethodItemAnnotationBlock();
242 28
                break;
243 28
            case self::METHOD_LAST:
244 28
                $annotationBlock = $this->getArrayMethodLastAnnotationBlock();
245 28
                break;
246 28
            case self::METHOD_OFFSET_GET:
247 28
                $annotationBlock = $this->getArrayMethodOffsetGetAnnotationBlock();
248 28
                break;
249 28
            case self::METHOD_ADD:
250 12
                $annotationBlock = $this->getArrayMethodAddAnnotationBlock();
251 12
                break;
252 28
            default:
253 28
                $annotationBlock = parent::getStructMethodAnnotationBlock($method);
254 28
                break;
255 28
        }
256 28
        return $annotationBlock;
257
    }
258
    /**
259
     * @param string $method
260
     * @param string $var
261
     * @return string
262
     */
263 28
    protected function getArrayMethodBody($method, $var = '')
264
    {
265 28
        return sprintf('return parent::%s(%s);', $method, $var);
266
    }
267
    /**
268
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
269
     * @throws \InvalidaArgumentException
270
     * @param AbstractModel $model
271
     * @return StructArray
272
     */
273 40
    public function setModel(AbstractModel $model)
274
    {
275 40
        if ($model instanceof StructModel && !$model->isArray()) {
276 8
            throw new \InvalidArgumentException('The model is not a valid array struct (name must contain Array and the model must contain only one property', __LINE__);
277
        }
278 32
        return parent::setModel($model);
279
    }
280
}
281