Passed
Push — main ( 4736ca...948518 )
by mikhail
03:04
created

methodThrowsUncaughtExceptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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

43
            '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...
44 14
        ];
45
    }
46
47 27
    private function requiresDocBlock(Node $node): bool
48
    {
49 27
        if ($node instanceof Class_ && $this->config->class) {
50 11
            return !$node->isAnonymous();
51
        }
52
53 27
        if ($node instanceof Trait_ && $this->config->trait) {
54 1
            return true;
55
        }
56
57 27
        if ($node instanceof Interface_ && $this->config->interface) {
58 1
            return true;
59
        }
60
61 27
        if ($node instanceof Enum_ && $this->config->enum) {
62 1
            return true;
63
        }
64
65 27
        if ($node instanceof Function_ && $this->config->function) {
66 1
            return true;
67
        }
68
69 27
        if ($node instanceof ClassMethod && $this->config->function) {
70 7
            if ($this->config->requireDocblocksForAllMethods) {
71 3
                return true;
72
            }
73 4
            return $this->methodRequiresAdditionalDocBlock($node);
74
        }
75
76 27
        if ($node instanceof Property && $this->config->property) {
77 5
            return true;
78
        }
79
80 27
        if ($node instanceof ClassConst && $this->config->constant) {
81 1
            return true;
82
        }
83
84 27
        return false;
85
    }
86
87
    /**
88
     * here we want to find methods that has uncaught throws statements or their return type will be better
89
     * described as generic
90
     */
91 4
    private function methodRequiresAdditionalDocBlock(Node $node): bool
92
    {
93 4
        $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

93
        /** @scrutinizer ignore-call */ 
94
        $returnType = $node->getReturnType();
Loading history...
94
95 4
        if ($returnType instanceof Node\Identifier && $returnType->toString() === 'array') {
96 2
            return true;
97
        }
98
99 2
        if ($this->methodThrowsUncaughtExceptions($node)) {
100
            return true;
101
        }
102
103 2
        return false;
104
    }
105
106 2
    private function methodThrowsUncaughtExceptions(Node $node): bool
107
    {
108 2
        $traverser = new NodeTraverser();
109 2
        $visitor = new UncaughtExceptionVisitor();
110
111 2
        $traverser->addVisitor($visitor);
112 2
        $traverser->traverse([$node]);
113
114 2
        return $visitor->throwsUncaughtExceptions;
115
    }
116
}
117