CLIController::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 10
dl 0
loc 17
rs 9.8666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Cli controller
5
 *
6
 * PHP Version 5.3.2
7
 *
8
 * Copyright (c) 2007-2010, Mayflower GmbH
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   * Redistributions of source code must retain the above copyright
16
 *     notice, this list of conditions and the following disclaimer.
17
 *
18
 *   * Redistributions in binary form must reproduce the above copyright
19
 *     notice, this list of conditions and the following disclaimer in
20
 *     the documentation and/or other materials provided with the
21
 *     distribution.
22
 *
23
 *   * Neither the name of Mayflower GmbH nor the names of his
24
 *     contributors may be used to endorse or promote products derived
25
 *     from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
 * POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category PHP_CodeBrowser
41
 *
42
 * @author Elger Thiele <[email protected]>
43
 * @author Simon Kohlmeyer <[email protected]>
44
 *
45
 * @copyright 2007-2010 Mayflower GmbH
46
 *
47
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
48
 *
49
 * @version SVN: $Id$
50
 *
51
 * @link http://www.phpunit.de/
52
 *
53
 * @since File available since  0.1.0
54
 */
55
56
namespace PHPCodeBrowser;
57
58
use Monolog\Logger;
59
use PHPCodeBrowser\Helper\IOHelper;
60
use PHPCodeBrowser\View\ViewReview;
61
use SebastianBergmann\FileIterator\Factory as FileIteratorFactory;
62
63
/**
64
 * CLIController
65
 *
66
 * @category PHP_CodeBrowser
67
 *
68
 * @author Elger Thiele <[email protected]>
69
 * @author Michel Hartmann <[email protected]>
70
 * @author Simon Kohlmeyer <[email protected]>
71
 *
72
 * @copyright 2007-2010 Mayflower GmbH
73
 *
74
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
75
 *
76
 * @version Release: @package_version@
77
 *
78
 * @link http://www.phpunit.de/
79
 *
80
 * @since Class available since  0.1.0
81
 */
82
class CLIController
83
{
84
    /**
85
     * Path to the Cruise Control input xml file
86
     *
87
     * @var string|null
88
     */
89
    private $logDir;
90
91
    /**
92
     * Path to the code browser html output folder
93
     *
94
     * @var string
95
     */
96
    private $htmlOutputDir;
97
98
    /**
99
     * Path to the project source code files
100
     *
101
     * @var array|null
102
     */
103
    private $projectSource;
104
105
    /**
106
     * array of PCREs. Matching files will not appear in the output.
107
     *
108
     * @var array
109
     */
110
    private $excludeExpressions;
111
112
    /**
113
     * array of glob patterns. Matching files will not appear in the output.
114
     *
115
     * @var array
116
     */
117
    private $excludePatterns;
118
119
    /**
120
     * The error plugin classes
121
     *
122
     * @var array
123
     */
124
    private $registeredPlugins;
125
126
    /**
127
     * The IOHelper used for filesystem interaction.
128
     *
129
     * @var IOHelper
130
     */
131
    private $ioHelper;
132
133
    /**
134
     * Pear Log object where debug output should go to.
135
     *
136
     * @var Logger
137
     */
138
    private $debugLog;
139
140
    /**
141
     * Plugin-specific options. Formatted like
142
     *  array(
143
     *      'ErrorCRAP' => array(
144
     *          'threshold' => 2
145
     *      )
146
     *  )
147
     *
148
     * @var array
149
     */
150
    private $pluginOptions = [];
151
152
    /**
153
     * File extensions that we take as php files.
154
     *
155
     * @var array
156
     */
157
    private $phpSuffixes;
158
159
    /**
160
     * We want to exclude files with no issues
161
     *
162
     * @var bool
163
     */
164
    private $excludeOK;
165
166
    /**
167
     * The constructor
168
     *
169
     * Standard setters are initialized
170
     *
171
     * @param string|null $logPath            The (path-to) xml log files. Can be null.
172
     * @param array|null  $projectSource      The project sources. Can be null.
173
     * @param string      $htmlOutputDir      The html output dir, where new files will be created
174
     * @param array       $excludeExpressions A list of PCREs. Files matching will not appear in the output.
175
     * @param array       $excludePatterns    A list of glob patterns. Files matching will not appear in the output.
176
     * @param array       $pluginOptions      array of arrays with plugin-specific options
177
     * @param IOHelper    $ioHelper           The IOHelper object to be used for filesystem interaction.
178
     * @param Logger      $debugLog
179
     * @param array       $phpSuffixes
180
     * @param bool        $excludeOK
181
     */
182
    public function __construct(?string $logPath, ?array $projectSource, string $htmlOutputDir, array $excludeExpressions, array $excludePatterns, array $pluginOptions, IOHelper $ioHelper, Logger $debugLog, array $phpSuffixes, bool $excludeOK = false)
183
    {
184
        $this->logDir             = $logPath;
185
        $this->projectSource      = $projectSource;
186
        $this->htmlOutputDir      = $htmlOutputDir;
187
        $this->excludeExpressions = $excludeExpressions;
188
        $this->excludePatterns    = $excludePatterns;
189
190
        foreach ($pluginOptions as $plugin => $options) {
191
            $this->pluginOptions["Error{$plugin}"] = $options;
192
        }
193
194
        $this->ioHelper          = $ioHelper;
195
        $this->debugLog          = $debugLog;
196
        $this->registeredPlugins = [];
197
        $this->phpSuffixes       = $phpSuffixes;
198
        $this->excludeOK         = $excludeOK;
199
    }
200
201
    /**
202
     * Setter/adder method for the used plugin classes.
203
     * For each plugin to use, add it to this array
204
     *
205
     * @param mixed $classNames Definition of plugin classes
206
     *
207
     * @return void
208
     */
209
    public function addErrorPlugins($classNames): void
210
    {
211
        foreach ((array) $classNames as $className) {
212
            $this->registeredPlugins[] = $className;
213
        }
214
    }
215
216
    /**
217
     * Main execute function for PHP_CodeBrowser.
218
     *
219
     * Following steps are resolved:
220
     * 1. Clean-up output directory
221
     * 2. Merge xml log files
222
     * 3. Generate XML file via error list from plugins
223
     * 4. Save the ErrorList as XML file
224
     * 5. Generate HTML output from XML
225
     * 6. Copy resources (css, js, images) from template directory to output
226
     *
227
     * @return void
228
     */
229
    public function run(): void
230
    {
231
        // clear and create output directory
232
        if (\is_dir($this->htmlOutputDir)) {
233
            $this->ioHelper->deleteDirectory($this->htmlOutputDir);
234
        } elseif (\is_file($this->htmlOutputDir)) {
235
            $this->ioHelper->deleteFile($this->htmlOutputDir);
236
        }
237
238
        $this->ioHelper->createDirectory($this->htmlOutputDir);
239
240
        // init needed classes
241
        $viewReview = new ViewReview(
242
            \getenv('PHPCB_TEMPLATE_DIR') ? \getenv('PHPCB_TEMPLATE_DIR') : \dirname(__FILE__, 3).'/templates',
243
            $this->htmlOutputDir,
244
            $this->ioHelper,
245
            $this->phpSuffixes
246
        );
247
248
        $sourceHandler = new SourceHandler($this->debugLog);
249
250
        if (null !== $this->logDir) {
251
            $issueXml = new IssueXML();
252
253
            // merge xml files
254
            $issueXml->addDirectory($this->logDir);
255
256
            // conversion of XML file cc to cb format
257
            foreach ($this->registeredPlugins as $className) {
258
                /** @var AbstractPlugin $plugin */
259
                $plugin = \array_key_exists($className, $this->pluginOptions) ? new $className(
260
                    $issueXml,
261
                    $this->pluginOptions[$className]
262
                ) : new $className($issueXml);
263
                $sourceHandler->addPlugin($plugin);
264
            }
265
        }
266
267
        if (null !== $this->projectSource) {
268
            foreach ($this->projectSource as $source) {
269
                if (\is_dir($source)) {
270
                    $factory = new FileIteratorFactory();
271
272
                    $suffixes = \array_merge(
273
                        $this->phpSuffixes,
274
                        ['php', 'js', 'css', 'html']
275
                    );
276
277
                    $sourceHandler->addSourceFiles(
278
                        $factory->getFileIterator(
279
                            $source,
280
                            $suffixes
281
                        )
282
                    );
283
                } else {
284
                    $sourceHandler->addSourceFile($source);
285
                }
286
            }
287
        }
288
289
        \array_walk(
290
            $this->excludeExpressions,
291
            [$sourceHandler, 'excludeMatchingPCRE']
292
        );
293
        \array_walk(
294
            $this->excludePatterns,
295
            [$sourceHandler, 'excludeMatchingPattern']
296
        );
297
298
        $files = $sourceHandler->getFiles();
299
300
        if (!$files) {
301
            $viewReview->copyNoErrorsIndex();
302
        } else {
303
            // Get the path prefix all files have in common
304
            $commonPathPrefix = $sourceHandler->getCommonPathPrefix();
305
306
            foreach ($files as $file) {
307
                $viewReview->generate(
308
                    $file->getIssues(),
309
                    $file->name(),
310
                    $commonPathPrefix,
311
                    $this->excludeOK
312
                );
313
            }
314
315
            // Copy needed resources (eg js libraries) to output directory
316
            $viewReview->copyResourceFolders();
317
            $viewReview->generateIndex($files, $this->excludeOK);
318
        }
319
    }
320
}
321