Passed
Push — main ( b13408...5e6da4 )
by mikhail
03:02
created

ComToLoc::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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