Completed
Push — develop ( 3a2f11...a89061 )
by Jaap
04:16
created

Transform::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 48
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 1
dl 0
loc 48
ccs 0
cts 19
cp 0
crap 12
rs 9.125
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 *  For the full copyright and license information, please view the LICENSE
6
 *  file that was distributed with this source code.
7
 *
8
 *  @copyright 2010-2017 Mike van Riel<[email protected]>
9
 *  @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 *  @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application\Stage;
14
15
use phpDocumentor\Compiler\Compiler;
16
use phpDocumentor\Compiler\CompilerPassInterface;
17
use phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper;
18
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
19
use phpDocumentor\Event\Dispatcher;
20
use phpDocumentor\Transformer\Event\PreTransformationEvent;
21
use phpDocumentor\Transformer\Event\PreTransformEvent;
22
use phpDocumentor\Transformer\Event\WriterInitializationEvent;
23
use phpDocumentor\Transformer\Transformer;
24
use Psr\Log\LoggerInterface;
25
use Symfony\Component\Stopwatch\Stopwatch;
26
use Zend\Cache\Storage\StorageInterface;
27
28
/**
29
 * Transforms the structure file into the specified output format
30
 *
31
 * This task will execute the transformation rules described in the given
32
 * template (defaults to 'responsive') with the given source (defaults to
33
 * output/structure.xml) and writes these to the target location (defaults to
34
 * 'output').
35
 *
36
 * It is possible for the user to receive additional information using the
37
 * verbose option or stop additional information using the quiet option. Please
38
 * take note that the quiet option also disables logging to file.
39
 */
40
final class Transform
41
{
42
    /** @var ProjectDescriptorBuilder $builder Object containing the project meta-data and AST */
43
    private $builder;
44
45
    /** @var Transformer $transformer Principal object for guiding the transformation process */
46
    private $transformer;
47
48
    /** @var Compiler $compiler Collection of pre-transformation actions (Compiler Passes) */
49
    private $compiler;
50
51
    /**
52
     * @var StorageInterface
53
     */
54
    private $cache;
55
56
    private $logger;
57
58
    /**
59
     * Initializes the command with all necessary dependencies to construct human-suitable output from the AST.
60
     */
61
    public function __construct(
62
        ProjectDescriptorBuilder $builder,
63
        Transformer $transformer,
64
        Compiler $compiler,
65
        StorageInterface $cache,
66
        LoggerInterface $logger
67
    ) {
68
        $this->builder = $builder;
69
        $this->transformer = $transformer;
70
        $this->compiler = $compiler;
71
        $this->cache = $cache;
72
        $this->logger = $logger;
73
74
        $this->connectOutputToEvents();
75
    }
76
77
    /**
78
     * Returns the builder object containing the AST and other meta-data.
79
     *
80
     * @return ProjectDescriptorBuilder
81
     */
82
    private function getBuilder()
83
    {
84
        return $this->builder;
85
    }
86
87
    /**
88
     * Returns the transformer used to guide the transformation process from AST to output.
89
     *
90
     * @return Transformer
91
     */
92
    private function getTransformer()
93
    {
94
        return $this->transformer;
95
    }
96
97
    /**
98
     * Executes the business logic involved with this command.
99
     *
100
     * @return integer
101
     * @throws \Exception if the target location is not a folder.
102
     */
103
    public function __invoke(array $configuration)
104
    {
105
        // initialize transformer
106
        $transformer = $this->getTransformer();
0 ignored issues
show
Unused Code introduced by Jaapio
$transformer 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...
107
108
        $projectDescriptor = $this->getBuilder()->getProjectDescriptor();
109
        $mapper = new ProjectDescriptorMapper($this->getCache());
110
111
        $stopWatch = new Stopwatch();
112
        $stopWatch->start('cache');
113
        $mapper->populate($projectDescriptor);
114
        $stopWatch->stop('cache');
115
116
        foreach (array_column($configuration['phpdocumentor']['templates'], 'name') as $template) {
117
            $stopWatch->start('load template');
118
            $this->transformer->getTemplates()->load($template);
119
//            $output->writeTimedLog(
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
60% 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...
120
//                'Preparing template "'. $template .'"',
121
//                array($transformer->getTemplates(), 'load'),
122
//                array($template, $transformer)
123
//            );
124
            $stopWatch->stop('load template');
125
        }
126
127
//        $output->writeTimedLog(
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
59% 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...
128
//            'Preparing ' . count($transformer->getTemplates()->getTransformations()) . ' transformations',
129
//            array($this, 'loadTransformations'),
130
//            array($transformer)
131
//        );
132
133
        //$this->loadTransformations($transformer);
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
86% 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...
134
135
//        if ($progress) {
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
65% 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...
136
//            $progress->start($output, count($transformer->getTemplates()->getTransformations()));
137
//        }
138
139
        /** @var CompilerPassInterface $pass */
140
        foreach ($this->compiler as $pass) {
141
            $pass->execute($projectDescriptor);
142
            //$output->writeTimedLog($pass->getDescription(), array($pass, 'execute'), array($projectDescriptor));
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
81% 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...
143
        }
144
145
//        if ($progress) {
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
58% 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...
146
//            $progress->finish();
147
//        }
148
149
        return 0;
150
    }
151
152
    /**
153
     * Returns the Cache.
154
     */
155
    private function getCache(): StorageInterface
156
    {
157
        return $this->cache;
158
    }
159
160
    /**
161
     * Connect a series of output messages to various events to display progress.
162
     */
163
    private function connectOutputToEvents()
164
    {
165
        Dispatcher::getInstance()->addListener(
166
            Transformer::EVENT_PRE_TRANSFORM,
167
            function (PreTransformEvent $event) {
168
                /** @var Transformer $transformer */
169
                $transformer = $event->getSubject();
170
                $templates = $transformer->getTemplates();
171
                $transformations = $templates->getTransformations();
172
                $this->logger->info(sprintf("\nApplying %d transformations", count($transformations)));
173
            }
174
        );
175
        Dispatcher::getInstance()->addListener(
176
            Transformer::EVENT_PRE_INITIALIZATION,
177
            function (WriterInitializationEvent $event) {
178
                $this->logger->info('  Initialize writer "' . get_class($event->getWriter()) . '"');
179
            }
180
        );
181
        Dispatcher::getInstance()->addListener(
182
            Transformer::EVENT_PRE_TRANSFORMATION,
183
            function (PreTransformationEvent $event) {
184
                $this->logger->info(
185
                    '  Execute transformation using writer "' . $event->getTransformation()->getWriter() . '"'
186
                );
187
            }
188
        );
189
    }
190
}
191