RunMetricsCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 6
c 8
b 3
f 1
lcom 0
cbo 12
dl 0
loc 116
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 58 1
B execute() 0 47 5
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Command;
11
use Hal\Application\Command\Job\QueueFactory;
12
use Hal\Application\Config\ConfigFactory;
13
use Hal\Component\Bounds\Bounds;
14
use Hal\Component\Evaluation\Evaluator;
15
use Hal\Component\File\Finder;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Command for run analysis
24
 *
25
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
26
 */
27
class RunMetricsCommand extends Command
28
{
29
    /**
30
     * @inheritdoc
31
     */
32
    protected function configure()
33
    {
34
        $this
35
                ->setName('metrics')
36
                ->setDescription('Run analysis')
37
                ->addArgument(
38
                    'path', InputArgument::OPTIONAL, 'Path to explore'
39
                )
40
                ->addOption(
41
                    'report-html',null, InputOption::VALUE_REQUIRED, 'Path to save report in HTML format. Example: /tmp/report.html'
42
                )
43
                ->addOption(
44
                    'report-xml', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in XML format. Example: /tmp/report.xml'
45
                )
46
                ->addOption(
47
                    'report-cli', null, InputOption::VALUE_NONE, 'Enable report in terminal'
48
                )
49
                ->addOption(
50
                    'violations-xml', null, InputOption::VALUE_REQUIRED, 'Path to save violations in XML format. Example: /tmp/report.xml'
51
                )
52
                ->addOption(
53
                    'report-csv', null, InputOption::VALUE_REQUIRED, 'Path to save summary report in CSV format. Example: /tmp/report.csv'
54
                )
55
                ->addOption(
56
                    'report-json', null, InputOption::VALUE_REQUIRED, 'Path to save detailed report in JSON format. Example: /tmp/report.json'
57
                )
58
                ->addOption(
59
                    'chart-bubbles', null, InputOption::VALUE_REQUIRED, 'Path to save Bubbles chart, in SVG format. Example: /tmp/chart.svg. Graphviz **IS** required'
60
                )
61
                ->addOption(
62
                    'level', null, InputOption::VALUE_REQUIRED, 'Depth of summary report', 0
63
                )
64
                ->addOption(
65
                    'extensions', null, InputOption::VALUE_REQUIRED, 'Regex of extensions to include', null
66
                )
67
                ->addOption(
68
                    'excluded-dirs', null, InputOption::VALUE_REQUIRED, 'Regex of subdirectories to exclude', null
69
                )
70
                ->addOption(
71
                    'symlinks', null, InputOption::VALUE_NONE, 'Enable following symlinks'
72
                )
73
                ->addOption(
74
                    'without-oop', null, InputOption::VALUE_NONE, 'If provided, tool will not extract any information about OOP model (faster)'
75
                )
76
                ->addOption(
77
                    'ignore-errors', null, InputOption::VALUE_NONE, 'If provided, files will be analyzed even with syntax errors'
78
                )
79
                ->addOption(
80
                    'failure-condition', null, InputOption::VALUE_REQUIRED, 'Optional failure condition, in english. For example: average.maintainabilityIndex < 50 or sum.loc > 10000', null
81
                )
82
                ->addOption(
83
                    'config', null, InputOption::VALUE_REQUIRED, 'Config file (YAML)', null
84
                )
85
                ->addOption(
86
                    'template-title', null, InputOption::VALUE_REQUIRED, 'Title for the HTML summary report', null
87
                )
88
        ;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    protected function execute(InputInterface $input, OutputInterface $output)
95
    {
96
97
        $output->writeln('PHPMetrics by Jean-François Lépine <https://twitter.com/Halleck45>');
98
        $output->writeln('');
99
100
        // config
101
        $configFactory = new ConfigFactory();
102
        $config = $configFactory->factory($input);
103
104
        // files
105
        if(null === $config->getPath()->getBasePath()) {
106
            throw new \LogicException('Please provide a path to analyze');
107
        }
108
109
        // files to analyze
110
        $finder = new Finder(
111
            $config->getPath()->getExtensions()
112
            , $config->getPath()->getExcludedDirs()
113
            , $config->getPath()->isFollowSymlinks() ? Finder::FOLLOW_SYMLINKS : null
114
        );
115
116
        // prepare structures
117
        $bounds = new Bounds();
118
        $collection = new \Hal\Component\Result\ResultCollection();
119
        $aggregatedResults = new \Hal\Component\Result\ResultCollection();
120
121
        // execute analyze
122
        $queueFactory = new QueueFactory($input, $output, $config);
123
        $queue = $queueFactory->factory($finder, $bounds);
124
        gc_disable();
125
        $queue->execute($collection, $aggregatedResults);
126
        gc_enable();
127
128
        $output->writeln('<info>done</info>');
129
130
        // evaluation of success
131
        $rule = $config->getFailureCondition();
132
        if(null !== $rule) {
133
            $evaluator = new Evaluator($collection, $aggregatedResults, $bounds);
134
            $result = $evaluator->evaluate($rule);
135
            // fail if failure-condition is realized
136
            return $result->isValid() ? 1 : 0;
137
        }
138
139
        return 0;
140
    }
141
142
}
143