Completed
Push — develop ( bd0ea7...cbc256 )
by Mike
08:51
created

Descriptor/Builder/Reflector/MethodAssembler.php (2 issues)

Labels
Severity

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\ArgumentDescriptor;
19
use phpDocumentor\Descriptor\MethodDescriptor;
20
use phpDocumentor\Reflection\DocBlock\Tags\Param;
21
use phpDocumentor\Reflection\Php\Argument;
22
use phpDocumentor\Reflection\Php\Method;
23
24
/**
25
 * Assembles a MethodDescriptor from a MethodReflector.
26
 */
27
class MethodAssembler extends AssemblerAbstract
28
{
29
    /** @var ArgumentAssembler */
30
    protected $argumentAssembler;
31
32
    /**
33
     * Initializes this assembler with its dependencies.
34
     */
35 3
    public function __construct(ArgumentAssembler $argumentAssembler)
36
    {
37 3
        $this->argumentAssembler = $argumentAssembler;
38 3
    }
39
40
    /**
41
     * Creates a Descriptor from the provided data.
42
     *
43
     * @param Method $data
44
     *
45
     * @return MethodDescriptor
46
     */
47 3
    public function create($data)
48
    {
49 3
        $methodDescriptor = new MethodDescriptor();
50 3
        $methodDescriptor->setNamespace(substr((string) $data->getFqsen(), 0, -strlen($data->getName()) - 4));
51 3
        $this->mapReflectorToDescriptor($data, $methodDescriptor);
52
53 3
        $this->assembleDocBlock($data->getDocBlock(), $methodDescriptor);
54 3
        $this->addArguments($data, $methodDescriptor);
55 3
        $this->addVariadicArgument($data, $methodDescriptor);
56
57 3
        return $methodDescriptor;
58
    }
59
60
    /**
61
     * Maps the fields to the reflector to the descriptor.
62
     */
63 3
    protected function mapReflectorToDescriptor(Method $reflector, MethodDescriptor $descriptor): void
64
    {
65 3
        $descriptor->setFullyQualifiedStructuralElementName($reflector->getFqsen());
66 3
        $descriptor->setName($reflector->getName());
67 3
        $descriptor->setVisibility((string) $reflector->getVisibility() ?: 'public');
68 3
        $descriptor->setFinal($reflector->isFinal());
69 3
        $descriptor->setAbstract($reflector->isAbstract());
70 3
        $descriptor->setStatic($reflector->isStatic());
71 3
        $descriptor->setLine($reflector->getLocation()->getLineNumber());
72 3
        $descriptor->setReturnType($reflector->getReturnType());
73 3
    }
74
75
    /**
76
     * Adds the reflected Arguments to the Descriptor.
77
     */
78 3
    protected function addArguments(Method $reflector, MethodDescriptor $descriptor): void
79
    {
80 3
        foreach ($reflector->getArguments() as $argument) {
81 3
            $this->addArgument($argument, $descriptor);
82
        }
83 3
    }
84
85
    /**
86
     * Adds a single reflected Argument to the Method Descriptor.
87
     */
88 3
    protected function addArgument(Argument $argument, MethodDescriptor $descriptor): void
89
    {
90 3
        $params = $descriptor->getTags()->get('param', []);
91
92 3
        if (!$this->argumentAssembler->getBuilder()) {
93 3
            $this->argumentAssembler->setBuilder($this->builder);
0 ignored issues
show
It seems like $this->builder can be null; however, setBuilder() 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...
94
        }
95
96 3
        $argumentDescriptor = $this->argumentAssembler->create($argument, $params);
97
98 3
        $descriptor->addArgument($argumentDescriptor->getName(), $argumentDescriptor);
99 3
    }
100
101
    /**
102
     * Checks if there is a variadic argument in the `@param` tags and adds it to the list of Arguments in
103
     * the Descriptor unless there is already one present.
104
     */
105 3
    protected function addVariadicArgument(Method $data, MethodDescriptor $methodDescriptor): void
106
    {
107 3
        if (!$data->getDocBlock()) {
108 1
            return;
109
        }
110
111 2
        $paramTags = $data->getDocBlock()->getTagsByName('param');
112
113
        /** @var Param $lastParamTag */
114 2
        $lastParamTag = end($paramTags);
115 2
        if (!$lastParamTag) {
116 1
            return;
117
        }
118
119 1
        if ($lastParamTag->isVariadic()
120 1
            && in_array($lastParamTag->getVariableName(), array_keys($methodDescriptor->getArguments()->getAll()), true)
121
        ) {
122 1
            $types = $lastParamTag->getType();
123
124 1
            $argument = new ArgumentDescriptor();
125 1
            $argument->setName($lastParamTag->getVariableName());
126 1
            $argument->setType($types);
127 1
            $argument->setDescription($lastParamTag->getDescription());
0 ignored issues
show
It seems like $lastParamTag->getDescription() targeting phpDocumentor\Reflection...seTag::getDescription() can also be of type null or object<phpDocumentor\Ref...n\DocBlock\Description>; however, phpDocumentor\Descriptor...tract::setDescription() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
128 1
            $argument->setLine($methodDescriptor->getLine());
129 1
            $argument->setVariadic(true);
130
131 1
            $methodDescriptor->getArguments()->set($argument->getName(), $argument);
132
        }
133 1
    }
134
}
135