Completed
Push — develop ( 9193e7...62056c )
by Jaap
12:45 queued 02:43
created

Descriptor/Builder/Reflector/FileAssembler.php (1 issue)

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
/**
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\Collection;
15
use phpDocumentor\Descriptor\FileDescriptor;
16
use phpDocumentor\Descriptor\TagDescriptor;
17
use phpDocumentor\Reflection\ClassReflector;
18
use phpDocumentor\Reflection\ConstantReflector;
19
use phpDocumentor\Reflection\FileReflector;
20
use phpDocumentor\Reflection\FunctionReflector;
21
use phpDocumentor\Reflection\InterfaceReflector;
22
use phpDocumentor\Reflection\Php\Class_;
23
use phpDocumentor\Reflection\Php\Constant;
24
use phpDocumentor\Reflection\Php\File;
25
use phpDocumentor\Reflection\Php\Function_;
26
use phpDocumentor\Reflection\Php\Interface_;
27
use phpDocumentor\Reflection\Php\Trait_;
28
use phpDocumentor\Reflection\TraitReflector;
29
30
/**
31
 * Assembles an FileDescriptor using an FileReflector and ParamDescriptors.
32
 */
33
class FileAssembler extends AssemblerAbstract
34
{
35
    /**
36
     * Creates a Descriptor from the provided data.
37
     *
38
     * @param File $data
39
     *
40
     * @return FileDescriptor
41
     */
42
    public function create($data)
43
    {
44
        $fileDescriptor = new FileDescriptor($data->getHash());
45
        $fileDescriptor->setPackage(
46
            $this->extractPackageFromDocBlock($data->getDocBlock()) ?: $this->getBuilder()->getDefaultPackage()
0 ignored issues
show
It seems like $data->getDocBlock() can be null; however, extractPackageFromDocBlock() 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...
47
        );
48
49
        $fileDescriptor->setName($data->getName());
50
        $fileDescriptor->setPath($data->getPath());
51
        if ($this->getBuilder()->getProjectDescriptor()->getSettings()->shouldIncludeSource()) {
52
            $fileDescriptor->setSource($data->getSource());
53
        }
54
        $fileDescriptor->setIncludes(new Collection($data->getIncludes()));
55
        $fileDescriptor->setNamespaceAliases(new Collection($data->getNamespaces()));
56
57
        $this->assembleDocBlock($data->getDocBlock(), $fileDescriptor);
58
        $this->overridePackageTag($data, $fileDescriptor);
59
60
        //$this->addMarkers($data->getMarkers(), $fileDescriptor);
61
        $this->addConstants($data->getConstants(), $fileDescriptor);
62
        $this->addFunctions($data->getFunctions(), $fileDescriptor);
63
        $this->addClasses($data->getClasses(), $fileDescriptor);
64
        $this->addInterfaces($data->getInterfaces(), $fileDescriptor);
65
        $this->addTraits($data->getTraits(), $fileDescriptor);
66
67
        return $fileDescriptor;
68
    }
69
70
    /**
71
     * Registers the child constants with the generated File Descriptor.
72
     *
73
     * @param Constant[] $constants
74
     * @param FileDescriptor      $fileDescriptor
75
     *
76
     * @return void
77
     */
78
    protected function addConstants($constants, $fileDescriptor)
79
    {
80
        foreach ($constants as $constant) {
81
            $constantDescriptor = $this->getBuilder()->buildDescriptor($constant);
82
            if ($constantDescriptor) {
83
                $constantDescriptor->setLocation($fileDescriptor, $constant->getLocation()->getLineNumber());
84
                if (count($constantDescriptor->getTags()->get('package', new Collection())) == 0) {
85
                    $constantDescriptor->getTags()
86
                        ->set('package', $fileDescriptor->getTags()->get('package', new Collection()));
87
                }
88
89
                $fileDescriptor->getConstants()->set(
90
                    (string)$constantDescriptor->getFullyQualifiedStructuralElementName(),
91
                    $constantDescriptor
92
                );
93
            }
94
        }
95
    }
96
97
    /**
98
     * Registers the child functions with the generated File Descriptor.
99
     *
100
     * @param Function_[] $functions
101
     * @param FileDescriptor      $fileDescriptor
102
     *
103
     * @return void
104
     */
105
    protected function addFunctions($functions, $fileDescriptor)
106
    {
107
        foreach ($functions as $function) {
108
            $functionDescriptor = $this->getBuilder()->buildDescriptor($function);
109
            if ($functionDescriptor) {
110
                $functionDescriptor->setLocation($fileDescriptor, $function->getLocation()->getLineNumber());
111
                if (count($functionDescriptor->getTags()->get('package', new Collection())) == 0) {
112
                    $functionDescriptor->getTags()
113
                        ->set('package', $fileDescriptor->getTags()->get('package', new Collection()));
114
                }
115
116
                $fileDescriptor->getFunctions()->set(
117
                    (string)$functionDescriptor->getFullyQualifiedStructuralElementName(),
118
                    $functionDescriptor
119
                );
120
            }
121
        }
122
    }
123
124
    /**
125
     * Registers the child classes with the generated File Descriptor.
126
     *
127
     * @param Class_[] $classes
128
     * @param FileDescriptor   $fileDescriptor
129
     *
130
     * @return void
131
     */
132
    protected function addClasses($classes, $fileDescriptor)
133
    {
134
        foreach ($classes as $class) {
135
            $classDescriptor = $this->getBuilder()->buildDescriptor($class);
136
            if ($classDescriptor) {
137
                $classDescriptor->setLocation($fileDescriptor, $class->getLocation()->getLineNumber());
138
                if (count($classDescriptor->getTags()->get('package', new Collection())) == 0) {
139
                    $classDescriptor->getTags()->set(
140
                        'package',
141
                        $fileDescriptor->getTags()->get('package', new Collection())
142
                    );
143
                }
144
145
                $fileDescriptor->getClasses()->set(
146
                    (string)($classDescriptor->getFullyQualifiedStructuralElementName()),
147
                    $classDescriptor
148
                );
149
            }
150
        }
151
    }
152
153
    /**
154
     * Registers the child interfaces with the generated File Descriptor.
155
     *
156
     * @param Interface_[] $interfaces
157
     * @param FileDescriptor   $fileDescriptor
158
     *
159
     * @return void
160
     */
161
    protected function addInterfaces($interfaces, $fileDescriptor)
162
    {
163
        foreach ($interfaces as $interface) {
164
            $interfaceDescriptor = $this->getBuilder()->buildDescriptor($interface);
165
            if ($interfaceDescriptor) {
166
                $interfaceDescriptor->setLocation($fileDescriptor, $interface->getLocation()->getLineNumber());
167
                if (count($interfaceDescriptor->getTags()->get('package', new Collection())) == 0) {
168
                    $interfaceDescriptor->getTags()
169
                        ->set('package', $fileDescriptor->getTags()->get('package', new Collection()));
170
                }
171
172
                $fileDescriptor->getInterfaces()->set(
173
                    (string)$interfaceDescriptor->getFullyQualifiedStructuralElementName(),
174
                    $interfaceDescriptor
175
                );
176
            }
177
        }
178
    }
179
180
    /**
181
     * Registers the child traits with the generated File Descriptor.
182
     *
183
     * @param Trait_[] $traits
184
     * @param FileDescriptor   $fileDescriptor
185
     *
186
     * @return void
187
     */
188
    protected function addTraits($traits, $fileDescriptor)
189
    {
190
        foreach ($traits as $trait) {
191
            $traitDescriptor = $this->getBuilder()->buildDescriptor($trait);
192
            if ($traitDescriptor) {
193
                $traitDescriptor->setLocation($fileDescriptor, $trait->getLocation()->getLineNumber());
194
                if (count($traitDescriptor->getTags()->get('package', new Collection())) == 0) {
195
                    $traitDescriptor->getTags()
196
                        ->set('package', $fileDescriptor->getTags()->get('package', new Collection()));
197
                }
198
199
                $fileDescriptor->getTraits()->set(
200
                    (string)$traitDescriptor->getFullyQualifiedStructuralElementName(),
201
                    $traitDescriptor
202
                );
203
            }
204
        }
205
    }
206
207
    /**
208
     * Registers the markers that were found in a File with the File Descriptor.
209
     *
210
     * @param string[]       $markers
211
     * @param FileDescriptor $fileDescriptor
212
     *
213
     * @return void
214
     */
215
    protected function addMarkers($markers, $fileDescriptor)
216
    {
217
        foreach ($markers as $marker) {
218
            list($type, $message, $line) = $marker;
219
            $fileDescriptor->getMarkers()->add(
220
                array(
221
                    'type'    => $type,
222
                    'message' => $message,
223
                    'line'    => $line,
224
                )
225
            );
226
        }
227
    }
228
229
    /**
230
     * @param $data
231
     * @param $fileDescriptor
232
     */
233
    protected function overridePackageTag($data, $fileDescriptor)
234
    {
235
        $packages = new Collection();
236
        $package  = $this->extractPackageFromDocBlock($data->getDocBlock());
237
        if (! $package) {
238
            $package = $this->getBuilder()->getDefaultPackage();
239
        }
240
        $tag = new TagDescriptor('package');
241
        $tag->setDescription($package);
242
        $packages->add($tag);
243
        $fileDescriptor->getTags()->set('package', $packages);
244
    }
245
}
246