Completed
Push — develop ( 9193e7...62056c )
by Jaap
12:45 queued 02:43
created

InterfaceAssembler::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 1
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Descriptor\Builder\Reflector;
13
14
use phpDocumentor\Descriptor\InterfaceDescriptor;
15
use phpDocumentor\Reflection\ClassReflector\MethodReflector;
16
use phpDocumentor\Reflection\ConstantReflector;
17
use phpDocumentor\Reflection\InterfaceReflector;
18
use phpDocumentor\Reflection\Php\Constant;
19
use phpDocumentor\Reflection\Php\Interface_;
20
use phpDocumentor\Reflection\Php\Method;
21
22
/**
23
 * Assembles an InterfaceDescriptor using an InterfaceReflector.
24
 */
25
class InterfaceAssembler extends AssemblerAbstract
26
{
27
    /**
28
     * Creates a Descriptor from the provided data.
29
     *
30
     * @param Interface_ $data
31
     *
32
     * @return InterfaceDescriptor
33
     */
34
    public function create($data)
35
    {
36
        $interfaceDescriptor = new InterfaceDescriptor();
37
38
        $interfaceDescriptor->setFullyQualifiedStructuralElementName($data->getFqsen());
39
        $interfaceDescriptor->setName($data->getName());
40
        $interfaceDescriptor->setLine($data->getLocation()->getLineNumber());
41
        $interfaceDescriptor->setPackage($this->extractPackageFromDocBlock($data->getDocBlock()) ?: '');
0 ignored issues
show
Bug introduced by
It seems like $data->getDocBlock() can be null; however, extractPackageFromDocBlock() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
42
43
        // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself
44
        $interfaceDescriptor->setNamespace(substr($data->getFqsen(), 0, -strlen($data->getName())-1));
45
46
        $this->assembleDocBlock($data->getDocBlock(), $interfaceDescriptor);
47
        $this->addConstants($data->getConstants(), $interfaceDescriptor);
48
        $this->addMethods($data->getMethods(), $interfaceDescriptor);
49
50
        foreach ($data->getParents() as $interfaceClassName) {
51
            $interfaceDescriptor->getParent()->set((string)$interfaceClassName, $interfaceClassName);
52
        }
53
54
        return $interfaceDescriptor;
55
    }
56
57
    /**
58
     * Registers the child constants with the generated Interface Descriptor.
59
     *
60
     * @param Constant[] $constants
61
     * @param InterfaceDescriptor $interfaceDescriptor
62
     *
63
     * @return void
64
     */
65
    protected function addConstants($constants, $interfaceDescriptor)
66
    {
67
        foreach ($constants as $constant) {
68
            $constantDescriptor = $this->getBuilder()->buildDescriptor($constant);
69
            if ($constantDescriptor) {
70
                $constantDescriptor->setParent($interfaceDescriptor);
71
                $interfaceDescriptor->getConstants()->set($constantDescriptor->getName(), $constantDescriptor);
72
            }
73
        }
74
    }
75
76
    /**
77
     * Registers the child methods with the generated Interface Descriptor.
78
     *
79
     * @param Method[] $methods
80
     * @param InterfaceDescriptor $interfaceDescriptor
81
     *
82
     * @return void
83
     */
84
    protected function addMethods($methods, $interfaceDescriptor)
85
    {
86
        foreach ($methods as $method) {
87
            $methodDescriptor = $this->getBuilder()->buildDescriptor($method);
88
            if ($methodDescriptor) {
89
                $methodDescriptor->setParent($interfaceDescriptor);
90
                $interfaceDescriptor->getMethods()->set($methodDescriptor->getName(), $methodDescriptor);
91
            }
92
        }
93
    }
94
}
95