Completed
Push — develop ( 8fda15...dbbcf5 )
by Jaap
14s
created

src/phpDocumentor/Descriptor/ServiceProvider.php (3 issues)

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;
13
14
use Cilex\Application;
15
use phpDocumentor\Descriptor\Builder\AssemblerFactory;
16
use phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler;
17
use phpDocumentor\Descriptor\Builder\Reflector\ClassAssembler;
18
use phpDocumentor\Descriptor\Builder\Reflector\ConstantAssembler;
19
use phpDocumentor\Descriptor\Builder\Reflector\FileAssembler;
20
use phpDocumentor\Descriptor\Builder\Reflector\FunctionAssembler;
21
use phpDocumentor\Descriptor\Builder\Reflector\InterfaceAssembler;
22
use phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler;
23
use phpDocumentor\Descriptor\Builder\Reflector\NamespaceAssembler;
24
use phpDocumentor\Descriptor\Builder\Reflector\PropertyAssembler;
25
use phpDocumentor\Descriptor\Builder\Reflector\Tags\AuthorAssembler;
26
use phpDocumentor\Descriptor\Builder\Reflector\Tags\DeprecatedAssembler;
27
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler;
28
use phpDocumentor\Descriptor\Builder\Reflector\Tags\GenericTagAssembler;
29
use phpDocumentor\Descriptor\Builder\Reflector\Tags\LinkAssembler;
30
use phpDocumentor\Descriptor\Builder\Reflector\Tags\MethodAssembler as MethodTagAssembler;
31
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ParamAssembler;
32
use phpDocumentor\Descriptor\Builder\Reflector\Tags\PropertyAssembler as PropertyTagAssembler;
33
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ReturnAssembler;
34
use phpDocumentor\Descriptor\Builder\Reflector\Tags\SeeAssembler;
35
use phpDocumentor\Descriptor\Builder\Reflector\Tags\SinceAssembler;
36
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ThrowsAssembler;
37
use phpDocumentor\Descriptor\Builder\Reflector\Tags\UsesAssembler;
38
use phpDocumentor\Descriptor\Builder\Reflector\Tags\VarAssembler;
39
use phpDocumentor\Descriptor\Builder\Reflector\Tags\VersionAssembler;
40
use phpDocumentor\Descriptor\Builder\Reflector\TraitAssembler;
41
use phpDocumentor\Descriptor\Filter\ClassFactory;
42
use phpDocumentor\Descriptor\Filter\Filter;
43
use phpDocumentor\Descriptor\Filter\StripIgnore;
44
use phpDocumentor\Descriptor\Filter\StripInternal;
45
use phpDocumentor\Descriptor\Filter\StripOnVisibility;
46
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
47
use phpDocumentor\Reflection\DocBlock\Tag;
48
use phpDocumentor\Reflection\DocBlock\Tags;
49
use phpDocumentor\Reflection\DocBlock\Tags\Author;
50
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
51
use phpDocumentor\Reflection\DocBlock\Tags\Example;
52
use phpDocumentor\Reflection\DocBlock\Tags\Link;
53
use phpDocumentor\Reflection\DocBlock\Tags\Param;
54
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
55
use phpDocumentor\Reflection\DocBlock\Tags\See;
56
use phpDocumentor\Reflection\DocBlock\Tags\Since;
57
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
58
use phpDocumentor\Reflection\DocBlock\Tags\Uses;
59
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
60
use phpDocumentor\Reflection\DocBlock\Tags\Version;
61
use phpDocumentor\Reflection\Php\Argument;
62
use phpDocumentor\Reflection\Php\Class_;
63
use phpDocumentor\Reflection\Php\Constant;
64
use phpDocumentor\Reflection\Php\File;
65
use phpDocumentor\Reflection\Php\Function_;
66
use phpDocumentor\Reflection\Php\Interface_;
67
use phpDocumentor\Reflection\Php\Method;
68
use phpDocumentor\Reflection\Php\Namespace_;
69
use phpDocumentor\Reflection\Php\Property;
70
use phpDocumentor\Reflection\Php\Trait_;
71
use Pimple\Container;
72
use Pimple\ServiceProviderInterface;
73
use Zend\Cache\Storage\Adapter\Filesystem;
74
use Zend\Cache\Storage\Plugin\PluginOptions;
75
use Zend\Cache\Storage\Plugin\Serializer as SerializerPlugin;
76
77
/**
78
 * This provider is responsible for registering the Descriptor component with the given Application.
79
 */
80
class ServiceProvider implements ServiceProviderInterface
81
{
82
    /**
83
     * Adds the services needed to build the descriptors.
84
     *
85
     * @param Container $app An Application instance
86
     */
87
    public function register(Container $app)
88
    {
89
        $app['parser.example.finder'] = new ExampleFinder();
90
91
        $this->addCache($app);
0 ignored issues
show
The call to the method phpDocumentor\Descriptor...iceProvider::addCache() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
92
        $this->addAssemblers($app);
93
        $this->addFilters($app);
0 ignored issues
show
The call to the method phpDocumentor\Descriptor...eProvider::addFilters() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
94
        $this->addBuilder($app);
95
96
        // I would prefer to extend it but due to a circular reference will pimple fatal
97
        $this->attachFiltersToManager($app['descriptor.filter'], $app);
98
99
        $app['descriptor.analyzer'] = function () {
100
            return new ProjectAnalyzer();
101
        };
102
    }
103
104
    /**
105
     * Registers the Assemblers used to convert Reflection objects to Descriptors.
106
     *
107
     * @return AssemblerFactory
108
     */
109
    public function attachAssemblersToFactory(AssemblerFactory $factory, Application $app)
110
    {
111
        // @codingStandardsIgnoreStart because we limit the verbosity by making all closures single-line
112
        $fileMatcher = function ($criteria) {
113
            return $criteria instanceof File;
114
        };
115
        $constantMatcher = function ($criteria) {
116
            return $criteria instanceof Constant; // || $criteria instanceof ClassConstant;
117
        };
118
        $traitMatcher = function ($criteria) {
119
            return $criteria instanceof Trait_;
120
        };
121
        $classMatcher = function ($criteria) {
122
            return $criteria instanceof Class_;
123
        };
124
        $interfaceMatcher = function ($criteria) {
125
            return $criteria instanceof Interface_;
126
        };
127
        $propertyMatcher = function ($criteria) {
128
            return $criteria instanceof Property;
129
        };
130
        $methodMatcher = function ($criteria) {
131
            return $criteria instanceof Method;
132
        };
133
        $argumentMatcher = function ($criteria) {
134
            return $criteria instanceof Argument;
135
        };
136
        $functionMatcher = function ($criteria) {
137
            return $criteria instanceof Function_;
138
        };
139
        $namespaceMatcher = function ($criteria) {
140
            return $criteria instanceof Namespace_;
141
        };
142
143
        $authorMatcher = function ($criteria) {
144
            return $criteria instanceof Author;
145
        };
146
        $deprecatedMatcher = function ($criteria) {
147
            return $criteria instanceof Deprecated;
148
        };
149
        $exampleMatcher = function ($criteria) {
150
            return $criteria instanceof Example;
151
        };
152
        $linkMatcher = function ($criteria) {
153
            return $criteria instanceof Link;
154
        };
155
        $methodTagMatcher = function ($criteria) {
156
            return $criteria instanceof Tags\Method;
157
        };
158
        $propertyTagMatcher = function ($criteria) {
159
            return $criteria instanceof Tags\Property;
160
        };
161
        $paramMatcher = function ($criteria) {
162
            return $criteria instanceof Param;
163
        };
164
        $throwsMatcher = function ($criteria) {
165
            return $criteria instanceof Throws;
166
        };
167
        $returnMatcher = function ($criteria) {
168
            return $criteria instanceof Return_;
169
        };
170
        $usesMatcher = function ($criteria) {
171
            return $criteria instanceof Uses;
172
        };
173
        $seeMatcher = function ($criteria) {
174
            return $criteria instanceof See;
175
        };
176
        $sinceMatcher = function ($criteria) {
177
            return $criteria instanceof Since;
178
        };
179
        $varMatcher = function ($criteria) {
180
            return $criteria instanceof Var_;
181
        };
182
        $versionMatcher = function ($criteria) {
183
            return $criteria instanceof Version;
184
        };
185
186
        //$typeCollectionMatcher = function ($criteria) { return $criteria instanceof TypeCollection; };
187
188
        $tagFallbackMatcher = function ($criteria) {
189
            return $criteria instanceof Tag;
190
        };
191
        // @codingStandardsIgnoreEnd
192
193
        $argumentAssembler = new ArgumentAssembler();
194
        $factory->register($fileMatcher, new FileAssembler());
195
        $factory->register($constantMatcher, new ConstantAssembler());
196
        $factory->register($traitMatcher, new TraitAssembler());
197
        $factory->register($classMatcher, new ClassAssembler());
198
        $factory->register($interfaceMatcher, new InterfaceAssembler());
199
        $factory->register($propertyMatcher, new PropertyAssembler());
200
        $factory->register($argumentMatcher, $argumentAssembler);
201
        $factory->register($methodMatcher, new MethodAssembler($argumentAssembler));
202
        $factory->register($functionMatcher, new FunctionAssembler($argumentAssembler));
203
        $factory->register($namespaceMatcher, new NamespaceAssembler());
204
205
        $factory->register($authorMatcher, new AuthorAssembler());
206
        $factory->register($deprecatedMatcher, new DeprecatedAssembler());
207
        $factory->register($exampleMatcher, new ExampleAssembler($app['parser.example.finder']));
208
        $factory->register($linkMatcher, new LinkAssembler());
209
        $factory->register($methodTagMatcher, new MethodTagAssembler());
210
        $factory->register($propertyTagMatcher, new PropertyTagAssembler());
211
        $factory->register($varMatcher, new VarAssembler());
212
        $factory->register($paramMatcher, new ParamAssembler());
213
        $factory->register($throwsMatcher, new ThrowsAssembler());
214
        $factory->register($returnMatcher, new ReturnAssembler());
215
        $factory->register($usesMatcher, new UsesAssembler());
216
        $factory->register($seeMatcher, new SeeAssembler());
217
        $factory->register($sinceMatcher, new SinceAssembler());
218
        $factory->register($versionMatcher, new VersionAssembler());
219
220
//        $factory->register($typeCollectionMatcher, new TypeCollectionAssembler());
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
221
222
        $factory->registerFallback($tagFallbackMatcher, new GenericTagAssembler());
223
224
        return $factory;
225
    }
226
227
    /**
228
     * Attaches filters to the manager.
229
     *
230
     * @return Filter
231
     */
232
    public function attachFiltersToManager(Filter $filterManager, Application $app)
233
    {
234
        $stripOnVisibility = new StripOnVisibility($app['descriptor.builder']);
235
        $filtersOnAllDescriptors = [
236
            new StripInternal($app['descriptor.builder']),
237
            new StripIgnore($app['descriptor.builder']),
238
        ];
239
240
        foreach ($filtersOnAllDescriptors as $filter) {
241
            $filterManager->attach('phpDocumentor\Descriptor\ConstantDescriptor', $filter);
242
            $filterManager->attach('phpDocumentor\Descriptor\FunctionDescriptor', $filter);
243
            $filterManager->attach('phpDocumentor\Descriptor\InterfaceDescriptor', $filter);
244
            $filterManager->attach('phpDocumentor\Descriptor\TraitDescriptor', $filter);
245
            $filterManager->attach('phpDocumentor\Descriptor\PropertyDescriptor', $filter);
246
            $filterManager->attach('phpDocumentor\Descriptor\MethodDescriptor', $filter);
247
        }
248
249
        $filterManager->attach('phpDocumentor\Descriptor\PropertyDescriptor', $stripOnVisibility);
250
        $filterManager->attach('phpDocumentor\Descriptor\MethodDescriptor', $stripOnVisibility);
251
252
        return $filterManager;
253
    }
254
255
    /**
256
     * Adds the caching mechanism to the dependency injection container with key 'descriptor.cache'.
257
     */
258
    protected function addCache(Application $app)
259
    {
260
        $app['descriptor.cache'] = function () {
261
            $cache = new Filesystem();
262
            $cache->setOptions(
263
                [
264
                    'namespace' => 'phpdoc-cache',
265
                    'cache_dir' => sys_get_temp_dir(),
266
                ]
267
            );
268
            $plugin = new SerializerPlugin();
269
270
            if (extension_loaded('igbinary')) {
271
                $options = new PluginOptions();
272
                $options->setSerializer('igbinary');
273
274
                $plugin->setOptions($options);
275
            }
276
277
            $cache->addPlugin($plugin);
278
279
            return $cache;
280
        };
281
    }
282
283
    /**
284
     * Adds the Building mechanism using the key 'descriptor.builder'.
285
     *
286
     * Please note that the type of serializer can be configured using the parameter 'descriptor.builder.serializer'; it
287
     * accepts any parameter that Zend\Serializer supports.
288
     */
289
    protected function addBuilder(Application $app)
290
    {
291
        if (extension_loaded('igbinary')) {
292
            $app['descriptor.builder.serializer'] = 'IgBinary';
293
        } else {
294
            $app['descriptor.builder.serializer'] = 'PhpSerialize';
295
        }
296
297
        $app['descriptor.builder'] = function ($container) {
298
            $builder = new ProjectDescriptorBuilder(
299
                $container['descriptor.builder.assembler.factory'],
300
                $container['descriptor.filter']
301
            );
302
            $builder->setTranslator($container['translator']);
303
304
            return $builder;
305
        };
306
    }
307
308
    /**
309
     * Adds the assembler factory and attaches the basic assemblers with key 'descriptor.builder.assembler.factory'.
310
     */
311
    protected function addAssemblers(Application $app)
312
    {
313
        $app['descriptor.builder.assembler.factory'] = function () {
314
            return new AssemblerFactory();
315
        };
316
317
        $provider = $this;
318
        $app['descriptor.builder.assembler.factory'] = $app->extend(
319
            'descriptor.builder.assembler.factory',
320
            function ($factory) use ($provider, $app) {
321
                return $provider->attachAssemblersToFactory($factory, $app);
322
            }
323
        );
324
    }
325
326
    /**
327
     * Adds the descriptor filtering mechanism and using key 'descriptor.filter'.
328
     *
329
     * Please note that filters can only be attached after the builder is instantiated because it is needed; so the
330
     * filters can be attached by extending 'descriptor.builder'.
331
     */
332
    protected function addFilters(Application $app)
333
    {
334
        $app['descriptor.filter'] = function () {
335
            return new Filter(new ClassFactory());
336
        };
337
    }
338
}
339