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