Passed
Push — main ( d846f8...8b63c1 )
by mikhail
04:31
created

ComToLoc::getRatio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\Metrics;
6
7
use SavinMikhail\CommentsDensity\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...
8
9
use function array_sum;
10
use function round;
11
12
final class ComToLoc
13
{
14
    private bool $exceedThreshold = false;
15
16 3
    public function __construct(private readonly array $thresholds)
17
    {
18 3
    }
19
20 1
    public function prepareComToLoc(array $commentStatistics, int $linesOfCode): ComToLocDTO
21
    {
22 1
        $ratio = $this->getRatio($commentStatistics, $linesOfCode);
23 1
        return new ComToLocDTO(
24 1
            $ratio,
25 1
            $this->getColorForRatio($ratio)
26 1
        );
27
    }
28
29 1
    public function hasExceededThreshold(): bool
30
    {
31 1
        return $this->exceedThreshold;
32
    }
33
34 1
    private function getRatio(array $commentStatistics, int $linesOfCode): float
35
    {
36 1
        $totalComments = array_sum($commentStatistics);
37 1
        return round($totalComments / $linesOfCode, 2);
38
    }
39
40 1
    private function getColorForRatio(float $ratio): string
41
    {
42 1
        if (! isset($this->thresholds['Com/LoC'])) {
43
            return 'white';
44
        }
45 1
        if ($ratio >= $this->thresholds['Com/LoC']) {
46 1
            return 'green';
47
        }
48
        $this->exceedThreshold = true;
49
        return 'red';
50
    }
51
}
52