Completed
Push — php7 ( ab40a7 )
by personal
04:06
created

DoAnalyze::execute()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 77
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 77
rs 6.5755
cc 7
eloc 46
nc 8
nop 2

How to fix   Long Method   

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
/*
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\Job;
11
use Hal\Application\Command\Job\Analyze\CardAndAgrestiAnalyzer;
12
use Hal\Application\Command\Job\Analyze\CouplingAnalyzer;
13
use Hal\Application\Command\Job\Analyze\FileAnalyzer;
14
use Hal\Application\Command\Job\Analyze\LcomAnalyzer;
15
use Hal\Component\Compatibility\PhpCompatibility;
16
use Hal\Component\File\Finder;
17
use Hal\Component\File\SyntaxChecker;
18
use Hal\Component\OOP\Extractor\ClassMap;
19
use Hal\Component\OOP\Extractor\Extractor;
20
use Hal\Component\Result\ResultCollection;
21
use Hal\Component\Token\Tokenizer;
22
use Symfony\Component\Console\Helper\ProgressBar;
23
use Symfony\Component\Console\Output\OutputInterface;
24
25
26
/**
27
 * Starts analyze
28
 *
29
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
30
 */
31
class DoAnalyze implements JobInterface
32
{
33
34
    /**
35
     * Path to analyze
36
     *
37
     * @var string
38
     */
39
    private $path;
40
41
    /**
42
     * Output
43
     *
44
     * @var \Symfony\Component\Console\Output\OutputInterface
45
     */
46
    private $output;
47
48
    /**
49
     * Finder
50
     *
51
     * @var Finder
52
     */
53
    private $finder;
54
55
    /**
56
     * do OOP analyze ?
57
     *
58
     * @var bool
59
     */
60
    private $withOOP;
61
62
    /**
63
     * Ignore errors ?
64
     * @var bool
65
     */
66
    private $ignoreErrors;
67
68
    /**
69
     * Constructor
70
     *
71
     * @param OutputInterface $output
72
     * @param Finder $finder
73
     * @param string $path
74
     * @param bool $withOOP
75
     * @param bool $ignoreErrors
76
     */
77
    public function __construct(OutputInterface $output, Finder $finder, $path, $withOOP, $ignoreErrors = false)
78
    {
79
        $this->output = $output;
80
        $this->finder = $finder;
81
        $this->path = $path;
82
        $this->withOOP = $withOOP;
83
        $this->ignoreErrors = $ignoreErrors;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function execute(ResultCollection $collection, ResultCollection $aggregatedResults) {
90
91
        $files = $this->finder->find($this->path);
92
93
        if(0 == sizeof($files, COUNT_NORMAL)) {
94
            throw new \LogicException('No file found');
95
        }
96
97
        $progress = new ProgressBar($this->output);
98
        $progress->start(sizeof($files, COUNT_NORMAL));
99
100
        // tools
101
        $classMap = new ClassMap();
102
        $tokenizer = new Tokenizer();
103
        $syntaxChecker = new SyntaxChecker();
104
105
        $fileAnalyzer = new FileAnalyzer(
106
            $this->output
107
            , $this->withOOP
108
            , new Extractor($tokenizer)
109
            , new \Hal\Metrics\Complexity\Text\Halstead\Halstead($tokenizer, new \Hal\Component\Token\TokenType())
110
            , new \Hal\Metrics\Complexity\Text\Length\Loc($tokenizer)
111
            , new \Hal\Metrics\Design\Component\MaintainabilityIndex\MaintainabilityIndex()
112
            , new \Hal\Metrics\Complexity\Component\McCabe\McCabe($tokenizer)
113
            , new \Hal\Metrics\Complexity\Component\Myer\Myer($tokenizer)
114
            , $classMap
115
        );
116
117
        foreach($files as $k => $filename) {
118
119
            $progress->advance();
120
121
            // Integrity
122
            if(!$this->ignoreErrors && !$syntaxChecker->isCorrect($filename)) {
123
                $this->output->writeln(sprintf('<error>file %s is not valid and has been skipped</error>', $filename));
124
                unset($files[$k]);
125
                continue;
126
            }
127
128
            // Analyze
129
            try {
130
                $resultSet = $fileAnalyzer->execute($filename);
131
            } catch(\Exception $e) {
132
                throw new \Exception(
133
                    (
134
                        sprintf("a '%s' exception occured analyzing file %s\nMessage: %s", get_class($e), $filename, $e->getMessage())
135
                        . sprintf("\n------------\nStack:\n%s", $e->getTraceAsString())
136
                        . sprintf("\n------------\nDo not hesitate to report a bug: https://github.com/Halleck45/PhpMetrics/issues")
137
                    )
138
                , 0, $e->getPrevious());
139
            }
140
141
            $collection->push($resultSet);
142
        }
143
144
        $progress->clear();
145
        $progress->finish();
146
147
148
        if($this->withOOP) {
149
            // COUPLING (should be done after parsing files)
150
            $this->output->write(str_pad("\x0DAnalyzing coupling. This will take few minutes...", 80, "\x20"));
151
            $couplingAnalyzer = new CouplingAnalyzer($classMap, $collection);
152
            $couplingAnalyzer->execute($files);
153
154
            // LCOM (should be done after parsing files)
155
            $this->output->write(str_pad("\x0DLack of cohesion of method (lcom). This will take few minutes...", 80, "\x20"));
156
            $lcomAnalyzer = new LcomAnalyzer($classMap, $collection);
157
            $lcomAnalyzer->execute($files);
158
159
            // Card and Agresti (should be done after parsing files)
160
            $this->output->write(str_pad("\x0DAnalyzing System complexity. This will take few minutes...", 80, "\x20"));
161
            $lcomAnalyzer = new CardAndAgrestiAnalyzer($classMap, $collection);
162
            $lcomAnalyzer->execute($files);
163
        }
164
165
    }
166
167
}
168