Completed
Push — develop ( d22805...7fc434 )
by Mikaël
35:39
created

StructArray   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 33
c 3
b 0
f 2
lcom 1
cbo 7
dl 0
loc 269
ccs 83
cts 83
cp 1
rs 9.3999

20 Methods

Rating   Name   Duplication   Size   Complexity  
A addArrayMethodFirst() 0 4 1
A addStructMethodsSetAndGet() 0 13 1
A addArrayMethodCurrent() 0 4 1
A addArrayMethodItem() 0 6 1
A addArrayMethodLast() 0 4 1
A addArrayMethodOffsetGet() 0 6 1
A addArrayMethodGetAttributeName() 0 4 1
A addArrayMethodAdd() 0 12 2
A addArrayMethodGenericMethod() 0 7 1
A getArrayMethodGetAttributeNameAnnotationBlock() 0 8 1
A getArrayMethodCurrentAnnotationBlock() 0 4 1
A getArrayMethodFirstAnnotationBlock() 0 4 1
A getArrayMethodLastAnnotationBlock() 0 4 1
A getArrayMethodItemAnnotationBlock() 0 4 1
A getArrayMethodOffsetGetAnnotationBlock() 0 4 1
A getArrayMethodAddAnnotationBlock() 0 11 1
A getArrayMethodGenericAnnotationBlock() 0 12 2
D getStructMethodAnnotationBlock() 0 36 10
A getArrayMethodBody() 0 4 1
A setModel() 0 7 3
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
    public function addStructMethodsSetAndGet(MethodContainer $methods)
46
    {
47
        parent::addStructMethodsSetAndGet($methods);
48
        $this
49
            ->addArrayMethodCurrent($methods)
50
            ->addArrayMethodItem($methods)
51
            ->addArrayMethodFirst($methods)
52
            ->addArrayMethodLast($methods)
53
            ->addArrayMethodOffsetGet($methods)
54
            ->addArrayMethodAdd($methods)
55 28
            ->addArrayMethodGetAttributeName($methods);
56
        return $this;
57 28
    }
58
    /**
59
     * @param MethodContainer $methods
60
     * @return StructArray
61
     */
62
    protected function addArrayMethodCurrent(MethodContainer $methods)
63
    {
64 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_CURRENT, $this->getArrayMethodBody(self::METHOD_CURRENT));
65
    }
66 28
    /**
67
     * @param MethodContainer $methods
68
     * @return StructArray
69
     */
70
    protected function addArrayMethodItem(MethodContainer $methods)
71
    {
72
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_ITEM, $this->getArrayMethodBody(self::METHOD_ITEM, '$index'), array(
73
            'index',
74 28
        ));
75
    }
76 28
    /**
77 28
     * @param MethodContainer $methods
78 21
     * @return StructArray
79 28
     */
80 28
    protected function addArrayMethodFirst(MethodContainer $methods)
81 28
    {
82
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_FIRST, $this->getArrayMethodBody(self::METHOD_FIRST));
83
    }
84
    /**
85
     * @param MethodContainer $methods
86
     * @return StructArray
87
     */
88
    protected function addArrayMethodLast(MethodContainer $methods)
89
    {
90 28
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_LAST, $this->getArrayMethodBody(self::METHOD_LAST));
91
    }
92 28
    /**
93 28
     * @param MethodContainer $methods
94
     * @return StructArray
95 28
     */
96
    protected function addArrayMethodOffsetGet(MethodContainer $methods)
97
    {
98
        return $this->addArrayMethodGenericMethod($methods, self::METHOD_OFFSET_GET, $this->getArrayMethodBody(self::METHOD_OFFSET_GET, '$offset'), array(
99
            'offset',
100
        ));
101
    }
102
    /**
103 28
     * @param MethodContainer $methods
104
     * @return StructArray
105 28
     */
106
    protected function addArrayMethodGetAttributeName(MethodContainer $methods)
107
    {
108
        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 (($model = $this->getRestrictionFromStructAttribute()) instanceof StructModel) {
117
            $method = new PhpMethod(self::METHOD_ADD, array(
118
                'item',
119
            ));
120
            $this->addStructMethodSetBodyForRestriction($method, $this->getModel()->getAttributes()->offsetGet(0), 'item');
121
            $method->addChild('return parent::add($item);');
122
            $methods->add($method);
123
        }
124 28
        return $this;
125
    }
126 28
    /**
127
     * @param MethodContainer $methods
128
     * @param string $name
129
     * @param string $body
130
     * @param string[] $methodParameters
131 28
     * @return StructArray
132
     */
133 28
    protected function addArrayMethodGenericMethod(MethodContainer $methods, $name, $body, array $methodParameters = array())
134 21
    {
135 28
        $method = new PhpMethod($name, $methodParameters);
136 28
        $method->addChild($body);
137 28
        $methods->add($method);
138 28
        return $this;
139 28
    }
140 28
    /**
141 28
     * @return PhpAnnotationBlock
142 28
     */
143
    protected function getArrayMethodGetAttributeNameAnnotationBlock()
144
    {
145
        return new PhpAnnotationBlock(array(
146
            'Returns the attribute name',
147
            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
        ));
150 28
    }
151
    /**
152
     * @return PhpAnnotationBlock
153
     */
154
    protected function getArrayMethodCurrentAnnotationBlock()
155
    {
156 28
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_CURRENT, 'Returns the current element');
157
    }
158 28
    /**
159 28
     * @return PhpAnnotationBlock
160 21
     */
161
    protected function getArrayMethodFirstAnnotationBlock()
162
    {
163
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_FIRST, 'Returns the first element');
164
    }
165
    /**
166 28
     * @return PhpAnnotationBlock
167
     */
168 28
    protected function getArrayMethodLastAnnotationBlock()
169
    {
170
        return $this->getArrayMethodGenericAnnotationBlock(self::METHOD_LAST, 'Returns the last element');
171
    }
172
    /**
173
     * @return PhpAnnotationBlock
174 28
     */
175
    protected function getArrayMethodItemAnnotationBlock()
176 28
    {
177
        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 28
    }
186 21
    /**
187
     * @return PhpAnnotationBlock
188
     */
189
    protected function getArrayMethodAddAnnotationBlock()
190
    {
191
        return new PhpAnnotationBlock(array(
192 28
            'Add element to array',
193
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::add()', $this->getModel()->getExtends(true))),
194 28
            new PhpAnnotation(self::ANNOTATION_THROWS, '\InvalidArgumentException'),
195
            new PhpAnnotation(self::ANNOTATION_USES, sprintf('%s::valueIsValid()', $this->getModelFromStructAttribute()->getPackagedName(true))),
196
            new PhpAnnotation(self::ANNOTATION_PARAM, sprintf('%s $item', $this->getStructAttributeType())),
197
            new PhpAnnotation(self::ANNOTATION_RETURN, sprintf('%s', $this->getModel()->getPackagedName(true))),
198
        ));
199
    }
200 28
    /**
201
     * @param string $name
202 28
     * @param string $description
203 12
     * @param string $param
204 12
     * @return PhpAnnotationBlock
205 9
     */
206
    protected function getArrayMethodGenericAnnotationBlock($name, $description, $param = null)
207 24
    {
208
        $annotationBlock = new PhpAnnotationBlock(array(
209
            $description,
210
            new PhpAnnotation(self::ANNOTATION_SEE, sprintf('%s::%s()', $this->getModel()->getExtends(true), $name)),
211
        ));
212
        if (!empty($param)) {
213
            $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_PARAM, $param));
214
        }
215
        $annotationBlock->addChild(new PhpAnnotation(self::ANNOTATION_RETURN, $this->getStructAttributeTypeGetAnnotation(null, false)));
216 28
        return $annotationBlock;
217
    }
218 28
    /**
219 28
     * @param PhpMethod $method
220 28
     * @return PhpAnnotationBlock|null
221 28
     */
222
    protected function getStructMethodAnnotationBlock(PhpMethod $method)
223
    {
224
        switch ($method->getName()) {
225
            case self::METHOD_CONSTRUCT:
226 28
                $annotationBlock = $this->getStructMethodConstructAnnotationBlock();
227
                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 21
                $annotationBlock = $this->getArrayMethodGetAttributeNameAnnotationBlock();
233
                break;
234
            case self::METHOD_CURRENT:
235
                $annotationBlock = $this->getArrayMethodCurrentAnnotationBlock();
236
                break;
237 28
            case self::METHOD_FIRST:
238
                $annotationBlock = $this->getArrayMethodFirstAnnotationBlock();
239 28
                break;
240
            case self::METHOD_ITEM:
241
                $annotationBlock = $this->getArrayMethodItemAnnotationBlock();
242
                break;
243
            case self::METHOD_LAST:
244 28
                $annotationBlock = $this->getArrayMethodLastAnnotationBlock();
245
                break;
246 28
            case self::METHOD_OFFSET_GET:
247
                $annotationBlock = $this->getArrayMethodOffsetGetAnnotationBlock();
248
                break;
249
            case self::METHOD_ADD:
250
                $annotationBlock = $this->getArrayMethodAddAnnotationBlock();
251 28
                break;
252
            default:
253 28
                $annotationBlock = parent::getStructMethodAnnotationBlock($method);
254
                break;
255
        }
256
        return $annotationBlock;
257
    }
258 28
    /**
259
     * @param string $method
260 28
     * @param string $var
261
     * @return string
262
     */
263
    protected function getArrayMethodBody($method, $var = '')
264
    {
265 28
        return sprintf('return parent::%s(%s);', $method, $var);
266
    }
267 28
    /**
268
     * @see \WsdlToPhp\PackageGenerator\File\AbstractModelFile::setModel()
269
     * @throws \InvalidaArgumentException
270
     * @param AbstractModel $model
271
     * @return StructArray
272 12
     */
273
    public function setModel(AbstractModel $model)
274 12
    {
275 12
        if ($model instanceof StructModel && !$model->isArray()) {
276 12
            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 12
        }
278 12
        return parent::setModel($model);
279 12
    }
280
}
281