Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

ComToLoc::getRatio()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Metrics;
6
7
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentStatisticsDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...ut\CommentStatisticsDTO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\ComToLocDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...\DTO\Output\ComToLocDTO was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use function round;
10
11
final class ComToLoc
12
{
13
    private bool $exceedThreshold = false;
14
15
    /**
16
     * @param array<string, float> $thresholds
17
     */
18
    public function __construct(private readonly array $thresholds) {}
19
20
    /**
21
     * @param CommentStatisticsDTO[] $commentStatistics
22
     */
23
    public function prepareComToLoc(array $commentStatistics, int $linesOfCode): ComToLocDTO
24
    {
25
        $ratio = $this->getRatio($commentStatistics, $linesOfCode);
26
27
        return new ComToLocDTO(
28
            $ratio,
29
            $this->getColorForRatio($ratio),
30
        );
31
    }
32
33
    public function hasExceededThreshold(): bool
34
    {
35
        return $this->exceedThreshold;
36
    }
37
38
    /**
39
     * @param CommentStatisticsDTO[] $commentStatistics
40
     */
41
    private function getRatio(array $commentStatistics, int $linesOfCode): float
42
    {
43
        if ($linesOfCode === 0) {
44
            return 0;
45
        }
46
47
        $totalComments = 0;
48
49
        foreach ($commentStatistics as $stat) {
50
            $totalComments += $stat->lines;
51
        }
52
53
        return round($totalComments / $linesOfCode, 2);
54
    }
55
56
    private function getColorForRatio(float $ratio): string
57
    {
58
        if (! isset($this->thresholds['Com/LoC'])) {
59
            return 'white';
60
        }
61
        if ($ratio >= $this->thresholds['Com/LoC']) {
62
            return 'green';
63
        }
64
        $this->exceedThreshold = true;
65
66
        return 'red';
67
    }
68
}
69