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

MissingDocBlockVisitor::enterNode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 9
cts 9
cp 1
crap 3
rs 10
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