ClassGenerator::handleValueMethod()   C
last analyzed

Complexity

Conditions 12
Paths 36

Size

Total Lines 66

Duplication

Lines 12
Ratio 18.18 %

Code Coverage

Tests 44
CRAP Score 12.5238

Importance

Changes 0
Metric Value
dl 12
loc 66
ccs 44
cts 52
cp 0.8462
rs 6.3151
c 0
b 0
f 0
cc 12
nc 36
nop 4
crap 12.5238

How to fix   Long Method    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
namespace Goetas\Xsd\XsdToPhp\Php;
3
4
use Doctrine\Common\Inflector\Inflector;
5
use Goetas\Xsd\XsdToPhp\Php\Structure\PHPClass;
6
use Goetas\Xsd\XsdToPhp\Php\Structure\PHPClassOf;
7
use Goetas\Xsd\XsdToPhp\Php\Structure\PHPProperty;
8
use Zend\Code\Generator;
9
use Zend\Code\Generator\DocBlock\Tag\ParamTag;
10
use Zend\Code\Generator\DocBlock\Tag\PropertyTag;
11
use Zend\Code\Generator\DocBlock\Tag\ReturnTag;
12
use Zend\Code\Generator\DocBlockGenerator;
13
use Zend\Code\Generator\MethodGenerator;
14
use Zend\Code\Generator\ParameterGenerator;
15
use Zend\Code\Generator\PropertyGenerator;
16
17
class ClassGenerator
18
{
19
20 8
    private function handleBody(Generator\ClassGenerator $class, PHPClass $type)
21
    {
22 8
        foreach ($type->getProperties() as $prop) {
23 7
            if ($prop->getName() !== '__value') {
24 7
                $this->handleProperty($class, $prop);
25 7
            }
26 8
        }
27 8
        foreach ($type->getProperties() as $prop) {
28 7
            if ($prop->getName() !== '__value') {
29 7
                $this->handleMethod($class, $prop, $type);
30 7
            }
31 8
        }
32
33 8
        if (count($type->getProperties()) === 1 && $type->hasProperty('__value')) {
34
            return false;
35
        }
36
37 8
        return true;
38
    }
39
40 8
    private function isNativeType(PHPClass $class)
41
    {
42 8
        return !$class->getNamespace() && in_array($class->getName(), [
43 8
            'string',
44 8
            'int',
45 8
            'float',
46 8
            'integer',
47 8
            'boolean',
48 8
            'array',
49 8
            'mixed',
50
            'callable'
51 8
        ]);
52
    }
53
54 8
    private function getPhpType(PHPClass $class)
55
    {
56 8
        if (!$class->getNamespace()) {
57 8
            if ($this->isNativeType($class)) {
58 8
                return $class->getName();
59
            }
60
            return "\\" . $class->getName();
61
        }
62 1
        return "\\" . $class->getFullName();
63
    }
64
65 2
    private function handleValueMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class, $all = true)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
Unused Code introduced by
The parameter $all 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...
66
    {
67 2
        $type = $prop->getType();
68
69 2
        $docblock = new DocBlockGenerator('Construct');
70 2
        $paramTag = new ParamTag("value", "mixed");
71 2
        $paramTag->setTypes(($type ? $this->getPhpType($type) : "mixed"));
72
73 2
        $docblock->setTag($paramTag);
74
75 2
        $param = new ParameterGenerator("value");
76 2
        if ($type && !$this->isNativeType($type)) {
77
            $param->setType($this->getPhpType($type));
78
        }
79 2
        $method = new MethodGenerator("__construct", [
80
            $param
81 2
        ]);
82 2
        $method->setDocBlock($docblock);
83 2
        $method->setBody("\$this->value(\$value);");
84
85 2
        $generator->addMethodFromGenerator($method);
86
87 2
        $docblock = new DocBlockGenerator('Gets or sets the inner value');
88 2
        $paramTag = new ParamTag("value", "mixed");
89 2 View Code Duplication
        if ($type && $type instanceof PHPClassOf) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $paramTag->setTypes($this->getPhpType($type->getArg()
91
                    ->getType()) . "[]");
92 2
        } elseif ($type) {
93 2
            $paramTag->setTypes($this->getPhpType($prop->getType()));
94 2
        }
95 2
        $docblock->setTag($paramTag);
96
97 2
        $returnTag = new ReturnTag("mixed");
98
99 2 View Code Duplication
        if ($type && $type instanceof PHPClassOf) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
            $returnTag->setTypes($this->getPhpType($type->getArg()
101
                    ->getType()) . "[]");
102 2
        } elseif ($type) {
103 2
            $returnTag->setTypes($this->getPhpType($type));
104 2
        }
105 2
        $docblock->setTag($returnTag);
106
107 2
        $param = new ParameterGenerator("value");
108 2
        $param->setDefaultValue(null);
109
110 2
        if ($type && !$this->isNativeType($type)) {
111
            $param->setType($this->getPhpType($type));
112
        }
113 2
        $method = new MethodGenerator("value", []);
114 2
        $method->setDocBlock($docblock);
115
116 2
        $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL;
117 2
        $methodBody .= "    \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL;
118 2
        $methodBody .= "}" . PHP_EOL;
119 2
        $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL;
120 2
        $method->setBody($methodBody);
121
122 2
        $generator->addMethodFromGenerator($method);
123
124 2
        $docblock = new DocBlockGenerator('Gets a string value');
125 2
        $docblock->setTag(new ReturnTag("string"));
126 2
        $method = new MethodGenerator("__toString");
127 2
        $method->setDocBlock($docblock);
128 2
        $method->setBody("return strval(\$this->" . $prop->getName() . ");");
129 2
        $generator->addMethodFromGenerator($method);
130 2
    }
131
132 7
    private function handleSetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
133
    {
134 7
        $methodBody = '';
135 7
        $docblock = new DocBlockGenerator();
136
137 7
        $docblock->setShortDescription("Sets a new " . $prop->getName());
138
139 7
        if ($prop->getDoc()) {
140
            $docblock->setLongDescription($prop->getDoc());
141
        }
142
143 7
        $patramTag = new ParamTag($prop->getName());
144 7
        $docblock->setTag($patramTag);
145
146 7
        $return = new ReturnTag("self");
147 7
        $docblock->setTag($return);
148
149 7
        $type = $prop->getType();
150
151 7
        $method = new MethodGenerator("set" . Inflector::classify($prop->getName()));
152
153 7
        $parameter = new ParameterGenerator($prop->getName(), "mixed");
154
155 7
        if ($type && $type instanceof PHPClassOf) {
156 4
            $patramTag->setTypes($this->getPhpType($type->getArg()
157 4
                    ->getType()) . "[]");
158 4
            $parameter->setType("array");
159
160 4
            if ($p = $this->isOneType($type->getArg()
161 4
                ->getType())
162 4
            ) {
163
                if (($t = $p->getType())) {
164
                    $patramTag->setTypes($this->getPhpType($t));
165
                }
166
            }
167 7
        } elseif ($type) {
168 3
            if ($this->isNativeType($type)) {
169 3
                $patramTag->setTypes($this->getPhpType($type));
170 3
            } elseif ($p = $this->isOneType($type)) {
171
                if (($t = $p->getType()) && !$this->isNativeType($t)) {
172
                    $patramTag->setTypes($this->getPhpType($t));
173
                    $parameter->setType($this->getPhpType($t));
174
                } elseif ($t && !$this->isNativeType($t)) {
175
                    $patramTag->setTypes($this->getPhpType($t));
176
                    $parameter->setType($this->getPhpType($t));
177
                } elseif ($t) {
178
                    $patramTag->setTypes($this->getPhpType($t));
179
                }
180
            } else {
181
                $patramTag->setTypes($this->getPhpType($type));
182
                $parameter->setType($this->getPhpType($type));
183
            }
184 3
        }
185
186 7
        $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL;
187 7
        $methodBody .= "return \$this;";
188 7
        $method->setBody($methodBody);
189 7
        $method->setDocBlock($docblock);
190 7
        $method->setParameter($parameter);
191
192 7
        $generator->addMethodFromGenerator($method);
193 7
    }
194
195 7
    private function handleGetter(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
196
    {
197
198 7
        if ($prop->getType() instanceof PHPClassOf) {
199 4
            $docblock = new DocBlockGenerator();
200 4
            $docblock->setShortDescription("isset " . $prop->getName());
201 4
            if ($prop->getDoc()) {
202
                $docblock->setLongDescription($prop->getDoc());
203
            }
204
205 4
            $patramTag = new ParamTag("index", "scalar");
206 4
            $docblock->setTag($patramTag);
207
208 4
            $docblock->setTag(new ReturnTag("boolean"));
209
210 4
            $paramIndex = new ParameterGenerator("index", "mixed");
211
212 4
            $method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]);
213 4
            $method->setDocBlock($docblock);
214 4
            $method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);");
215 4
            $generator->addMethodFromGenerator($method);
216
217 4
            $docblock = new DocBlockGenerator();
218 4
            $docblock->setShortDescription("unset " . $prop->getName());
219 4
            if ($prop->getDoc()) {
220
                $docblock->setLongDescription($prop->getDoc());
221
            }
222
223 4
            $patramTag = new ParamTag("index", "scalar");
224 4
            $docblock->setTag($patramTag);
225 4
            $paramIndex = new ParameterGenerator("index", "mixed");
226
227 4
            $docblock->setTag(new ReturnTag("void"));
228
229
230 4
            $method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]);
231 4
            $method->setDocBlock($docblock);
232 4
            $method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);");
233 4
            $generator->addMethodFromGenerator($method);
234 4
        }
235
        // ////
236
237 7
        $docblock = new DocBlockGenerator();
238
239 7
        $docblock->setShortDescription("Gets as " . $prop->getName());
240
241 7
        if ($prop->getDoc()) {
242
            $docblock->setLongDescription($prop->getDoc());
243
        }
244
245 7
        $tag = new ReturnTag("mixed");
246 7
        $type = $prop->getType();
247 7
        if ($type && $type instanceof PHPClassOf) {
248 4
            $tt = $type->getArg()->getType();
249 4
            $tag->setTypes($this->getPhpType($tt) . "[]");
250 4 View Code Duplication
            if ($p = $this->isOneType($tt)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
                if (($t = $p->getType())) {
252
                    $tag->setTypes($this->getPhpType($t) . "[]");
253
                }
254
            }
255 7
        } elseif ($type) {
256
257 3
            if ($p = $this->isOneType($type)) {
258
                if ($t = $p->getType()) {
259
                    $tag->setTypes($this->getPhpType($t));
260
                }
261
            } else {
262 3
                $tag->setTypes($this->getPhpType($type));
263
            }
264 3
        }
265
266 7
        $docblock->setTag($tag);
267
268 7
        $method = new MethodGenerator("get" . Inflector::classify($prop->getName()));
269 7
        $method->setDocBlock($docblock);
270 7
        $method->setBody("return \$this->" . $prop->getName() . ";");
271
272 7
        $generator->addMethodFromGenerator($method);
273 7
    }
274
275 8
    private function isOneType(PHPClass $type, $onlyParent = false)
276
    {
277 8
        if ($onlyParent) {
278
            $e = $type->getExtends();
279
            if ($e) {
280
                if ($e->hasProperty('__value')) {
281
                    return $e->getProperty('__value');
282
                }
283
            }
284
        } else {
285 8
            if ($type->hasPropertyInHierarchy('__value') && count($type->getPropertiesInHierarchy()) === 1) {
286 2
                return $type->getPropertyInHierarchy("__value");
287
            }
288
        }
289 7
    }
290
291 4
    private function handleAdder(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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...
292
    {
293 4
        $type = $prop->getType();
294 4
        $propName = $type->getArg()->getName();
295
296 4
        $docblock = new DocBlockGenerator();
297 4
        $docblock->setShortDescription("Adds as $propName");
298
299 4
        if ($prop->getDoc()) {
300
            $docblock->setLongDescription($prop->getDoc());
301
        }
302
303 4
        $return = new ReturnTag();
304 4
        $return->setTypes("self");
305 4
        $docblock->setTag($return);
306
307 4
        $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg()
308 4
            ->getType()));
309 4
        $docblock->setTag($patramTag);
310
311 4
        $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName()));
312
313 4
        $parameter = new ParameterGenerator($propName);
314 4
        $tt = $type->getArg()->getType();
315
316 4
        if (!$this->isNativeType($tt)) {
317
318 1
            if ($p = $this->isOneType($tt)) {
319
                if (($t = $p->getType())) {
320
                    $patramTag->setTypes($this->getPhpType($t));
321
322
                    if (!$this->isNativeType($t)) {
323
                        $parameter->setType($this->getPhpType($t));
324
                    }
325
                }
326 1
            } elseif (!$this->isNativeType($tt)) {
327 1
                $parameter->setType($this->getPhpType($tt));
328 1
            }
329 1
        }
330
331 4
        $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL;
332 4
        $methodBody .= "return \$this;";
333 4
        $method->setBody($methodBody);
334 4
        $method->setDocBlock($docblock);
335 4
        $method->setParameter($parameter);
336
337 4
        $generator->addMethodFromGenerator($method);
338 4
    }
339
340 7
    private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class)
341
    {
342 7
        if ($prop->getType() instanceof PHPClassOf) {
343 4
            $this->handleAdder($generator, $prop, $class);
344 4
        }
345
346 7
        $this->handleGetter($generator, $prop, $class);
347 7
        $this->handleSetter($generator, $prop, $class);
348 7
    }
349
350 8
    private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop)
351
    {
352 8
        $generatedProp = new PropertyGenerator($prop->getName());
353 8
        $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE);
354
355 8
        $class->addPropertyFromGenerator($generatedProp);
356
357 8
        if ($prop->getType() && (!$prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) {
358
            // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE);
359 4
        }
360
361 8
        $docBlock = new DocBlockGenerator();
362 8
        $generatedProp->setDocBlock($docBlock);
363
364 8
        if ($prop->getDoc()) {
365
            $docBlock->setLongDescription($prop->getDoc());
366
        }
367 8
        $tag = new PropertyTag($prop->getName(), 'mixed');
368
369 8
        $type = $prop->getType();
370
371 8
        if ($type && $type instanceof PHPClassOf) {
372 4
            $tt = $type->getArg()->getType();
373 4
            $tag->setTypes($this->getPhpType($tt) . "[]");
374 4 View Code Duplication
            if ($p = $this->isOneType($tt)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
375
                if (($t = $p->getType())) {
376
                    $tag->setTypes($this->getPhpType($t) . "[]");
377
                }
378
            }
379 8
        } elseif ($type) {
380
381 4
            if ($this->isNativeType($type)) {
382 4
                $tag->setTypes($this->getPhpType($type));
383 4 View Code Duplication
            } elseif (($p = $this->isOneType($type)) && ($t = $p->getType())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
384
                $tag->setTypes($this->getPhpType($t));
385
            } else {
386
                $tag->setTypes($this->getPhpType($prop->getType()));
387
            }
388 4
        }
389 8
        $docBlock->setTag($tag);
390 8
    }
391
392 8
    public function generate(Generator\ClassGenerator $class, PHPClass $type)
393
    {
394 8
        $docblock = new DocBlockGenerator("Class representing " . $type->getName());
395 8
        if ($type->getDoc()) {
396 8
            $docblock->setLongDescription($type->getDoc());
397 8
        }
398 8
        $class->setNamespaceName($type->getNamespace() ?: NULL);
399 8
        $class->setName($type->getName());
400 8
        $class->setDocblock($docblock);
401
402 8
        if ($extends = $type->getExtends()) {
403
404 2
            if ($p = $this->isOneType($extends)) {
405 2
                $this->handleProperty($class, $p);
406 2
                $this->handleValueMethod($class, $p, $extends);
407 2
            } else {
408
409
                $class->setExtendedClass($extends->getName());
410
411
                if ($extends->getNamespace() != $type->getNamespace()) {
412
                    if ($extends->getName() == $type->getName()) {
413
                        $class->addUse($type->getExtends()
414
                            ->getFullName(), $extends->getName() . "Base");
415
                        $class->setExtendedClass($extends->getName() . "Base");
416
                    } else {
417
                        $class->addUse($extends->getFullName());
418
                    }
419
                }
420
            }
421 2
        }
422
423 8
        if ($this->handleBody($class, $type)) {
424 8
            return true;
425
        }
426
    }
427
}
428