Passed
Push — master ( 830897...939e6f )
by Kyle
01:43 queued 35s
created

Command::run()   C

Complexity

Conditions 11
Paths 97

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
nc 97
nop 2
dl 0
loc 67
ccs 32
cts 32
cp 1
crap 11
rs 6.5733
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\TextUI;
19
20
use PHPMD\PHPMD;
21
use PHPMD\RuleSetFactory;
22
use PHPMD\Writer\StreamWriter;
23
24
/**
25
 * This class provides a command line interface for PHPMD
26
 */
27
class Command
28
{
29
    /**
30
     * Exit codes used by the phpmd command line tool.
31
     */
32
    const EXIT_SUCCESS = 0,
33
        EXIT_EXCEPTION = 1,
34
        EXIT_VIOLATION = 2,
35
        EXIT_ERROR = 3;
36
37
    /**
38
     * This method creates a PHPMD instance and configures this object based
39
     * on the user's input, then it starts the source analysis.
40
     *
41
     * The return value of this method can be used as an exit code. A value
42
     * equal to <b>EXIT_SUCCESS</b> means that no violations or errors were
43
     * found in the analyzed code. Otherwise this method will return a value
44
     * equal to <b>EXIT_VIOLATION</b> or <b>EXIT_ERROR</b> respectively.
45
     *
46
     * The use of the flags <b>--ignore-violations-on-exit</b> and
47
     * <b>--ignore-errors-on-exit</b> will result to a <b>EXIT_SUCCESS</b>
48
     * even if any violation or error is found.
49
     *
50
     * @param \PHPMD\TextUI\CommandLineOptions $opts
51
     * @param \PHPMD\RuleSetFactory $ruleSetFactory
52 14
     * @return integer
53
     */
54 14
    public function run(CommandLineOptions $opts, RuleSetFactory $ruleSetFactory)
55 1
    {
56 1
        if ($opts->hasVersion()) {
57
            fwrite(STDOUT, sprintf('PHPMD %s', $this->getVersion()) . PHP_EOL);
58
59
            return self::EXIT_SUCCESS;
60 13
        }
61
62
        // Create a report stream
63 13
        $stream = $opts->getReportFile() ? $opts->getReportFile() : STDOUT;
64 12
65
        // Create renderer and configure output
66 12
        $renderer = $opts->createRenderer();
67
        $renderer->setWriter(new StreamWriter($stream));
68 12
69 1
        $renderers = array($renderer);
70 1
71
        foreach ($opts->getReportFiles() as $reportFormat => $reportFile) {
72 1
            $reportRenderer = $opts->createRenderer($reportFormat);
73
            $reportRenderer->setWriter(new StreamWriter($reportFile));
74
75
            $renderers[] = $reportRenderer;
76 12
        }
77 12
78 12
        // Configure a rule set factory
79 1
        $ruleSetFactory->setMinimumPriority($opts->getMinimumPriority());
80
        $ruleSetFactory->setMaximumPriority($opts->getMaximumPriority());
81
        if ($opts->hasStrict()) {
82 12
            $ruleSetFactory->setStrict();
83 12
        }
84 12
85
        $phpmd = new PHPMD();
86 12
        $phpmd->setOptions(
87
            array_filter(
88
                array(
89
                    'coverage' => $opts->getCoverageReport(),
90
                )
91 12
            )
92 12
        );
93 1
94
        $extensions = $opts->getExtensions();
95
        if ($extensions !== null) {
96 12
            $phpmd->setFileExtensions(explode(',', $extensions));
97 12
        }
98 1
99
        $ignore = $opts->getIgnore();
100
        if ($ignore !== null) {
101 12
            $phpmd->addIgnorePatterns(explode(',', $ignore));
102 12
        }
103 12
104
        $phpmd->processFiles(
105
            $opts->getInputPath(),
106
            $opts->getRuleSets(),
107
            $renderers,
108 12
            $ruleSetFactory
109 6
        );
110
111 6
        if ($phpmd->hasErrors() && !$opts->ignoreErrorsOnExit()) {
112
            return self::EXIT_ERROR;
113
        }
114
115
        if ($phpmd->hasViolations() && !$opts->ignoreViolationsOnExit()) {
116
            return self::EXIT_VIOLATION;
117
        }
118
119 1
        return self::EXIT_SUCCESS;
120
    }
121 1
122
    /**
123 1
     * Returns the current version number.
124 1
     *
125 1
     * @return string
126 1
     */
127
    private function getVersion()
128 1
    {
129
        $build = __DIR__ . '/../../../../../build.properties';
130
131
        $version = '@package_version@';
132
        if (file_exists($build)) {
133
            $data = @parse_ini_file($build);
134
            $version = $data['project.version'];
135
        }
136
137
        return $version;
138 14
    }
139
140
    /**
141 14
     * The main method that can be used by a calling shell script, the return
142 14
     * value can be used as exit code.
143 14
     *
144
     * @param string[] $args The raw command line arguments array.
145 14
     * @return integer
146 1
     */
147 1
    public static function main(array $args)
148 1
    {
149
        try {
150 14
            $ruleSetFactory = new RuleSetFactory();
151
            $options = new CommandLineOptions($args, $ruleSetFactory->listAvailableRuleSets());
152
            $command = new Command();
153
154
            $exitCode = $command->run($options, $ruleSetFactory);
155
        } catch (\Exception $e) {
156
            fwrite(STDERR, $e->getMessage() . PHP_EOL);
157
            $exitCode = self::EXIT_EXCEPTION;
158
        }
159
160
        return $exitCode;
161
    }
162
}
163