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

MissingDocBlockAnalyzer::getColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity;
6
7
use function in_array;
8
use function is_array;
9
10
use const T_CLASS;
11
use const T_DOC_COMMENT;
12
use const T_FUNCTION;
13
use const T_INTERFACE;
14
15
final class MissingDocBlockAnalyzer
16
{
17
    private bool $exceedThreshold = true;
18
19
    /**
20
     * Analyzes the tokens of a file for docblocks.
21
     *
22
     * @param array $tokens The tokens to analyze.
23
     * @return array The analysis results.
24
     */
25 1
    private function analyzeTokens(array $tokens, string $filename): array
26
    {
27 1
        $lastDocBlock = null;
28 1
        $missingDocBlocks = [];
29 1
        $tokenCount = count($tokens);
30
31 1
        for ($i = 0; $i < $tokenCount; $i++) {
32 1
            $token = $tokens[$i];
33
34 1
            if (!is_array($token)) {
35
                continue;
36
            }
37
38 1
            if ($token[0] === T_DOC_COMMENT) {
39 1
                $lastDocBlock = $token[1];
40 1
            } elseif (in_array($token[0], [T_CLASS, T_TRAIT, T_INTERFACE, T_ENUM, T_FUNCTION], true)) {
41
                if (empty($lastDocBlock)) {
42
                    $missingDocBlocks[] = [
43
                        'type' => 'missingDocblock',
44
                        'content' => '',
45
                        'file' => $filename,
46
                        'line' => $token[2]
47
                    ];
48
                }
49
                $lastDocBlock = null;
50
            }
51
        }
52
53 1
        return $missingDocBlocks;
54
    }
55
56 1
    public function getMissingDocblocks(array $tokens, string $filename): array
57
    {
58 1
        return $this->analyzeTokens($tokens, $filename);
59
    }
60
61 1
    public function getColor(): string
62
    {
63 1
        return 'red';
64
    }
65
66 1
    public function getStatColor(float $count, array $thresholds): string
67
    {
68 1
        if (! isset($thresholds['missingDocBlock'])) {
69 1
            return 'white';
70
        }
71
        if ($count <= $thresholds['missingDocBlock']) {
72
            return 'green';
73
        }
74
        $this->exceedThreshold = true;
75
        return 'red';
76
    }
77
78 1
    public function hasExceededThreshold(): bool
79
    {
80 1
        return $this->exceedThreshold;
81
    }
82
83 1
    public function getName(): string
84
    {
85 1
        return 'missingDocblock';
86
    }
87
}
88