Skip to content

Commit 33fe782

Browse files
committed
Fix project mi metrics
1 parent 9ed87c6 commit 33fe782

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ based on [PHP Mess Detector](https://phpmd.org/) (PHPMD) tool.
131131
| dit | [Depth of Inheritance Tree](https://phpmd.org/rules/design.html#depthofinheritance) | | 6 | | |
132132
| hb | [Halstead Bugs](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 0.19 |
133133
| hd | [Halstead Difficulty](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 20 |
134-
| he | [Halstead Effort](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 5000 |
134+
| he | [Halstead Effort](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 8000 |
135135
| hi | [Halstead Intelligence Content](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | ||
136136
| hl | [Halstead Level](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | ||
137137
| hnd | [Halstead Vocabulary](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | ||
138138
| hnt | [Halstead Length](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | ||
139139
| ht | [Halstead Programming Time](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | ||
140-
| hv | [Halstead Volume](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 500 |
140+
| hv | [Halstead Volume](https://en.wikipedia.org/wiki/Halstead_complexity_measures) | | | | 700 |
141141
| lloc | Logical Lines Of Code || | | |
142142
| loc | [Lines Of Code](https://phpmd.org/rules/codesize.html#excessivemethodlength) | | 1000 | | 100 |
143143
| max mi | Maximum of [Maintainability Index](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?view=vs-2022) | 50 | | | |
@@ -149,7 +149,7 @@ based on [PHP Mess Detector](https://phpmd.org/) (PHPMD) tool.
149149
| nof | Number Of Functions || | | |
150150
| noi | Number Of Interfaces || | | |
151151
| nom | [Number Of Methods](https://phpmd.org/rules/codesize.html#toomanymethods) | | 25 | | |
152-
| npm | [Number of Public Methods](https://phpmd.org/rules/codesize.html#toomanypublicmethods) | | 10 | 10 | |
152+
| npm | [Number of Public Methods](https://phpmd.org/rules/codesize.html#toomanypublicmethods) | | 20 | 20 | |
153153
| npath | [NPath Complexity](https://phpmd.org/rules/codesize.html#npathcomplexity) | | | | 200 |
154154
| nop | Number of Packages (namespaces) || | | |
155155
| std mi | [Standard deviation](https://en.wikipedia.org/wiki/Standard_deviation) of [Maintainability Index](https://learn.microsoft.com/en-us/visualstudio/code-quality/code-metrics-maintainability-index-range-and-meaning?view=vs-2022) || | | |

config/pdepend-summary-formatter.yml.dist

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ colorizer:
3030

3131
# https://phpmd.org/rules/codesize.html#toomanypublicmethods
3232
npm:
33-
green: [ 0, 10 ]
34-
red+bold: [ 11, null ]
33+
green: [ 0, 20 ]
34+
red+bold: [ 21, null ]
3535

3636
# https://phpmd.org/rules/codesize.html#toomanyfields
3737
vars:
@@ -108,14 +108,14 @@ colorizer:
108108
# https://en.wikipedia.org/wiki/Halstead_complexity_measures
109109
hv:
110110
green: [ 0, 400 ]
111-
yellow+bold: [ 400, 500 ]
112-
red+bold: [ 500, null ]
111+
yellow+bold: [ 400, 700 ]
112+
red+bold: [ 700, null ]
113113

114114
# https://en.wikipedia.org/wiki/Halstead_complexity_measures
115115
he:
116116
green: [ 0, 4000 ]
117-
yellow+bold: [ 4000, 5000 ]
118-
red+bold: [ 5000, null ]
117+
yellow+bold: [ 4000, 8000 ]
118+
red+bold: [ 8000, null ]
119119

120120
# https://www.artima.com/weblogs/viewpost.jsp?thread=210575
121121
# https://stackoverflow.com/questions/4731774/how-to-read-improve-c-r-a-p-index-calculated-by-php

src/AndreyTech/Pdepend/Summary/Formatter/Parser/ProjectMiMetrics.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@
1313

1414
final class ProjectMiMetrics
1515
{
16-
public float $max = 100.0;
16+
public float $max = 0.0;
1717
public float $min = 0.0;
1818
public float $avg = 0.0;
1919
public float $std = 0.0;
20+
public int $total = 0;
2021

2122
/**
2223
* @var float[]
2324
*/
24-
public array $miList = [];
25+
private array $miList = [];
2526

2627
public function addMi(float $mi): void
2728
{
@@ -37,17 +38,17 @@ public function calculate(): void
3738
$this->min = min($this->miList);
3839
$this->max = max($this->miList);
3940

40-
$total = count($this->miList);
41+
$this->total = count($this->miList);
4142

42-
$this->avg = array_sum($this->miList) / $total;
43+
$this->avg = array_sum($this->miList) / $this->total;
4344

4445
$variance = array_reduce(
4546
$this->miList,
4647
fn (float $std, float $mi): float => $std + ($mi - $this->avg) ** 2,
4748
0.0
4849
);
4950

50-
$this->std = sqrt($variance / $total);
51+
$this->std = sqrt($variance / $this->total);
5152

5253
$this->miList = [];
5354
}

src/AndreyTech/Pdepend/Summary/Formatter/Renderer.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,11 @@ private function addMethodTableRow(Table $table, MethodMetrics $methodMetrics):
146146

147147
private function addProjectTableRow(Table $table, ProjectMetrics $projectMetrics): void
148148
{
149+
$projectMiMetrics = $this->buildProjectMiMetrics($projectMetrics);
150+
149151
$table->addRow([
150152
$projectMetrics->generated,
151-
$this->colorizer->colorizeMethodMetric('mi', round($projectMetrics->projectMiMetrics->min)),
152-
$this->colorizer->colorizeMethodMetric('mi', round($projectMetrics->projectMiMetrics->avg)),
153-
$this->colorizer->colorizeMethodMetric('mi', round($projectMetrics->projectMiMetrics->max)),
154-
$this->colorizer->colorizeMethodMetric('', round($projectMetrics->projectMiMetrics->std)),
153+
...$projectMiMetrics,
155154
$this->colorizer->colorizeMethodMetric('', $projectMetrics->noc),
156155
$this->colorizer->colorizeMethodMetric('', $projectMetrics->nom),
157156
$this->colorizer->colorizeMethodMetric('', $projectMetrics->noi),
@@ -163,6 +162,22 @@ private function addProjectTableRow(Table $table, ProjectMetrics $projectMetrics
163162
]);
164163
}
165164

165+
private function buildProjectMiMetrics(ProjectMetrics $projectMetrics): array
166+
{
167+
$mi = $projectMetrics->projectMiMetrics;
168+
169+
if (0 === $mi->total) {
170+
$miMin = $miAvg = $miMax = $miStd = '-';
171+
} else {
172+
$miMin = $this->colorizer->colorizeMethodMetric('mi', round($mi->min));
173+
$miAvg = $this->colorizer->colorizeMethodMetric('mi', round($mi->avg));
174+
$miMax = $this->colorizer->colorizeMethodMetric('mi', round($mi->max));
175+
$miStd = $this->colorizer->colorizeMethodMetric('', round($mi->std));
176+
}
177+
178+
return [ $miMin, $miAvg, $miMax, $miStd ];
179+
}
180+
166181
private function buildTitle(OutputInterface $output, string $file): void
167182
{
168183
$output->writeln(

0 commit comments

Comments
 (0)