Passed
Push — main ( 73419e...3a4be5 )
by mikhail
14:25
created

MissingDocBlockAnalyzer::analyzeTokens()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 2
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MissingDocBlockAnalyzer::getColor() 0 3 1
A MissingDocBlockAnalyzer::getMissingDocblocks() 0 3 1
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 23
    public function __construct(
17
        private readonly MissingDocblockConfigDTO $docblockConfigDTO,
18
    ) {
19 23
    }
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 23
    public function analyze(string $code, string $filename): array
29
    {
30 23
        $parser = (new ParserFactory())->createForHostVersion();
31 23
        $ast = $parser->parse($code);
32
33 23
        $traverser = new NodeTraverser();
34 23
        $visitor = new MissingDocBlockVisitor($filename, $this->docblockConfigDTO);
35
36 23
        $traverser->addVisitor(new NameResolver());
37 23
        $traverser->addVisitor($visitor);
38 23
        $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

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