Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
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-2018 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 1
    protected function mapReflectorPropertiesOntoDescriptor($reflector, $descriptor)
63
    {
64 1
        $packages = new Collection();
65 1
        $package = $this->extractPackageFromDocBlock($reflector->getDocBlock());
66
        //TODO: this looks like a potential bug. Have to investigate this!
67 1
        if ($package) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $package of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68 1
            $tag = new TagDescriptor('package');
69 1
            $tag->setDescription($package);
70 1
            $packages->add($tag);
71
        }
72
73 1
        $descriptor->getTags()->set('package', $packages);
74
75 1
        $descriptor->setFullyQualifiedStructuralElementName($reflector->getFqsen());
76 1
        $descriptor->setName($reflector->getName());
77 1
        $descriptor->setLine($reflector->getLocation()->getLineNumber());
78 1
        $descriptor->setNamespace(substr($reflector->getFqsen(), 0, -strlen($reflector->getName())));
79 1
        $descriptor->setReturnType($reflector->getReturnType());
80 1
    }
81
82
    /**
83
     * Converts each argument reflector to an argument descriptor and adds it to the function descriptor.
84
     *
85
     * @param Argument[] $arguments
86
     * @param FunctionDescriptor                    $functionDescriptor
87
     */
88 1
    protected function addArgumentsToFunctionDescriptor(array $arguments, $functionDescriptor)
89
    {
90 1
        foreach ($arguments as $argument) {
91 1
            $this->addArgumentDescriptorToFunction(
92 1
                $functionDescriptor,
93 1
                $this->createArgumentDescriptor($functionDescriptor, $argument)
94
            );
95
        }
96 1
    }
97
98
    /**
99
     * Adds the given argument to the function.
100
     *
101
     * @param FunctionDescriptor $functionDescriptor
102
     * @param ArgumentDescriptor $argumentDescriptor
103
     */
104 1
    protected function addArgumentDescriptorToFunction($functionDescriptor, $argumentDescriptor)
105
    {
106 1
        $functionDescriptor->getArguments()->set($argumentDescriptor->getName(), $argumentDescriptor);
107 1
    }
108
109
    /**
110
     * Creates a new ArgumentDescriptor from the given Reflector and Param.
111
     *
112
     * @return ArgumentDescriptor
113
     */
114 1
    protected function createArgumentDescriptor(FunctionDescriptor $functionDescriptor, Argument $argument)
115
    {
116 1
        $params = $functionDescriptor->getTags()->get('param', []);
117
118 1
        if (!$this->argumentAssembler->getBuilder()) {
119 1
            $this->argumentAssembler->setBuilder($this->builder);
120
        }
121
122 1
        return $this->argumentAssembler->create($argument, $params);
123
    }
124
}
125