Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

Descriptor/Builder/Reflector/FunctionAssembler.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\ArgumentDescriptor;
15
use phpDocumentor\Descriptor\Collection;
16
use phpDocumentor\Descriptor\FunctionDescriptor;
17
use phpDocumentor\Descriptor\TagDescriptor;
18
use phpDocumentor\Reflection\FunctionReflector;
19
use phpDocumentor\Reflection\Php\Argument;
20
use phpDocumentor\Reflection\Php\Function_;
21
22
/**
23
 * Assembles a FunctionDescriptor from a FunctionReflector.
24
 */
25
class FunctionAssembler extends AssemblerAbstract
26
{
27
    /** @var ArgumentAssembler */
28
    protected $argumentAssembler;
29
30
    /**
31
     * Initializes this assembler and its dependencies.
32
     */
33 1
    public function __construct(ArgumentAssembler $argumentAssembler)
34
    {
35 1
        $this->argumentAssembler = $argumentAssembler;
36 1
    }
37
38
    /**
39
     * Creates a Descriptor from the provided data.
40
     *
41
     * @param Function_ $data
42
     *
43
     * @return FunctionDescriptor
44
     */
45 1
    public function create($data)
46
    {
47 1
        $functionDescriptor = new FunctionDescriptor();
48
49 1
        $this->mapReflectorPropertiesOntoDescriptor($data, $functionDescriptor);
50 1
        $this->assembleDocBlock($data->getDocBlock(), $functionDescriptor);
51 1
        $this->addArgumentsToFunctionDescriptor($data->getArguments(), $functionDescriptor);
52
53 1
        return $functionDescriptor;
54
    }
55
56
    /**
57
     * Maps the properties of the Function reflector onto the Descriptor.
58
     *
59
     * @param Function_  $reflector
60
     * @param FunctionDescriptor $descriptor
61
     *
62
     * @return void
63
     */
64 1
    protected function mapReflectorPropertiesOntoDescriptor($reflector, $descriptor)
65
    {
66 1
        $packages = new Collection();
67 1
        $package = $this->extractPackageFromDocBlock($reflector->getDocBlock());
68
        //TODO: this looks like a potential bug. Have to investigate this!
69 1
        if ($package) {
70 1
            $tag = new TagDescriptor('package');
71 1
            $tag->setDescription($package);
72 1
            $packages->add($tag);
73
        }
74 1
        $descriptor->getTags()->set('package', $packages);
75
76 1
        $descriptor->setFullyQualifiedStructuralElementName($reflector->getFqsen());
77 1
        $descriptor->setName($reflector->getName());
78 1
        $descriptor->setLine($reflector->getLocation()->getLineNumber());
79 1
        $descriptor->setNamespace(substr($reflector->getFqsen(), 0, -strlen($reflector->getName())));
80 1
        $descriptor->setReturnType($reflector->getReturnType());
81 1
    }
82
83
    /**
84
     * Converts each argument reflector to an argument descriptor and adds it to the function descriptor.
85
     *
86
     * @param Argument[] $arguments
87
     * @param FunctionDescriptor                    $functionDescriptor
88
     *
89
     * @return void
90
     */
91 1
    protected function addArgumentsToFunctionDescriptor(array $arguments, $functionDescriptor)
92
    {
93 1
        foreach ($arguments as $argument) {
94 1
            $this->addArgumentDescriptorToFunction(
95 1
                $functionDescriptor,
96 1
                $this->createArgumentDescriptor($functionDescriptor, $argument)
0 ignored issues
show
$argument is of type object<phpDocumentor\Reflection\Php\Argument>, but the function expects a object<phpDocumentor\Ref...ctor\ArgumentReflector>.

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...
97
            );
98
        }
99 1
    }
100
101
    /**
102
     * Adds the given argument to the function.
103
     *
104
     * @param FunctionDescriptor $functionDescriptor
105
     * @param ArgumentDescriptor $argumentDescriptor
106
     *
107
     * @return void
108
     */
109 1
    protected function addArgumentDescriptorToFunction($functionDescriptor, $argumentDescriptor)
110
    {
111 1
        $functionDescriptor->getArguments()->set($argumentDescriptor->getName(), $argumentDescriptor);
112 1
    }
113
114
    /**
115
     * Creates a new ArgumentDescriptor from the given Reflector and Param.
116
     *
117
     * @param FunctionDescriptor                  $functionDescriptor
118
     * @param FunctionReflector\ArgumentReflector $argument
119
     *
120
     * @return ArgumentDescriptor
121
     */
122 1
    protected function createArgumentDescriptor($functionDescriptor, $argument)
123
    {
124 1
        $params = $functionDescriptor->getTags()->get('param', array());
125
126 1
        if (!$this->argumentAssembler->getBuilder()) {
127 1
            $this->argumentAssembler->setBuilder($this->builder);
128
        }
129
130 1
        return $this->argumentAssembler->create($argument, $params);
131
    }
132
}
133