Test Failed
Push — main ( 27d416...4ff5eb )
by mikhail
03:21
created

MissingDocBlockAnalyzer::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock;
6
7
use PhpParser\NodeTraverser;
8
use PhpParser\NodeVisitor\NameResolver;
9
use PhpParser\ParserFactory;
10
use SavinMikhail\CommentsDensity\AnalyzeComments\Analyzer\DTO\Output\CommentDTO;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...r\DTO\Output\CommentDTO 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
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\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...
12
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\Checkers\NodeNeedsDocblockChecker;
0 ignored issues
show
Bug introduced by
The type SavinMikhail\CommentsDen...odeNeedsDocblockChecker 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...
13
use SavinMikhail\CommentsDensity\AnalyzeComments\MissingDocblock\Visitors\MissingDocBlockVisitor;
14
15
final class MissingDocBlockAnalyzer
16
{
17
    public const NAME = 'missingDocblock';
18
    public const COLOR = 'red';
19
20
    private bool $exceedThreshold = false;
21
22
    public function __construct(
23
        private readonly MissingDocblockConfigDTO $docblockConfigDTO,
24
    ) {}
25
26
    /**
27
     * Analyzes the AST of a file for missing docblocks.
28
     *
29
     * @param string $code the code to analyze
30
     * @param string $filename the filename of the code
31
     *
32
     * @return CommentDTO[] the analysis results
33
     */
34
    public function analyze(string $code, string $filename): array
35
    {
36
        $parser = (new ParserFactory())->createForHostVersion();
37
        $ast = $parser->parse($code);
38
        $traverser = new NodeTraverser();
39
40
        $nameResolver = new NameResolver();
41
        $traverser->addVisitor($nameResolver);
42
        $fqnNodes = $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

42
        $fqnNodes = $traverser->traverse(/** @scrutinizer ignore-type */ $ast);
Loading history...
43
44
        $visitor = new MissingDocBlockVisitor(
45
            $filename,
46
            new NodeNeedsDocblockChecker($this->docblockConfigDTO),
47
        );
48
        $traverser->removeVisitor($nameResolver);
49
        $traverser->addVisitor($visitor);
50
        $traverser->traverse($fqnNodes);
51
52
        return $visitor->missingDocBlocks;
53
    }
54
55
    /**
56
     * @return CommentDTO[]
57
     */
58
    public function getMissingDocblocks(string $code, string $filename): array
59
    {
60
        return $this->analyze($code, $filename);
61
    }
62
63
    public function getColor(): string
64
    {
65
        return self::COLOR;
66
    }
67
68
    /**
69
     * @param array<string, float> $thresholds
70
     */
71
    public function getStatColor(float $count, array $thresholds): string
72
    {
73
        if (!isset($thresholds['missingDocBlock'])) {
74
            return 'white';
75
        }
76
        if ($count <= $thresholds['missingDocBlock']) {
77
            return 'green';
78
        }
79
        $this->exceedThreshold = true;
80
81
        return 'red';
82
    }
83
84
    public function hasExceededThreshold(): bool
85
    {
86
        return $this->exceedThreshold;
87
    }
88
89
    public function getName(): string
90
    {
91
        return self::NAME;
92
    }
93
}
94