|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Registry; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node\Stmt; |
|
6
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
7
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
8
|
|
|
use PhpParser\Node\Stmt\Interface_; |
|
9
|
|
|
use PhpParser\Node\Stmt\Trait_; |
|
10
|
|
|
|
|
11
|
|
|
class Registry |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* A list of contexts with all the nodes that were found in the source code. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
public $data; // TODO: Convert to protected <[email protected]> |
|
19
|
|
|
/** |
|
20
|
|
|
* A list of contexts with all the file path leading |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
public $mapping; // TODO: Convert to protected <[email protected]> |
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $currentFile; |
|
31
|
|
|
|
|
32
|
41 |
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
$contexts = [ |
|
35
|
41 |
|
'class' => [], |
|
36
|
|
|
'function' => [], |
|
37
|
|
|
'interface' => [], |
|
38
|
|
|
'trait' => [], |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
41 |
|
$this->data = $contexts; |
|
42
|
41 |
|
$this->mapping = $contexts; |
|
43
|
41 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \PhpParser\Node\Stmt\Class_ $class |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function addClass(Class_ $class) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$this->addNode('class', $class); |
|
51
|
6 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param \PhpParser\Node\Stmt\Function_ $function |
|
55
|
|
|
*/ |
|
56
|
21 |
|
public function addFunction(Function_ $function) |
|
57
|
|
|
{ |
|
58
|
21 |
|
$this->addNode('function', $function); |
|
59
|
21 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param \PhpParser\Node\Stmt\Interface_ $interface |
|
63
|
|
|
*/ |
|
64
|
6 |
|
public function addInterface(Interface_ $interface) |
|
65
|
|
|
{ |
|
66
|
6 |
|
$this->addNode('interface', $interface); |
|
67
|
6 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param \PhpParser\Node\Stmt\Trait_ $trait |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function addTrait(Trait_ $trait) |
|
73
|
|
|
{ |
|
74
|
6 |
|
$this->addNode('trait', $trait); |
|
75
|
6 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $context |
|
79
|
|
|
* @param Stmt $node |
|
80
|
|
|
*/ |
|
81
|
39 |
|
protected function addNode($context, Stmt $node) |
|
82
|
|
|
{ |
|
83
|
39 |
|
$fullyQualifiedName = $this->fullyQualifiedName($node); |
|
84
|
39 |
|
$this->data[$context][$fullyQualifiedName] = $node; |
|
85
|
39 |
|
$this->mapping[$context][$fullyQualifiedName] = $this->getCurrentFile(); |
|
86
|
39 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Stmt $node |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
39 |
|
protected function fullyQualifiedName(Stmt $node) |
|
93
|
|
|
{ |
|
94
|
39 |
|
return isset($node->namespacedName) ? $node->namespacedName->toString() : $node->name->toString(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $file |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public function setCurrentFile($file) |
|
101
|
|
|
{ |
|
102
|
5 |
|
$this->currentFile = realpath($file); |
|
103
|
5 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
39 |
|
public function getCurrentFile() |
|
109
|
|
|
{ |
|
110
|
39 |
|
return $this->currentFile; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|