Passed
Push — main ( 30dfbd...b8dd52 )
by mikhail
03:24
created

MissingDocBlockVisitor::methodNeedsGeneric()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.0222

Importance

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

52
            '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...
53 31
        ];
54
    }
55
56 51
    private function requiresDocBlock(Node $node): bool
57
    {
58 51
        if ($node instanceof Class_ && $this->config->class) {
59 18
            return !$node->isAnonymous();
60
        }
61
62 51
        if ($node instanceof Trait_ && $this->config->trait) {
63 1
            return true;
64
        }
65
66 51
        if ($node instanceof Interface_ && $this->config->interface) {
67 1
            return true;
68
        }
69
70 51
        if ($node instanceof Enum_ && $this->config->enum) {
71 2
            return true;
72
        }
73
74 51
        if ($node instanceof Function_ && $this->config->function) {
75 1
            return true;
76
        }
77
78 51
        if ($node instanceof ClassMethod && $this->config->function) {
79 26
            if ($this->config->requireForAllMethods) {
80 6
                return true;
81
            }
82 20
            return $this->methodRequiresAdditionalDocBlock($node);
83
        }
84
85 51
        if ($node instanceof Property && $this->config->property) {
86 5
            return true;
87
        }
88
89 51
        if ($node instanceof ClassConst && $this->config->constant) {
90 1
            return true;
91
        }
92
93 51
        return false;
94
    }
95
96
    /**
97
     * here we want to find methods that have uncaught throw statements or their return type will be better
98
     * described as generic
99
     */
100 20
    private function methodRequiresAdditionalDocBlock(Node $node): bool
101
    {
102 20
        return
103 20
            $this->methodNeedsGeneric($node)
104 20
            || $this->methodThrowsUncaughtExceptions($node);
105
    }
106
107 20
    private function methodNeedsGeneric(Node $node): bool
108
    {
109 20
        $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

109
        /** @scrutinizer ignore-call */ 
110
        $returnType = $node->getReturnType();
Loading history...
110
111 20
        if ($returnType === null) {
112
            return false;
113
        }
114
115
        if (
116 20
            $returnType instanceof Identifier
117 20
            && in_array($returnType->toString(), ['array', 'iterable'], true)
118
        ) {
119 8
            return $this->arrayElementsHaveConsistentTypes($node);
120
        }
121
122
        if (
123 12
            $returnType instanceof Name
124 12
            && in_array($returnType->toString(), ['Generator', 'Traversable', 'Iterator', 'ArrayAccess'], true)
125
        ) {
126 3
            return $this->arrayElementsHaveConsistentTypes($node);
127
        }
128
129 9
        if ($this->isReturnClassTraversable($returnType)) {
130 2
            return $this->arrayElementsHaveConsistentTypes($node);
131
        }
132
133 7
        return false;
134
    }
135
136 9
    private function isReturnClassTraversable(ComplexType|Identifier|Name $returnType): bool
137
    {
138 9
        if (!($returnType instanceof Name)) {
139 6
            return false;
140
        }
141
142 3
        $returnTypeName = $returnType->toString();
143 3
        if (!class_exists($returnTypeName)) {
144 1
            return false;
145
        }
146 2
        $reflectionClass = new ReflectionClass($returnTypeName);
147
        if (
148 2
            ! $reflectionClass->implementsInterface(Iterator::class)
149 2
            && !$reflectionClass->implementsInterface(ArrayAccess::class)
150
        ) {
151
            return false;
152
        }
153 2
        return true;
154
    }
155
156 13
    private function arrayElementsHaveConsistentTypes(Node $node): bool
157
    {
158 13
        $traverser = new NodeTraverser();
159 13
        $visitor = new MissingGenericVisitor();
160
161 13
        $traverser->addVisitor($visitor);
162 13
        $traverser->traverse([$node]);
163
164 13
        return $visitor->hasConsistentTypes;
165
    }
166
167 11
    private function methodThrowsUncaughtExceptions(Node $node): bool
168
    {
169 11
        $traverser = new NodeTraverser();
170 11
        $visitor = new UncaughtExceptionVisitor();
171
172 11
        $traverser->addVisitor($visitor);
173 11
        $traverser->traverse([$node]);
174
175 11
        return $visitor->hasUncaughtThrows;
176
    }
177
}
178