Completed
Push — develop ( 0b8425...c51867 )
by Jaap
15s queued 11s
created

TraitAssembler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 12
cts 26
cp 0.4615
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addProperties() 0 10 3
A create() 0 21 2
A addMethods() 0 10 3
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\MethodDescriptor;
19
use phpDocumentor\Descriptor\PropertyDescriptor;
20
use phpDocumentor\Descriptor\TraitDescriptor;
21
use phpDocumentor\Reflection\Php\Method;
22
use phpDocumentor\Reflection\Php\Property;
23
use phpDocumentor\Reflection\Php\Trait_;
24
25
/**
26
 * Assembles an TraitDescriptor using an TraitReflector.
27
 */
28
class TraitAssembler extends AssemblerAbstract
29
{
30
    /**
31
     * Creates a Descriptor from the provided data.
32
     *
33
     * @param Trait_ $data
34
     *
35
     * @return TraitDescriptor
36
     */
37 1
    public function create($data)
38
    {
39 1
        $traitDescriptor = new TraitDescriptor();
40
41 1
        $traitDescriptor->setFullyQualifiedStructuralElementName($data->getFqsen());
42 1
        $traitDescriptor->setName($data->getName());
43 1
        $traitDescriptor->setLine($data->getLocation()->getLineNumber());
44 1
        $traitDescriptor->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...
Documentation introduced by
$this->extractPackageFro...a->getDocBlock()) ?: '' is of type string, but the function expects a object<phpDocumentor\Des...ptor\PackageDescriptor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
46
        // Reflection library formulates namespace as global but this is not wanted for phpDocumentor itself
47 1
        $traitDescriptor->setNamespace(
48 1
            substr((string) $data->getFqsen(), 0, -strlen($data->getName()) - 1)
49
        );
50
51 1
        $this->assembleDocBlock($data->getDocBlock(), $traitDescriptor);
52
53 1
        $this->addProperties($data->getProperties(), $traitDescriptor);
54 1
        $this->addMethods($data->getMethods(), $traitDescriptor);
55
56 1
        return $traitDescriptor;
57
    }
58
59
    /**
60
     * Registers the child properties with the generated Trait Descriptor.
61
     *
62
     * @param Property[] $properties
63
     */
64
    protected function addProperties(array $properties, TraitDescriptor $traitDescriptor): void
65
    {
66
        foreach ($properties as $property) {
67
            $propertyDescriptor = $this->getBuilder()->buildDescriptor($property);
68
            if ($propertyDescriptor instanceof PropertyDescriptor) {
69
                $propertyDescriptor->setParent($traitDescriptor);
70
                $traitDescriptor->getProperties()->set($propertyDescriptor->getName(), $propertyDescriptor);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Registers the child methods with the generated Trait Descriptor.
77
     *
78
     * @param Method[] $methods
79
     */
80
    protected function addMethods(array $methods, TraitDescriptor $traitDescriptor): void
81
    {
82
        foreach ($methods as $method) {
83
            $methodDescriptor = $this->getBuilder()->buildDescriptor($method);
84
            if ($methodDescriptor instanceof MethodDescriptor) {
85
                $methodDescriptor->setParent($traitDescriptor);
86
                $traitDescriptor->getMethods()->set($methodDescriptor->getName(), $methodDescriptor);
87
            }
88
        }
89
    }
90
}
91