Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Builder/Reflector/InterfaceAssembler.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
19
/**
20
 * Assembles an InterfaceDescriptor using an InterfaceReflector.
21
 */
22
class InterfaceAssembler extends AssemblerAbstract
23
{
24
    /**
25
     * Creates a Descriptor from the provided data.
26
     *
27
     * @param InterfaceReflector $data
28
     *
29
     * @return InterfaceDescriptor
30
     */
31
    public function create($data)
32
    {
33
        $interfaceDescriptor = new InterfaceDescriptor();
34
35
        $interfaceDescriptor->setFullyQualifiedStructuralElementName($data->getName());
36
        $interfaceDescriptor->setName($data->getShortName());
37
        $interfaceDescriptor->setLine($data->getLinenumber());
38
        $interfaceDescriptor->setPackage($this->extractPackageFromDocBlock($data->getDocBlock()) ?: '');
0 ignored issues
show
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...
39
40
        // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself
41
        $interfaceDescriptor->setNamespace(
42
            '\\' . (strtolower($data->getNamespace()) == 'global' ? '' :$data->getNamespace())
43
        );
44
45
        $this->assembleDocBlock($data->getDocBlock(), $interfaceDescriptor);
46
        $this->addConstants($data->getConstants(), $interfaceDescriptor);
47
        $this->addMethods($data->getMethods(), $interfaceDescriptor);
48
49
        foreach ($data->getParentInterfaces() as $interfaceClassName) {
50
            $interfaceDescriptor->getParent()->set($interfaceClassName, $interfaceClassName);
51
        }
52
53
        return $interfaceDescriptor;
54
    }
55
56
    /**
57
     * Registers the child constants with the generated Interface Descriptor.
58
     *
59
     * @param ConstantReflector[] $constants
60
     * @param InterfaceDescriptor $interfaceDescriptor
61
     *
62
     * @return void
63
     */
64
    protected function addConstants($constants, $interfaceDescriptor)
65
    {
66
        foreach ($constants as $constant) {
67
            $constantDescriptor = $this->getBuilder()->buildDescriptor($constant);
68
            if ($constantDescriptor) {
69
                $constantDescriptor->setParent($interfaceDescriptor);
70
                $interfaceDescriptor->getConstants()->set($constantDescriptor->getName(), $constantDescriptor);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Registers the child methods with the generated Interface Descriptor.
77
     *
78
     * @param MethodReflector[] $methods
79
     * @param InterfaceDescriptor $interfaceDescriptor
80
     *
81
     * @return void
82
     */
83
    protected function addMethods($methods, $interfaceDescriptor)
84
    {
85
        foreach ($methods as $method) {
86
            $methodDescriptor = $this->getBuilder()->buildDescriptor($method);
87
            if ($methodDescriptor) {
88
                $methodDescriptor->setParent($interfaceDescriptor);
89
                $interfaceDescriptor->getMethods()->set($methodDescriptor->getName(), $methodDescriptor);
90
            }
91
        }
92
    }
93
}
94