Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
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
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
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor\Builder\Reflector;
17
18
use phpDocumentor\Descriptor\ConstantDescriptor;
19
use phpDocumentor\Descriptor\InterfaceDescriptor;
20
use phpDocumentor\Descriptor\MethodDescriptor;
21
use phpDocumentor\Reflection\Php\Constant;
22
use phpDocumentor\Reflection\Php\Interface_;
23
use phpDocumentor\Reflection\Php\Method;
24
25
/**
26
 * Assembles an InterfaceDescriptor using an InterfaceReflector.
27
 */
28
class InterfaceAssembler extends AssemblerAbstract
29
{
30
    /**
31
     * Creates a Descriptor from the provided data.
32
     *
33
     * @param Interface_ $data
34
     *
35
     * @return InterfaceDescriptor
36
     */
37
    public function create($data)
38
    {
39
        $interfaceDescriptor = new InterfaceDescriptor();
40
41
        $interfaceDescriptor->setFullyQualifiedStructuralElementName($data->getFqsen());
42
        $interfaceDescriptor->setName($data->getName());
43
        $interfaceDescriptor->setLine($data->getLocation()->getLineNumber());
44
        $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...
45
46
        // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself
47
        $interfaceDescriptor->setNamespace(substr((string) $data->getFqsen(), 0, -strlen($data->getName()) - 1));
48
49
        $this->assembleDocBlock($data->getDocBlock(), $interfaceDescriptor);
50
        $this->addConstants($data->getConstants(), $interfaceDescriptor);
51
        $this->addMethods($data->getMethods(), $interfaceDescriptor);
52
53
        foreach ($data->getParents() as $interfaceClassName) {
54
            $interfaceDescriptor->getParent()->set((string) $interfaceClassName, $interfaceClassName);
55
        }
56
57
        return $interfaceDescriptor;
58
    }
59
60
    /**
61
     * Registers the child constants with the generated Interface Descriptor.
62
     *
63
     * @param Constant[] $constants
64
     */
65
    protected function addConstants(array $constants, InterfaceDescriptor $interfaceDescriptor): void
66
    {
67
        foreach ($constants as $constant) {
68
            $constantDescriptor = $this->getBuilder()->buildDescriptor($constant);
69
            if ($constantDescriptor instanceof 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
     */
81
    protected function addMethods(array $methods, InterfaceDescriptor $interfaceDescriptor): void
82
    {
83
        foreach ($methods as $method) {
84
            $methodDescriptor = $this->getBuilder()->buildDescriptor($method);
85
            if ($methodDescriptor instanceof MethodDescriptor) {
86
                $methodDescriptor->setParent($interfaceDescriptor);
87
                $interfaceDescriptor->getMethods()->set($methodDescriptor->getName(), $methodDescriptor);
88
            }
89
        }
90
    }
91
}
92