This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInterfaceQuestioner.php
More file actions
100 lines (89 loc) · 3.09 KB
/
InterfaceQuestioner.php
File metadata and controls
100 lines (89 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php declare(strict_types=1);
namespace PHPVisualDebt\Questioner;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\NodeVisitor;
use PHPVisualDebt\FileQuestions;
class InterfaceQuestioner extends AbstractQuestioner implements NodeVisitor
{
const VISUAL_DEBT = 1;
/** @var Node\Stmt\Interface_[] */
protected $interfaces = [];
/** @var Node\Stmt\Class_[] */
protected $classes = [];
/** @var \SplFileInfo[] */
protected $fileClasses = [];
public function beforeTraverse(array $nodes)
{
// TODO: Implement beforeTraverse() method.
}
public function enterNode(Node $node)
{
if ($node instanceof Node\Stmt\Interface_) {
/** @var Name $name */
$name = $node->namespacedName;
$this->interfaces[$name->toString()] = $node;
}
if ($node instanceof Node\Stmt\Class_ && \count($node->implements) > 0) {
/** @var Name $name */
$name = $node->namespacedName;
$this->classes[$name->toString()] = $node;
$this->fileClasses[$name->toString()] = $this->fileInfo;
}
}
public function leaveNode(Node $node)
{
// TODO: Implement leaveNode() method.
}
public function afterTraverse(array $nodes)
{
// TODO: Implement afterTraverse() method.
}
public function getFileQuestions() : array
{
$filesQuestions = [];
$implements = [];
foreach ($this->classes as $class) {
/** @var Name\FullyQualified $implement */
foreach ($class->implements as $implement) {
if (!\array_key_exists($implement->toString(), $implements)) {
$implements[$implement->toString()] = 0;
}
$implements[$implement->toString()]++;
}
}
foreach ($this->classes as $class) {
/** @var Name $name */
$name = $class->namespacedName;
/** @var Name\FullyQualified $implement */
foreach ($class->implements as $implement) {
if (
\array_key_exists($name->toString(), $this->fileClasses) &&
\array_key_exists($implement->toString(), $this->interfaces) &&
\array_key_exists($implement->toString(), $implements) &&
1 === $implements[$implement->toString()]
) {
$filesQuestions[] = new FileQuestions(
$this->fileClasses[$name->toString()],
[
new Question(
"Is there any justification for interface <name>{$implement}</name> usage?",
self::VISUAL_DEBT,
$implement->getLine()
),
]
);
}
}
}
return $filesQuestions;
}
public function isSingleFileMode(): bool
{
return false;
}
public function isPostAnalysisMode(): bool
{
return true;
}
}