-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Analyser.php
183 lines (156 loc) · 5.51 KB
/
Analyser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php declare(strict_types=1);
/*
* This file is part of PHPLOC.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\PHPLOC;
use function array_sum;
use function array_unique;
use function assert;
use function count;
use function dirname;
use function explode;
use function file_get_contents;
use function is_string;
use function max;
use function min;
use function sprintf;
use function substr_count;
use PhpParser\Error;
use PhpParser\Lexer;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use SebastianBergmann\Complexity\ComplexityCalculatingVisitor;
use SebastianBergmann\Complexity\ComplexityCollection;
use SebastianBergmann\LinesOfCode\LineCountingVisitor;
use SebastianBergmann\LinesOfCode\LinesOfCode;
final class Analyser
{
/**
* @psalm-param list<non-empty-string> $files
*/
public function analyse(array $files, bool $debug): Result
{
$errors = [];
$directories = [];
$complexity = ComplexityCollection::fromList();
/** @psalm-suppress MissingThrowsDocblock */
$linesOfCode = new LinesOfCode(0, 0, 0, 0);
foreach ($files as $file) {
if ($debug) {
print $file . PHP_EOL;
}
$directories[] = dirname($file);
try {
$result = $this->analyseFile($file);
$complexity = $complexity->mergeWith($result['complexity']);
$linesOfCode = $linesOfCode->plus($result['linesOfCode']);
} catch (ParserException $e) {
$message = $e->getMessage();
assert(is_string($message) && !empty($message));
$errors[] = $message;
}
}
$classesOrTraits = [];
foreach ($complexity->isMethod() as $item) {
$classesOrTraits[] = explode('::', $item->name())[0];
}
$classesOrTraits = count(array_unique($classesOrTraits));
$complexityFunctions = $complexity->isFunction();
$numberOfFunctions = $complexityFunctions->count();
$complexityFunctions = $this->cyclomaticComplexityStatistics($complexityFunctions);
$complexityMethods = $complexity->isMethod();
$numberOfMethods = $complexityMethods->count();
$complexityMethods = $this->cyclomaticComplexityStatistics($complexityMethods);
return new Result(
$errors,
count(array_unique($directories)),
count($files),
$linesOfCode->linesOfCode(),
$linesOfCode->commentLinesOfCode(),
$linesOfCode->nonCommentLinesOfCode(),
$linesOfCode->logicalLinesOfCode(),
$numberOfFunctions,
$complexityFunctions['minimum'],
$complexityFunctions['average'],
$complexityFunctions['maximum'],
$classesOrTraits,
$numberOfMethods,
$complexityMethods['minimum'],
$complexityMethods['average'],
$complexityMethods['maximum'],
);
}
/**
* @psalm-param non-empty-string $file
*
* @psalm-return array{complexity: ComplexityCollection, linesOfCode: LinesOfCode}
*
* @throws ParserException
*/
private function analyseFile(string $file): array
{
$parser = $this->parser();
$source = file_get_contents($file);
$lines = substr_count($source, "\n");
if ($lines === 0 && !empty($source)) {
$lines = 1;
}
assert($lines >= 0);
try {
$nodes = $parser->parse(file_get_contents($file));
assert($nodes !== null);
$traverser = new NodeTraverser;
$complexityCalculatingVisitor = new ComplexityCalculatingVisitor(false);
$lineCountingVisitor = new LineCountingVisitor($lines);
$traverser->addVisitor(new NameResolver);
$traverser->addVisitor(new ParentConnectingVisitor);
$traverser->addVisitor($complexityCalculatingVisitor);
$traverser->addVisitor($lineCountingVisitor);
$traverser->traverse($nodes);
} catch (Error $error) {
throw new ParserException(
sprintf(
'Cannot parse %s: %s',
$file,
$error->getMessage(),
),
$error->getCode(),
$error,
);
}
return [
'complexity' => $complexityCalculatingVisitor->result(),
'linesOfCode' => $lineCountingVisitor->result(),
];
}
private function parser(): Parser
{
return (new ParserFactory)->create(
ParserFactory::PREFER_PHP7,
new Lexer,
);
}
/**
* @psalm-return array{minimum: non-negative-int, maximum: non-negative-int, average: float}
*/
private function cyclomaticComplexityStatistics(ComplexityCollection $items): array
{
$values = [];
foreach ($items as $item) {
$values[] = $item->cyclomaticComplexity();
}
return [
'minimum' => !empty($values) ? min($values) : 0,
'maximum' => !empty($values) ? max($values) : 0,
'average' => !empty($values) ? array_sum($values) / count($values) : 0,
];
}
}