Issues (52)

src/AnalyzeComments/Metrics/ComToLoc.php (2 issues)

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