|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SavinMikhail\CommentsDensity\MissingDocblock; |
|
6
|
|
|
|
|
7
|
|
|
use PhpParser\Node; |
|
8
|
|
|
use PhpParser\Node\Expr\Array_; |
|
9
|
|
|
use PhpParser\Node\Expr\Cast; |
|
10
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
|
11
|
|
|
use PhpParser\Node\Expr\Instanceof_; |
|
12
|
|
|
use PhpParser\Node\Expr\New_; |
|
13
|
|
|
use PhpParser\Node\Expr\Variable; |
|
14
|
|
|
use PhpParser\Node\Expr\FuncCall; |
|
15
|
|
|
use PhpParser\Node\Expr\StaticCall; |
|
16
|
|
|
use PhpParser\Node\Expr\Yield_; |
|
17
|
|
|
use PhpParser\Node\Expr\YieldFrom; |
|
18
|
|
|
use PhpParser\Node\Expr\MethodCall; |
|
19
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch; |
|
20
|
|
|
use PhpParser\Node\Name; |
|
21
|
|
|
use PhpParser\Node\Stmt\Return_; |
|
22
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
23
|
|
|
use PhpParser\Node\Expr\Assign; |
|
24
|
|
|
|
|
25
|
|
|
use function array_unique; |
|
26
|
|
|
|
|
27
|
|
|
class MissingGenericVisitor extends NodeVisitorAbstract |
|
28
|
|
|
{ |
|
29
|
|
|
public bool $hasConsistentTypes = false; |
|
30
|
|
|
public array $elementTypes = []; |
|
31
|
|
|
|
|
32
|
13 |
|
public function enterNode(Node $node): null |
|
33
|
|
|
{ |
|
34
|
13 |
|
if ($node instanceof Return_) { |
|
35
|
12 |
|
$this->analyzeExpression($node->expr); |
|
36
|
12 |
|
return null; |
|
37
|
|
|
} |
|
38
|
13 |
|
if ($node instanceof Yield_ || $node instanceof YieldFrom) { |
|
39
|
1 |
|
$this->analyzeExpression($node->value); |
|
|
|
|
|
|
40
|
1 |
|
return null; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
13 |
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
13 |
|
private function analyzeExpression($expr): void |
|
47
|
|
|
{ |
|
48
|
13 |
|
if ($expr instanceof Array_) { |
|
49
|
7 |
|
foreach ($expr->items as $item) { |
|
50
|
6 |
|
$this->analyzeExpression($item->value); |
|
51
|
|
|
} |
|
52
|
12 |
|
} elseif ($expr instanceof New_) { |
|
53
|
8 |
|
if ($expr->class instanceof Name) { |
|
54
|
8 |
|
$this->elementTypes[] = $expr->class->toString(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
4 |
|
} elseif ($expr instanceof Variable) { |
|
57
|
2 |
|
$this->elementTypes[] = 'variable'; |
|
58
|
2 |
|
} elseif ($expr instanceof FuncCall || $expr instanceof StaticCall || $expr instanceof MethodCall) { |
|
59
|
|
|
$this->elementTypes[] = 'function_call'; |
|
60
|
2 |
|
} elseif ($expr instanceof Cast) { |
|
61
|
|
|
$this->analyzeExpression($expr->expr); |
|
62
|
2 |
|
} elseif ($expr instanceof Yield_ || $expr instanceof YieldFrom) { |
|
63
|
|
|
$this->analyzeExpression($expr->value); |
|
|
|
|
|
|
64
|
2 |
|
} elseif ($expr instanceof StaticPropertyFetch) { |
|
65
|
|
|
$this->elementTypes[] = 'static_property'; |
|
66
|
2 |
|
} elseif ($expr instanceof Assign) { |
|
67
|
|
|
$this->analyzeExpression($expr->expr); |
|
68
|
2 |
|
} elseif ($expr instanceof Instanceof_) { |
|
69
|
|
|
$this->analyzeExpression($expr->expr); |
|
70
|
2 |
|
} elseif ($expr instanceof ConstFetch) { |
|
71
|
|
|
$this->elementTypes[] = 'constant'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
13 |
|
public function leaveNode(Node $node): null |
|
76
|
|
|
{ |
|
77
|
13 |
|
if (!empty($this->elementTypes)) { |
|
78
|
10 |
|
$this->hasConsistentTypes = count(array_unique($this->elementTypes)) === 1; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
13 |
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|