Completed
Pull Request — master (#121)
by
unknown
11:29
created

ClassGenerator::handleConstant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 2
crap 2
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\PhpConstant;
8
use Goetas\Xsd\XsdToPhp\Php\Structure\PHPProperty;
9
use Zend\Code\Generator;
10
use Zend\Code\Generator\DocBlock\Tag\ParamTag;
11
use Zend\Code\Generator\DocBlock\Tag\PropertyTag;
12
use Zend\Code\Generator\DocBlock\Tag\ReturnTag;
13
use Zend\Code\Generator\DocBlockGenerator;
14
use Zend\Code\Generator\MethodGenerator;
15
use Zend\Code\Generator\ParameterGenerator;
16
use Zend\Code\Generator\PropertyGenerator;
17
18
class ClassGenerator
19
{
20
21 8
    private function handleBody(Generator\ClassGenerator $class, PHPClass $type)
22
    {
23 8
        foreach ($type->getConstants() as $const) {
24
            $this->handleConstant($class, $const);
25 8
        }
26
27 8
        foreach ($type->getProperties() as $prop) {
28 7
            if ($prop->getName() !== '__value') {
29 7
                $this->handleProperty($class, $prop);
30 7
            }
31 8
        }
32 8
        foreach ($type->getProperties() as $prop) {
33 7
            if ($prop->getName() !== '__value') {
34 7
                $this->handleMethod($class, $prop, $type);
35 7
            }
36 8
        }
37
38 8
        if (count($type->getProperties()) === 1 && $type->hasProperty('__value')) {
39
            if (!count($type->getConstants())) {
40
                return false;
41
            }
42
            $class->removeMethod('__construct')
43
                ->removeMethod('__toString')
44
                ->removeMethod('value');
45
        }
46
47 8
        return true;
48
    }
49
50 8
    private function isNativeType(PHPClass $class)
51
    {
52 8
        return !$class->getNamespace() && in_array($class->getName(), [
53 8
            'string',
54 8
            'int',
55 8
            'float',
56 8
            'integer',
57 8
            'boolean',
58 8
            'array',
59 8
            'mixed',
60
            'callable'
61 8
        ]);
62
    }
63
64 8
    private function getPhpType(PHPClass $class)
65
    {
66 8
        if (!$class->getNamespace()) {
67 8
            if ($this->isNativeType($class)) {
68 8
                return $class->getName();
69
            }
70
            return "\\" . $class->getName();
71
        }
72 1
        return "\\" . $class->getFullName();
73
    }
74
75 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...
76
    {
77 2
        $type = $prop->getType();
78
79 2
        $docblock = new DocBlockGenerator('Construct');
80 2
        $paramTag = new ParamTag("value", "mixed");
81 2
        $paramTag->setTypes(($type ? $this->getPhpType($type) : "mixed"));
82
83 2
        $docblock->setTag($paramTag);
84
85 2
        $param = new ParameterGenerator("value");
86 2
        if ($type && !$this->isNativeType($type)) {
87
            $param->setType($this->getPhpType($type));
88
        }
89 2
        $method = new MethodGenerator("__construct", [
90
            $param
91 2
        ]);
92 2
        $method->setDocBlock($docblock);
93 2
        $method->setBody("\$this->value(\$value);");
94
95 2
        $generator->addMethodFromGenerator($method);
96
97 2
        $docblock = new DocBlockGenerator('Gets or sets the inner value');
98 2
        $paramTag = new ParamTag("value", "mixed");
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
            $paramTag->setTypes($this->getPhpType($type->getArg()
101
                    ->getType()) . "[]");
102 2
        } elseif ($type) {
103 2
            $paramTag->setTypes($this->getPhpType($prop->getType()));
104 2
        }
105 2
        $docblock->setTag($paramTag);
106
107 2
        $returnTag = new ReturnTag("mixed");
108
109 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...
110
            $returnTag->setTypes($this->getPhpType($type->getArg()
111
                    ->getType()) . "[]");
112 2
        } elseif ($type) {
113 2
            $returnTag->setTypes($this->getPhpType($type));
114 2
        }
115 2
        $docblock->setTag($returnTag);
116
117 2
        $param = new ParameterGenerator("value");
118 2
        $param->setDefaultValue(null);
119
120 2
        if ($type && !$this->isNativeType($type)) {
121
            $param->setType($this->getPhpType($type));
122
        }
123 2
        $method = new MethodGenerator("value", []);
124 2
        $method->setDocBlock($docblock);
125
126 2
        $methodBody = "if (\$args = func_get_args()) {" . PHP_EOL;
127 2
        $methodBody .= "    \$this->" . $prop->getName() . " = \$args[0];" . PHP_EOL;
128 2
        $methodBody .= "}" . PHP_EOL;
129 2
        $methodBody .= "return \$this->" . $prop->getName() . ";" . PHP_EOL;
130 2
        $method->setBody($methodBody);
131
132 2
        $generator->addMethodFromGenerator($method);
133
134 2
        $docblock = new DocBlockGenerator('Gets a string value');
135 2
        $docblock->setTag(new ReturnTag("string"));
136 2
        $method = new MethodGenerator("__toString");
137 2
        $method->setDocBlock($docblock);
138 2
        $method->setBody("return strval(\$this->" . $prop->getName() . ");");
139 2
        $generator->addMethodFromGenerator($method);
140 2
    }
141
142 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...
143
    {
144 7
        $methodBody = '';
145 7
        $docblock = new DocBlockGenerator();
146
147 7
        $docblock->setShortDescription("Sets a new " . $prop->getName());
148
149 7
        if ($prop->getDoc()) {
150
            $docblock->setLongDescription($prop->getDoc());
151
        }
152
153 7
        $patramTag = new ParamTag($prop->getName());
154 7
        $docblock->setTag($patramTag);
155
156 7
        $return = new ReturnTag("self");
157 7
        $docblock->setTag($return);
158
159 7
        $type = $prop->getType();
160
161 7
        $method = new MethodGenerator("set" . Inflector::classify($prop->getName()));
162
163 7
        $parameter = new ParameterGenerator($prop->getName(), "mixed", new Generator\ValueGenerator(null, Generator\ValueGenerator::TYPE_NULL));
164
165 7
        if ($type && $type instanceof PHPClassOf) {
166 4
            $patramTag->setTypes($this->getPhpType($type->getArg()
167 4
                    ->getType()) . "[]");
168 4
            $parameter->setType("array");
169
170 4
            if ($p = $this->isOneType($type->getArg()
171 4
                ->getType())
172 4
            ) {
173
                if (($t = $p->getType())) {
174
                    $patramTag->setTypes($this->getPhpType($t));
175
                }
176
            }
177 7
        } elseif ($type) {
178 3
            if ($this->isNativeType($type)) {
179 3
                $patramTag->setTypes($this->getPhpType($type));
180 3
            } elseif ($p = $this->isOneType($type)) {
181
                if (($t = $p->getType()) && !$this->isNativeType($t)) {
182
                    $patramTag->setTypes($this->getPhpType($t));
183
                    $parameter->setType($this->getPhpType($t));
184
                } elseif ($t && !$this->isNativeType($t)) {
185
                    $patramTag->setTypes($this->getPhpType($t));
186
                    $parameter->setType($this->getPhpType($t));
187
                } elseif ($t) {
188
                    $patramTag->setTypes($this->getPhpType($t));
189
                }
190
            } else {
191
                $patramTag->setTypes($this->getPhpType($type));
192
                $parameter->setType($this->getPhpType($type));
193
            }
194 3
        }
195
196 7
        $methodBody .= "\$this->" . $prop->getName() . " = \$" . $prop->getName() . ";" . PHP_EOL;
197 7
        $methodBody .= "return \$this;";
198 7
        $method->setBody($methodBody);
199 7
        $method->setDocBlock($docblock);
200 7
        $method->setParameter($parameter);
201
202 7
        $generator->addMethodFromGenerator($method);
203 7
    }
204
205 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...
206
    {
207
208 7
        if ($prop->getType() instanceof PHPClassOf) {
209 4
            $docblock = new DocBlockGenerator();
210 4
            $docblock->setShortDescription("isset " . $prop->getName());
211 4
            if ($prop->getDoc()) {
212
                $docblock->setLongDescription($prop->getDoc());
213
            }
214
215 4
            $patramTag = new ParamTag("index", "scalar");
216 4
            $docblock->setTag($patramTag);
217
218 4
            $docblock->setTag(new ReturnTag("boolean"));
219
220 4
            $paramIndex = new ParameterGenerator("index", "mixed");
221
222 4
            $method = new MethodGenerator("isset" . Inflector::classify($prop->getName()), [$paramIndex]);
223 4
            $method->setDocBlock($docblock);
224 4
            $method->setBody("return isset(\$this->" . $prop->getName() . "[\$index]);");
225 4
            $generator->addMethodFromGenerator($method);
226
227 4
            $docblock = new DocBlockGenerator();
228 4
            $docblock->setShortDescription("unset " . $prop->getName());
229 4
            if ($prop->getDoc()) {
230
                $docblock->setLongDescription($prop->getDoc());
231
            }
232
233 4
            $patramTag = new ParamTag("index", "scalar");
234 4
            $docblock->setTag($patramTag);
235 4
            $paramIndex = new ParameterGenerator("index", "mixed");
236
237 4
            $docblock->setTag(new ReturnTag("void"));
238
239
240 4
            $method = new MethodGenerator("unset" . Inflector::classify($prop->getName()), [$paramIndex]);
241 4
            $method->setDocBlock($docblock);
242 4
            $method->setBody("unset(\$this->" . $prop->getName() . "[\$index]);");
243 4
            $generator->addMethodFromGenerator($method);
244 4
        }
245
        // ////
246
247 7
        $docblock = new DocBlockGenerator();
248
249 7
        $docblock->setShortDescription("Gets as " . $prop->getName());
250
251 7
        if ($prop->getDoc()) {
252
            $docblock->setLongDescription($prop->getDoc());
253
        }
254
255 7
        $tag = new ReturnTag("mixed");
256 7
        $type = $prop->getType();
257 7
        if ($type && $type instanceof PHPClassOf) {
258 4
            $tt = $type->getArg()->getType();
259 4
            $tag->setTypes($this->getPhpType($tt) . "[]");
260 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...
261
                if (($t = $p->getType())) {
262
                    $tag->setTypes($this->getPhpType($t) . "[]");
263
                }
264
            }
265 7
        } elseif ($type) {
266
267 3
            if ($p = $this->isOneType($type)) {
268
                if ($t = $p->getType()) {
269
                    $tag->setTypes($this->getPhpType($t));
270
                }
271
            } else {
272 3
                $tag->setTypes($this->getPhpType($type));
273
            }
274 3
        }
275
276 7
        $docblock->setTag($tag);
277
278 7
        $method = new MethodGenerator("get" . Inflector::classify($prop->getName()));
279 7
        $method->setDocBlock($docblock);
280 7
        $method->setBody("return \$this->" . $prop->getName() . ";");
281
282 7
        $generator->addMethodFromGenerator($method);
283 7
    }
284
285 8
    private function isOneType(PHPClass $type, $onlyParent = false)
286
    {
287 8
        if ($onlyParent) {
288
            $e = $type->getExtends();
289
            if ($e) {
290
                if ($e->hasProperty('__value')) {
291
                    return $e->getProperty('__value');
292
                }
293
            }
294
        } else {
295 8
            if ($type->hasPropertyInHierarchy('__value') && count($type->getPropertiesInHierarchy()) === 1) {
296 2
                return $type->getPropertyInHierarchy("__value");
297
            }
298
        }
299 7
    }
300
301 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...
302
    {
303 4
        $type = $prop->getType();
304 4
        $propName = $type->getArg()->getName();
305
306 4
        $docblock = new DocBlockGenerator();
307 4
        $docblock->setShortDescription("Adds as $propName");
308
309 4
        if ($prop->getDoc()) {
310
            $docblock->setLongDescription($prop->getDoc());
311
        }
312
313 4
        $return = new ReturnTag();
314 4
        $return->setTypes("self");
315 4
        $docblock->setTag($return);
316
317 4
        $patramTag = new ParamTag($propName, $this->getPhpType($type->getArg()
318 4
            ->getType()));
319 4
        $docblock->setTag($patramTag);
320
321 4
        $method = new MethodGenerator("addTo" . Inflector::classify($prop->getName()));
322
323 4
        $parameter = new ParameterGenerator($propName);
324 4
        $tt = $type->getArg()->getType();
325
326 4
        if (!$this->isNativeType($tt)) {
327
328 1
            if ($p = $this->isOneType($tt)) {
329
                if (($t = $p->getType())) {
330
                    $patramTag->setTypes($this->getPhpType($t));
331
332
                    if (!$this->isNativeType($t)) {
333
                        $parameter->setType($this->getPhpType($t));
334
                    }
335
                }
336 1
            } elseif (!$this->isNativeType($tt)) {
337 1
                $parameter->setType($this->getPhpType($tt));
338 1
            }
339 1
        }
340
341 4
        $methodBody = "\$this->" . $prop->getName() . "[] = \$" . $propName . ";" . PHP_EOL;
342 4
        $methodBody .= "return \$this;";
343 4
        $method->setBody($methodBody);
344 4
        $method->setDocBlock($docblock);
345 4
        $method->setParameter($parameter);
346
347 4
        $generator->addMethodFromGenerator($method);
348 4
    }
349
350 7
    private function handleMethod(Generator\ClassGenerator $generator, PHPProperty $prop, PHPClass $class)
351
    {
352 7
        if ($prop->getType() instanceof PHPClassOf) {
353 4
            $this->handleAdder($generator, $prop, $class);
354 4
        }
355
356 7
        $this->handleGetter($generator, $prop, $class);
357 7
        $this->handleSetter($generator, $prop, $class);
358 7
    }
359
360 8
    private function handleProperty(Generator\ClassGenerator $class, PHPProperty $prop)
361
    {
362 8
        $generatedProp = new PropertyGenerator($prop->getName());
363 8
        $generatedProp->setVisibility(PropertyGenerator::VISIBILITY_PRIVATE);
364
365 8
        $class->addPropertyFromGenerator($generatedProp);
366
367 8
        if ($prop->getType() && (!$prop->getType()->getNamespace() && $prop->getType()->getName() == "array")) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
368
            // $generatedProp->setDefaultValue(array(), PropertyValueGenerator::TYPE_AUTO, PropertyValueGenerator::OUTPUT_SINGLE_LINE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
369 4
        }
370
371 8
        $docBlock = new DocBlockGenerator();
372 8
        $generatedProp->setDocBlock($docBlock);
373
374 8
        if ($prop->getDoc()) {
375
            $docBlock->setLongDescription($prop->getDoc());
376
        }
377 8
        $tag = new PropertyTag($prop->getName(), 'mixed');
378
379 8
        $type = $prop->getType();
380
381 8
        if ($type && $type instanceof PHPClassOf) {
382 4
            $tt = $type->getArg()->getType();
383 4
            $tag->setTypes($this->getPhpType($tt) . "[]");
384 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...
385
                if (($t = $p->getType())) {
386
                    $tag->setTypes($this->getPhpType($t) . "[]");
387
                }
388
            }
389 8
        } elseif ($type) {
390
391 4
            if ($this->isNativeType($type)) {
392 4
                $tag->setTypes($this->getPhpType($type));
393 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...
394
                $tag->setTypes($this->getPhpType($t));
395
            } else {
396
                $tag->setTypes($this->getPhpType($prop->getType()));
397
            }
398 4
        }
399 8
        $docBlock->setTag($tag);
400 8
    }
401
402
    private function handleConstant(Generator\ClassGenerator $class, PhpConstant $const)
403
    {
404
        $class->addConstant($const->getName(), $const->getValue());
405
    }
406
407 8
    public function generate(Generator\ClassGenerator $class, PHPClass $type)
408
    {
409 8
        $docblock = new DocBlockGenerator("Class representing " . $type->getName());
410 8
        if ($type->getDoc()) {
411 8
            $docblock->setLongDescription($type->getDoc());
412 8
        }
413 8
        $class->setNamespaceName($type->getNamespace());
414 8
        $class->setName($type->getName());
415 8
        $class->setDocblock($docblock);
416
417 8
        if ($extends = $type->getExtends()) {
418
419 2
            if ($p = $this->isOneType($extends)) {
420 2
                $this->handleProperty($class, $p);
421 2
                $this->handleValueMethod($class, $p, $extends);
422 2
            } else {
423
424
                $class->setExtendedClass($extends->getName());
425
426
                if ($extends->getNamespace() != $type->getNamespace()) {
427
                    if ($extends->getName() == $type->getName()) {
428
                        $class->addUse($type->getExtends()
429
                            ->getFullName(), $extends->getName() . "Base");
430
                        $class->setExtendedClass($extends->getName() . "Base");
431
                    } else {
432
                        $class->addUse($extends->getFullName());
433
                    }
434
                }
435
            }
436 2
        }
437
438 8
        if ($this->handleBody($class, $type)) {
439 8
            return true;
440
        }
441
    }
442
}
443