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

MissingDocBlockVisitor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 62
ccs 31
cts 31
cp 1
rs 10
wmc 21

3 Methods

Rating   Name   Duplication   Size   Complexity  
C requiresDocBlock() 0 35 17
A __construct() 0 4 1
A enterNode() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock;
6
7
use PhpParser\Node;
8
use PhpParser\NodeVisitorAbstract;
9
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...
10
11
final class MissingDocBlockVisitor extends NodeVisitorAbstract
12
{
13
    public array $missingDocBlocks = [];
14
    private string $filename;
15
    private MissingDocblockConfigDTO $config;
16
17 23
    public function __construct(string $filename, MissingDocblockConfigDTO $config)
18
    {
19 23
        $this->filename = $filename;
20 23
        $this->config = $config;
21
    }
22
23 23
    public function enterNode(Node $node): void
24
    {
25 23
        if ($this->requiresDocBlock($node)) {
26 14
            $docComment = $node->getDocComment();
27 14
            if ($docComment === null) {
28 12
                $this->missingDocBlocks[] = [
29 12
                    'type' => 'missingDocblock',
30 12
                    'content' => '',
31 12
                    'file' => $this->filename,
32 12
                    'line' => $node->getLine(),
0 ignored issues
show
Deprecated Code introduced by
The function PhpParser\Node::getLine() has been deprecated: Use getStartLine() instead ( Ignorable by Annotation )

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

32
                    'line' => /** @scrutinizer ignore-deprecated */ $node->getLine(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
33 12
                ];
34
            }
35
        }
36
    }
37
38 23
    private function requiresDocBlock(Node $node): bool
39
    {
40 23
        if ($node instanceof Node\Stmt\Class_ && $this->config->class) {
41 11
            return !$node->isAnonymous();
42
        }
43
44 23
        if ($node instanceof Node\Stmt\Trait_ && $this->config->trait) {
45 1
            return true;
46
        }
47
48 23
        if ($node instanceof Node\Stmt\Interface_ && $this->config->interface) {
49 1
            return true;
50
        }
51
52 23
        if ($node instanceof Node\Stmt\Enum_ && $this->config->enum) {
53 1
            return true;
54
        }
55
56 23
        if ($node instanceof Node\Stmt\Function_ && $this->config->function) {
57 1
            return true;
58
        }
59
60 23
        if ($node instanceof Node\Stmt\ClassMethod && $this->config->function) {
61 3
            return true;
62
        }
63
64 23
        if ($node instanceof Node\Stmt\Property && $this->config->property) {
65 5
            return true;
66
        }
67
68 23
        if ($node instanceof Node\Stmt\ClassConst && $this->config->constant) {
69 1
            return true;
70
        }
71
72 23
        return false;
73
    }
74
}
75