Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

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