Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

src/phpDocumentor/Parser/ServiceProvider.php (2 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-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\Parser;
13
14
use Cilex\Application;
15
use League\Flysystem\MountManager;
16
use phpDocumentor\Infrastructure\FlySystemFactory;
17
use phpDocumentor\Infrastructure\Parser\FlySystemCollector;
18
use phpDocumentor\Infrastructure\Parser\SpecificationFactory;
19
use Pimple\Container;
20
use Pimple\ServiceProviderInterface;
21
use phpDocumentor\Fileset\Collection;
22
use phpDocumentor\Parser\Command\Project\ParseCommand;
23
use phpDocumentor\Parser\Middleware\CacheMiddleware;
24
use phpDocumentor\Parser\Middleware\ErrorHandlingMiddleware;
25
use phpDocumentor\Parser\Middleware\StopwatchMiddleware;
26
use phpDocumentor\Parser\Middleware\EmittingMiddleware;
27
use phpDocumentor\Plugin\Core\Descriptor\Validator\ValidatorAbstract;
28
use phpDocumentor\Reflection\DocBlockFactory;
29
use phpDocumentor\Reflection\Php\Factory;
30
use phpDocumentor\Reflection\Php\NodesFactory;
31
use phpDocumentor\Reflection\Php\ProjectFactory;
32
use phpDocumentor\Reflection\PrettyPrinter;
33
use phpDocumentor\Translator\Translator;
34
use Stash\Driver\FileSystem;
35
use Stash\Pool;
36
37
/**
38
 * This provider is responsible for registering the parser component with the given Application.
39
 */
40
class ServiceProvider implements ServiceProviderInterface
41
{
42
    /**
43
     * Registers services on the given app.
44
     *
45
     * @param Container|Application $app An Application instance
46
     *
47
     * @throws Exception\MissingDependencyException if the Descriptor Builder is not present.
48
     * @throws \Stash\Exception\RuntimeException
49
     *
50
     * @return void
51
     *
52
     */
53
    public function register(Container $app)
54
    {
55
        if (!isset($app['descriptor.builder'])) {
56
            throw new Exception\MissingDependencyException(
57
                'The builder object that is used to construct the ProjectDescriptor is missing'
58
            );
59
        }
60
61
        $app['parser.middleware.cache'] = function () {
62
                return new CacheMiddleware(
63
                    new Pool(new FileSystem(['path' => 'build/api-cache']))
64
                );
65
            };
0 ignored issues
show
Closing brace indented incorrectly; expected 8 spaces, found 12
Loading history...
66
67
        $app['parser'] = function ($app) {
68
            $stopWatch = $app['kernel.stopwatch'];
69
70
            $adapter = new FileSystem(['path' => 'build/api-cache']);
71
            $cachePool = new Pool($adapter);
0 ignored issues
show
$cachePool is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
73
            $strategies = [
74
                new Factory\Argument(new PrettyPrinter()),
75
                new Factory\Class_(),
76
                new Factory\Constant(new PrettyPrinter()),
77
                new Factory\DocBlock(DocBlockFactory::createInstance()),
78
                new Factory\Function_(),
79
                new Factory\Interface_(),
80
                new Factory\Method(),
81
                new Factory\Property(new PrettyPrinter()),
82
                new Factory\Trait_(),
83
                new Factory\File(
84
                    NodesFactory::createInstance(),
85
                    [
86
                        new StopwatchMiddleware(
87
                            $stopWatch
88
                        ),
89
                        $app['parser.middleware.cache'],
90
                        new EmittingMiddleware(),
91
                        new ErrorHandlingMiddleware()
92
                    ]
93
                )
94
            ];
95
96
            $parser = new Parser(
97
                new ProjectFactory($strategies),
98
                $stopWatch
99
            );
100
101
            return $parser;
102
        };
103
104
        /** @var Translator $translator */
105
        $translator = $app['translator'];
106
        $translator->addTranslationFolder(__DIR__ . DIRECTORY_SEPARATOR . 'Messages');
107
108
        $fileCollector = new FlySystemCollector(
109
            new SpecificationFactory(),
110
            new FlySystemFactory(new MountManager())
111
        );
112
113
        $app->command(
114
            new ParseCommand(
115
                $app['descriptor.builder'],
116
                $app['parser'],
117
                $fileCollector,
118
                $translator,
119
                $app['descriptor.cache'],
120
                $app['parser.example.finder'],
121
                $app['partials']
122
            )
123
        );
124
    }
125
126
    /**
127
     * Checks all phpDocumentor whether they match the given rules.
128
     *
129
     * @param PostDocBlockExtractionEvent $data Event object containing the parameters.
130
     *
131
     * @todo convert this method to the new style validators; this method is not invoked anymore
132
     *
133
     * @return void
134
     */
135
    public function validateDocBlocks($data)
136
    {
137
        /** @var \phpDocumentor\Reflection\BaseReflector $element */
138
        $element = $data->getSubject();
139
140
        /** @var \phpDocumentor\Reflection\DocBlock $docblock */
141
        $docblock = $data->getDocblock();
142
143
        // get the type of element
144
        $type = substr(
145
            get_class($element),
146
            strrpos(get_class($element), '\\') + 1,
147
            -9 // Reflector
148
        );
149
150
        // no docblock, or docblock should be ignored, so no reason to validate
151
        if ($docblock && $docblock->hasTag('ignore')) {
152
            return;
153
        }
154
155
        $validatorOptions = $this->loadConfiguration();
156
157
        foreach (array('Deprecated', 'Required', $type) as $validator) {
158
159
            // todo: move to a factory or builder class
160
            $class = 'phpDocumentor\Plugin\Core\Parser\DocBlock\Validator\\' . $validator . 'Validator';
161
            if (class_exists($class)) {
162
                /** @var ValidatorAbstract $val */
163
                $val = new $class($element->getName(), $docblock, $element);
164
                $val->setOptions($validatorOptions);
165
                $val->isValid($element);
166
            }
167
        }
168
    }
169
170
    /**
171
     * Load the configuration from the plugin.xml file
172
     *
173
     * @todo restore required/deprecated validators
174
     *
175
     * @return array
176
     */
177
    protected function loadConfiguration()
178
    {
179
        //$configOptions = $this->plugin->getOptions();
180
        $validatorOptions = array();
181
182
        //foreach (array('deprecated', 'required') as $tag) {
183
        //    $validatorOptions[$tag] = $this->loadConfigurationByElement($configOptions, $tag);
184
        //}
185
186
        return $validatorOptions;
187
    }
188
189
    /**
190
     * Load the configuration for given element (deprecated/required)
191
     *
192
     * @param array  $configOptions The configuration from the plugin.xml file
193
     * @param string $configType    Required/Deprecated for the time being
194
     *
195
     * @return array
196
     */
197
    protected function loadConfigurationByElement($configOptions, $configType)
198
    {
199
        $validatorOptions = array();
200
201
        if (isset($configOptions[$configType]->tag)) {
202
203
            foreach ($configOptions[$configType]->tag as $tag) {
204
                $tagName = (string)$tag['name'];
205
206
                if (isset($tag->element)) {
207
                    foreach ($tag->element as $type) {
208
                        $typeName = (string)$type;
209
                        $validatorOptions[$typeName][] = $tagName;
210
                    }
211
                } else {
212
                    $validatorOptions['__ALL__'][] = $tagName;
213
                }
214
            }
215
        }
216
217
        return $validatorOptions;
218
    }
219
}
220