Passed
Push — main ( 09e2a2...6a58fa )
by mikhail
13:23
created

MissingDocBlockAnalyzer::getStatColor()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock;
6
7
use PhpParser\NodeTraverser;
8
use PhpParser\ParserFactory;
9
use PhpParser\NodeVisitor\NameResolver;
10
use SavinMikhail\CommentsDensity\DTO\Input\MissingDocblockConfigDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...issingDocblockConfigDTO 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 MissingDocBlockAnalyzer
13
{
14
    private bool $exceedThreshold = false;
15
16 55
    public function __construct(
17
        private readonly MissingDocblockConfigDTO $docblockConfigDTO,
18
    ) {
19 55
    }
20
21
    /**
22
     * Analyzes the AST of a file for missing docblocks.
23
     *
24
     * @param string $code The code to analyze.
25
     * @param string $filename The filename of the code.
26
     * @return array The analysis results.
27
     */
28 51
    public function analyze(string $code, string $filename): array
29
    {
30 51
        $parser = (new ParserFactory())->createForHostVersion();
31 51
        $ast = $parser->parse($code);
32
33 51
        $traverser = new NodeTraverser();
34 51
        $visitor = new MissingDocBlockVisitor(
35 51
            $filename,
36 51
            new DocBlockChecker($this->docblockConfigDTO, new MethodAnalyzer())
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...Docblock\MethodAnalyzer 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...
37 51
        );
38
39 51
        $traverser->addVisitor(new NameResolver());
40 51
        $traverser->addVisitor($visitor);
41 51
        $traverser->traverse($ast);
0 ignored issues
show
Bug introduced by
It seems like $ast can also be of type null; however, parameter $nodes of PhpParser\NodeTraverser::traverse() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $traverser->traverse(/** @scrutinizer ignore-type */ $ast);
Loading history...
42
43 51
        return $visitor->missingDocBlocks;
44
    }
45
46 51
    public function getMissingDocblocks(string $code, string $filename): array
47
    {
48 51
        return $this->analyze($code, $filename);
49
    }
50
51 1
    public function getColor(): string
52
    {
53 1
        return 'red';
54
    }
55
56 2
    public function getStatColor(float $count, array $thresholds): string
57
    {
58 2
        if (!isset($thresholds['missingDocBlock'])) {
59 1
            return 'white';
60
        }
61 2
        if ($count <= $thresholds['missingDocBlock']) {
62 1
            return 'green';
63
        }
64 2
        $this->exceedThreshold = true;
65 2
        return 'red';
66
    }
67
68 2
    public function hasExceededThreshold(): bool
69
    {
70 2
        return $this->exceedThreshold;
71
    }
72
73 1
    public function getName(): string
74
    {
75 1
        return 'missingDocblock';
76
    }
77
}
78