Passed
Push — main ( 3a4be5...4736ca )
by mikhail
03:07
created

methodRequiresAdditionalDocBlock()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.0466

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
crap 4.0466
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\MissingDocblock;
6
7
use PhpParser\Node;
8
use PhpParser\Node\Expr\Throw_;
9
use PhpParser\Node\Stmt\Class_;
10
use PhpParser\Node\Stmt\ClassConst;
11
use PhpParser\Node\Stmt\ClassMethod;
12
use PhpParser\Node\Stmt\Enum_;
13
use PhpParser\Node\Stmt\Function_;
14
use PhpParser\Node\Stmt\Interface_;
15
use PhpParser\Node\Stmt\Property;
16
use PhpParser\Node\Stmt\Trait_;
17
use PhpParser\NodeTraverser;
18
use PhpParser\NodeVisitorAbstract;
19
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...
20
21
final class MissingDocBlockVisitor extends NodeVisitorAbstract
22
{
23
    public array $missingDocBlocks = [];
24
25 26
    public function __construct(
26
        private readonly string $filename,
27
        private readonly MissingDocblockConfigDTO $config
28
    ) {
29 26
    }
30
31 26
    public function enterNode(Node $node): void
32
    {
33 26
        if (! $this->requiresDocBlock($node)) {
34 26
            return;
35
        }
36 16
        $docComment = $node->getDocComment();
37 16
        if ($docComment !== null) {
38 9
            return;
39
        }
40 14
        $this->missingDocBlocks[] = [
41 14
            'type' => 'missingDocblock',
42 14
            'content' => '',
43 14
            'file' => $this->filename,
44 14
            '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

44
            '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...
45 14
        ];
46
    }
47
48 26
    private function requiresDocBlock(Node $node): bool
49
    {
50 26
        if ($node instanceof Class_ && $this->config->class) {
51 11
            return !$node->isAnonymous();
52
        }
53
54 26
        if ($node instanceof Trait_ && $this->config->trait) {
55 1
            return true;
56
        }
57
58 26
        if ($node instanceof Interface_ && $this->config->interface) {
59 1
            return true;
60
        }
61
62 26
        if ($node instanceof Enum_ && $this->config->enum) {
63 1
            return true;
64
        }
65
66 26
        if ($node instanceof Function_ && $this->config->function) {
67 1
            return true;
68
        }
69
70 26
        if ($node instanceof ClassMethod && $this->config->function) {
71 6
            if ($this->config->requireDocblocksForAllMethods) {
72 3
                return true;
73
            }
74 3
            return $this->methodRequiresAdditionalDocBlock($node);
75
        }
76
77 26
        if ($node instanceof Property && $this->config->property) {
78 5
            return true;
79
        }
80
81 26
        if ($node instanceof ClassConst && $this->config->constant) {
82 1
            return true;
83
        }
84
85 26
        return false;
86
    }
87
88
    /**
89
     * here we want to find methods that has uncaught throws statements or their return type will be better
90
     * described as generic
91
     */
92 3
    private function methodRequiresAdditionalDocBlock(Node $node): bool
93
    {
94 3
        $returnType = $node->getReturnType();
0 ignored issues
show
Bug introduced by
The method getReturnType() does not exist on PhpParser\Node. It seems like you code against a sub-type of PhpParser\Node such as PhpParser\Node\FunctionLike or PhpParser\Node\Stmt\ClassMethod or PhpParser\Node\Stmt\Function_ or PhpParser\Node\Expr\ArrowFunction or PhpParser\Node\Expr\Closure. ( Ignorable by Annotation )

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

94
        /** @scrutinizer ignore-call */ 
95
        $returnType = $node->getReturnType();
Loading history...
95
96 3
        if ($returnType instanceof Node\Identifier && $returnType->toString() === 'array') {
97 2
            return true;
98
        }
99
100 1
        if ($this->methodThrowsExceptions($node)) {
101
            return true;
102
        }
103
104 1
        return false;
105
    }
106
107 1
    private function methodThrowsExceptions(Node $node): bool
108
    {
109 1
        $traverser = new NodeTraverser();
110 1
        $visitor = new class extends NodeVisitorAbstract {
111
            public bool $throwsExceptions = false;
112
113
            public function enterNode(Node $node): void
114
            {
115 1
                if ($node instanceof Throw_) {
116
                    $this->throwsExceptions = true;
117
                }
118
            }
119 1
        };
120 1
        $traverser->addVisitor($visitor);
121 1
        $traverser->traverse([$node]);
122
123 1
        return $visitor->throwsExceptions;
124
    }
125
}
126