Completed
Push — develop ( 40623c...488cb7 )
by Mike
06:19
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
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\Collection;
20
use phpDocumentor\Descriptor\FunctionDescriptor;
21
use phpDocumentor\Descriptor\TagDescriptor;
22
use phpDocumentor\Reflection\Php\Argument;
23
use phpDocumentor\Reflection\Php\Function_;
24
25
/**
26
 * Assembles a FunctionDescriptor from a FunctionReflector.
27
 */
28
class FunctionAssembler extends AssemblerAbstract
29
{
30
    /** @var ArgumentAssembler */
31
    protected $argumentAssembler;
32
33
    /**
34
     * Initializes this assembler and its dependencies.
35
     */
36 2
    public function __construct(ArgumentAssembler $argumentAssembler)
37
    {
38 2
        $this->argumentAssembler = $argumentAssembler;
39 2
    }
40
41
    /**
42
     * Creates a Descriptor from the provided data.
43
     *
44
     * @param Function_ $data
45
     *
46
     * @return FunctionDescriptor
47
     */
48 1
    public function create($data)
49
    {
50 1
        $functionDescriptor = new FunctionDescriptor();
51
52 1
        $this->mapReflectorPropertiesOntoDescriptor($data, $functionDescriptor);
53 1
        $this->assembleDocBlock($data->getDocBlock(), $functionDescriptor);
54 1
        $this->addArgumentsToFunctionDescriptor($data->getArguments(), $functionDescriptor);
55
56 1
        return $functionDescriptor;
57
    }
58
59
    /**
60
     * Maps the properties of the Function reflector onto the Descriptor.
61
     */
62 1
    protected function mapReflectorPropertiesOntoDescriptor(Function_ $reflector, FunctionDescriptor $descriptor): void
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('\\' . trim(substr((string) $reflector->getFqsen(), 0, -strlen($reflector->getName()) - 2), '\\'));
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
     */
87 1
    protected function addArgumentsToFunctionDescriptor(array $arguments, FunctionDescriptor $functionDescriptor): void
88
    {
89 1
        foreach ($arguments as $argument) {
90 1
            $this->addArgumentDescriptorToFunction(
91 1
                $functionDescriptor,
92 1
                $this->createArgumentDescriptor($functionDescriptor, $argument)
93
            );
94
        }
95 1
    }
96
97
    /**
98
     * Adds the given argument to the function.
99
     */
100 1
    protected function addArgumentDescriptorToFunction(FunctionDescriptor $functionDescriptor, ArgumentDescriptor $argumentDescriptor): void
101
    {
102 1
        $functionDescriptor->getArguments()->set($argumentDescriptor->getName(), $argumentDescriptor);
103 1
    }
104
105
    /**
106
     * Creates a new ArgumentDescriptor from the given Reflector and Param.
107
     */
108 1
    protected function createArgumentDescriptor(FunctionDescriptor $functionDescriptor, Argument $argument): ArgumentDescriptor
109
    {
110 1
        $params = $functionDescriptor->getTags()->get('param', []);
111
112 1
        if (!$this->argumentAssembler->getBuilder()) {
113 1
            $this->argumentAssembler->setBuilder($this->builder);
114
        }
115
116 1
        return $this->argumentAssembler->create($argument, $params);
117
    }
118
}
119