Completed
Push — develop ( 9dbd4b...3bc77a )
by Jaap
16s
created

Descriptor/Builder/Reflector/MethodAssembler.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\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 4
    public function __construct(ArgumentAssembler $argumentAssembler)
36
    {
37 4
        $this->argumentAssembler = $argumentAssembler;
38 4
    }
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);
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->setTypes($types);
0 ignored issues
show
$types is of type object<phpDocumentor\Reflection\Type>|null, but the function expects a object<phpDocumentor\Descriptor\Collection>.

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...
127 1
            $argument->setDescription($lastParamTag->getDescription());
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