Trait_::doCreate()   B
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
nc 2
nop 3
dl 0
loc 31
rs 8.1795
c 0
b 0
f 0
ccs 21
cts 21
cp 1
crap 8
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php\Factory;
16
17
use phpDocumentor\Reflection\Fqsen;
18
use phpDocumentor\Reflection\Location;
19
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
20
use phpDocumentor\Reflection\Php\StrategyContainer;
21
use phpDocumentor\Reflection\Php\Trait_ as TraitElement;
22
use phpDocumentor\Reflection\Types\Context;
23
use PhpParser\Node\Stmt\ClassMethod;
24
use PhpParser\Node\Stmt\Property as PropertyNode;
25
use PhpParser\Node\Stmt\Trait_ as TraitNode;
26
use PhpParser\Node\Stmt\TraitUse;
27
28
// @codingStandardsIgnoreStart
29
final class Trait_ extends AbstractFactory implements ProjectFactoryStrategy
30
// @codingStandardsIgnoreEnd
31
{
32 1
    public function matches($object): bool
33
    {
34 1
        return $object instanceof TraitNode;
35
    }
36
37
    /**
38
     * Creates an TraitElement out of the given object.
39
     *
40
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
41
     * used to create nested Elements.
42
     *
43
     * @param TraitNode $object object to convert to an TraitElement
44
     * @param StrategyContainer $strategies used to convert nested objects.
45
     * @return TraitElement
46
     */
47 5
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
48
    {
49 5
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
50
51 5
        $trait = new TraitElement($object->fqsen, $docBlock, new Location($object->getLine()));
52
53 5
        if (isset($object->stmts)) {
54 3
            foreach ($object->stmts as $stmt) {
55 3
                switch (get_class($stmt)) {
56 3
                    case PropertyNode::class:
57 1
                        $properties = new PropertyIterator($stmt);
58 1
                        foreach ($properties as $property) {
59 1
                            $element = $this->createMember($property, $strategies, $context);
60 1
                            $trait->addProperty($element);
0 ignored issues
show
Compatibility introduced by
$element of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Property>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61
                        }
62 1
                        break;
63 2
                    case ClassMethod::class:
64 1
                        $method = $this->createMember($stmt, $strategies, $context);
65 1
                        $trait->addMethod($method);
0 ignored issues
show
Compatibility introduced by
$method of type object<phpDocumentor\Reflection\Element> is not a sub-type of object<phpDocumentor\Reflection\Php\Method>. It seems like you assume a concrete implementation of the interface phpDocumentor\Reflection\Element to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
66 1
                        break;
67 1
                    case TraitUse::class:
68 1
                        foreach ($stmt->traits as $use) {
69 1
                            $trait->addUsedTrait(new Fqsen('\\' . $use->toString()));
70
                        }
71 3
                        break;
72
                }
73
            }
74
        }
75
76 5
        return $trait;
77
    }
78
}
79