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

MissingDocBlockVisitor::requiresDocBlock()   C

Complexity

Conditions 17
Paths 9

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 17
c 1
b 0
f 0
nc 9
nop 1
dl 0
loc 35
ccs 18
cts 18
cp 1
crap 17
rs 5.2166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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