Interface_::doCreate()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0061

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 3
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
crap 7.0061
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\Interface_ as InterfaceElement;
20
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
21
use phpDocumentor\Reflection\Php\StrategyContainer;
22
use phpDocumentor\Reflection\Types\Context;
23
use PhpParser\Node\Stmt\ClassConst;
24
use PhpParser\Node\Stmt\ClassMethod;
25
use PhpParser\Node\Stmt\Interface_ as InterfaceNode;
26
27
/**
28
 * Strategy to create a InterfaceElement including all sub elements.
29
 */
30
// @codingStandardsIgnoreStart
31
final class Interface_ extends AbstractFactory implements ProjectFactoryStrategy
32
// @codingStandardsIgnoreEnd
33
{
34 1
    public function matches($object): bool
35
    {
36 1
        return $object instanceof InterfaceNode;
37
    }
38
39
    /**
40
     * Creates an Interface_ out of the given object.
41
     * Since an object might contain other objects that need to be converted the $factory is passed so it can be
42
     * used to create nested Elements.
43
     *
44
     * @param InterfaceNode $object object to convert to an Element
45
     * @param StrategyContainer $strategies used to convert nested objects.
46
     * @param Context $context of the created object
47
     * @return InterfaceElement
48
     */
49 4
    protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
50
    {
51 4
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);
52 4
        $parents = [];
53 4
        foreach ($object->extends as $extend) {
54
            $parents['\\' . (string) $extend] = new Fqsen('\\' . (string) $extend);
55
        }
56
57 4
        $interface = new InterfaceElement($object->fqsen, $parents, $docBlock, new Location($object->getLine()));
58
59 4
        if (isset($object->stmts)) {
60 2
            foreach ($object->stmts as $stmt) {
61 2
                switch (get_class($stmt)) {
62 2
                    case ClassMethod::class:
63 1
                        $method = $this->createMember($stmt, $strategies, $context);
64 1
                        $interface->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...
65 1
                        break;
66 1
                    case ClassConst::class:
67 1
                        $constants = new ClassConstantIterator($stmt);
68 1
                        foreach ($constants as $const) {
69 1
                            $element = $this->createMember($const, $strategies, $context);
70 1
                            $interface->addConstant($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\Constant>. 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...
71
                        }
72 2
                        break;
73
                }
74
            }
75
        }
76
77 4
        return $interface;
78
    }
79
}
80