Passed
Push — main ( fcc13f...628042 )
by mikhail
02:55
created

CDS::getMinPossibleScore()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.9
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity;
6
7
use Mikhail\PrimitiveWrappers\Int\Integer;
8
use SavinMikhail\CommentsDensity\Comments\CommentFactory;
9
use SavinMikhail\CommentsDensity\Comments\DocBlockComment;
10
use SavinMikhail\CommentsDensity\DTO\Output\CdsDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDensity\DTO\Output\CdsDTO 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...
11
12
final class CDS
13
{
14
    private const MISSING_DOCBLOCK_WEIGHT = -1;
15
16
    private bool $exceedThreshold = false;
17
18 4
    public function __construct(
19
        private readonly array $thresholds,
20
        private readonly CommentFactory $commentFactory,
21
    ) {
22 4
    }
23
24 2
    public function calculateCDS(array $commentStatistics): float
25
    {
26 2
        $rawScore = $this->calculateRawScore($commentStatistics);
27 2
        $minPossibleScore = $this->getMinPossibleScore($commentStatistics);
28 2
        $maxPossibleScore = $this->getMaxPossibleScore($commentStatistics);
29
30
        try {
31 2
            return (new Integer(0))
32 2
                ->scaleToRange($rawScore, $minPossibleScore, $maxPossibleScore);
33
        } catch (\InvalidArgumentException) {
34
            return 0;
35
        }
36
    }
37
38 2
    private function calculateRawScore(array $commentStatistics): float
39
    {
40 2
        $rawScore = 0;
41
42 2
        foreach ($commentStatistics as $type => $count) {
43 2
            $comment = $this->commentFactory->getCommentType($type);
44 2
            if ($comment) {
45 2
                $rawScore += $count * $comment->getWeight();
46 2
                continue;
47
            }
48 2
            $rawScore += $count * self::MISSING_DOCBLOCK_WEIGHT;
49
        }
50
51 2
        return $rawScore;
52
    }
53
54 2
    private function getMinPossibleScore(array $commentStatistics): float
55
    {
56 2
        $minScore = 0;
57 2
        foreach ($commentStatistics as $type => $count) {
58 2
            $comment = $this->commentFactory->getCommentType($type);
59 2
            if (!$comment) {
60 2
                $minScore += self::MISSING_DOCBLOCK_WEIGHT * $count;
61 2
                continue;
62
            }
63 2
            if (in_array($comment->getAttitude(), ['bad', 'unwanted'])) {
64 2
                $minScore += $comment->getWeight() * $count;
65 2
                continue;
66
            }
67 2
            $minScore -= $comment->getWeight() * $count;
68
        }
69 2
        return $minScore;
70
    }
71
72 2
    private function getMaxPossibleScore(array $commentStatistics): float
73
    {
74 2
        return (
75 2
                ($commentStatistics['missingDocblock'] ?? 0)
76 2
                + ($commentStatistics['docblock'] ?? 0)
77 2
            )
78 2
            * ((new DocBlockComment())->getWeight());
79
    }
80
81 1
    public function prepareCDS(float $cds): CdsDTO
82
    {
83 1
        $cds = round($cds, 2);
84 1
        return new CdsDTO(
85 1
            $cds,
86 1
            $this->getColorForCDS($cds),
87 1
        );
88
    }
89
90 1
    private function getColorForCDS(float $cds): string
91
    {
92 1
        if (! isset($this->thresholds['CDS'])) {
93
            return 'white';
94
        }
95 1
        if ($cds >= $this->thresholds['CDS']) {
96
            return 'green';
97
        }
98 1
        $this->exceedThreshold = true;
99 1
        return 'red';
100
    }
101
102 1
    public function hasExceededThreshold(): bool
103
    {
104 1
        return $this->exceedThreshold;
105
    }
106
}
107