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

ServiceProvider   C

Complexity

Total Complexity 13

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 27

Importance

Changes 0
Metric Value
dl 0
loc 170
rs 5
c 0
b 0
f 0
wmc 13
lcom 0
cbo 27

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 62 2
B validateDocBlocks() 0 34 5
A loadConfiguration() 0 11 1
B loadConfigurationByElement() 0 22 5
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 Cilex\ServiceProviderInterface;
16
use phpDocumentor\Fileset\Collection;
17
use phpDocumentor\Parser\Command\Project\ParseCommand;
18
use phpDocumentor\Parser\Middleware\CacheMiddleware;
19
use phpDocumentor\Parser\Middleware\ErrorHandlingMiddleware;
20
use phpDocumentor\Parser\Middleware\StopwatchMiddleware;
21
use phpDocumentor\Parser\Middleware\EmittingMiddleware;
22
use phpDocumentor\Plugin\Core\Descriptor\Validator\ValidatorAbstract;
23
use phpDocumentor\Reflection\DocBlockFactory;
24
use phpDocumentor\Reflection\Event\PostDocBlockExtractionEvent;
25
use phpDocumentor\Reflection\Php\Factory as Factory;
26
use phpDocumentor\Reflection\Php\NodesFactory;
27
use phpDocumentor\Reflection\Php\ProjectFactory;
28
use phpDocumentor\Reflection\PrettyPrinter;
29
use phpDocumentor\Translator\Translator;
30
use Stash\Driver\FileSystem;
31
use Stash\Pool;
32
33
/**
34
 * This provider is responsible for registering the parser component with the given Application.
35
 */
36
class ServiceProvider implements ServiceProviderInterface
0 ignored issues
show
Complexity introduced by
The class ServiceProvider has a coupling between objects value of 28. Consider to reduce the number of dependencies under 13.
Loading history...
37
{
38
    /**
39
     * Registers services on the given app.
40
     *
41
     * @param Application $app An Application instance
42
     *
43
     * @throws Exception\MissingDependencyException if the Descriptor Builder is not present.
44
     * @throws \Stash\Exception\RuntimeException
45
     *
46
     * @return void
47
     *
48
     */
49
    public function register(Application $app)
50
    {
51
        if (!isset($app['descriptor.builder'])) {
52
            throw new Exception\MissingDependencyException(
53
                'The builder object that is used to construct the ProjectDescriptor is missing'
54
            );
55
        }
56
57
        $app['parser'] = $app->share(
58
            function ($app) {
59
                $stopWatch = $app['kernel.stopwatch'];
60
61
                $adapter = new FileSystem(['path' => 'build/api-cache']);
62
                $cachePool = new Pool($adapter);
63
64
                $strategies = [
65
                    new Factory\Argument(new PrettyPrinter()),
66
                    new Factory\Class_(),
67
                    new Factory\Constant(new PrettyPrinter()),
68
                    new Factory\DocBlock(DocBlockFactory::createInstance()),
69
                    new Factory\Function_(),
70
                    new Factory\Interface_(),
71
                    new Factory\Method(),
72
                    new Factory\Property(new PrettyPrinter()),
73
                    new Factory\Trait_(),
74
                    new Factory\File(
75
                        NodesFactory::createInstance(),
76
                        [
77
                            new EmittingMiddleware(),
78
                            new StopwatchMiddleware(
79
                                $stopWatch
80
                            ),
81
                            new CacheMiddleware(
82
                                $cachePool
83
                            ),
84
                            new ErrorHandlingMiddleware()
85
                        ]
86
                    )
87
                ];
88
89
                $parser = new Parser(
90
                    new ProjectFactory($strategies),
91
                    $stopWatch
92
                );
93
94
                return $parser;
95
            }
96
        );
97
98
        $app['markdown'] = $app->share(
99
            function () {
100
                return \Parsedown::instance();
101
            }
102
        );
103
104
        /** @var Translator $translator  */
105
        $translator = $app['translator'];
106
        $translator->addTranslationFolder(__DIR__ . DIRECTORY_SEPARATOR . 'Messages');
107
108
        $app['parser.files'] = new Collection();
109
        $app->command(new ParseCommand($app['descriptor.builder'], $app['parser'], $translator, $app['parser.files']));
110
    }
111
112
    /**
113
     * Checks all phpDocumentor whether they match the given rules.
114
     *
115
     * @param PostDocBlockExtractionEvent $data Event object containing the parameters.
116
     *
117
     * @todo convert this method to the new style validators; this method is not invoked anymore
118
     *
119
     * @return void
120
     */
121
    public function validateDocBlocks($data)
122
    {
123
        /** @var \phpDocumentor\Reflection\BaseReflector $element */
124
        $element = $data->getSubject();
125
126
        /** @var \phpDocumentor\Reflection\DocBlock $docblock */
127
        $docblock = $data->getDocblock();
128
129
        // get the type of element
130
        $type = substr(
131
            get_class($element),
132
            strrpos(get_class($element), '\\') + 1,
133
            -9 // Reflector
134
        );
135
136
        // no docblock, or docblock should be ignored, so no reason to validate
137
        if ($docblock && $docblock->hasTag('ignore')) {
138
            return;
139
        }
140
141
        $validatorOptions = $this->loadConfiguration();
142
143
        foreach (array('Deprecated', 'Required', $type) as $validator) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
144
145
            // todo: move to a factory or builder class
146
            $class = 'phpDocumentor\Plugin\Core\Parser\DocBlock\Validator\\' . $validator . 'Validator';
147
            if (class_exists($class)) {
148
                /** @var ValidatorAbstract $val */
149
                $val = new $class($element->getName(), $docblock, $element);
150
                $val->setOptions($validatorOptions);
151
                $val->isValid($element);
152
            }
153
        }
154
    }
155
156
    /**
157
     * Load the configuration from the plugin.xml file
158
     *
159
     * @todo restore required/deprecated validators
160
     *
161
     * @return array
162
     */
163
    protected function loadConfiguration()
164
    {
165
        //$configOptions = $this->plugin->getOptions();
166
        $validatorOptions = array();
167
168
        //foreach (array('deprecated', 'required') as $tag) {
169
        //    $validatorOptions[$tag] = $this->loadConfigurationByElement($configOptions, $tag);
170
        //}
171
172
        return $validatorOptions;
173
    }
174
175
    /**
176
     * Load the configuration for given element (deprecated/required)
177
     *
178
     * @param array  $configOptions The configuration from the plugin.xml file
179
     * @param string $configType    Required/Deprecated for the time being
180
     *
181
     * @return array
182
     */
183
    protected function loadConfigurationByElement($configOptions, $configType)
184
    {
185
        $validatorOptions = array();
186
187
        if (isset($configOptions[$configType]->tag)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
188
189
            foreach ($configOptions[$configType]->tag as $tag) {
190
                $tagName = (string) $tag['name'];
191
192
                if (isset($tag->element)) {
193
                    foreach ($tag->element as $type) {
194
                        $typeName = (string) $type;
195
                        $validatorOptions[$typeName][] = $tagName;
196
                    }
197
                } else {
198
                    $validatorOptions['__ALL__'][] = $tagName;
199
                }
200
            }
201
        }
202
203
        return $validatorOptions;
204
    }
205
}
206