|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Padawan\Domain\Scope; |
|
4
|
|
|
|
|
5
|
|
|
use Padawan\Domain\Scope; |
|
6
|
|
|
use Padawan\Domain\Project\FQCN; |
|
7
|
|
|
use Padawan\Domain\Project\Node\Uses; |
|
8
|
|
|
use Padawan\Domain\Project\Node\Variable; |
|
9
|
|
|
use Padawan\Domain\Project\Node\FunctionData; |
|
10
|
|
|
use Padawan\Domain\Project\Node\TypeHint; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractScope implements Scope |
|
13
|
|
|
{ |
|
14
|
|
|
private $variables = []; |
|
15
|
|
|
private $functions = []; |
|
16
|
|
|
private $constants = []; |
|
17
|
|
|
/** @var Scope */ |
|
18
|
|
|
private $parent; |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
private $typeHints = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @return Variable[] */ |
|
23
|
|
|
public function getVars($startLine = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (!is_null($startLine) && !empty($this->typeHints)) { |
|
26
|
|
|
return array_merge( |
|
27
|
|
|
$this->variables, |
|
28
|
|
|
$this->_filterTypeHints($startLine) |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
return $this->variables; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** @return Variable */ |
|
35
|
|
|
public function getVar($varName, $startLine = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!is_null($startLine) && !empty($this->typeHints)) { |
|
38
|
|
|
$candidates = array_merge( |
|
39
|
|
|
$this->variables, |
|
40
|
|
|
$this->_filterTypeHints($startLine) |
|
41
|
|
|
); |
|
42
|
|
|
} else { |
|
43
|
|
|
$candidates = $this->variables; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (array_key_exists($varName, $candidates)) { |
|
47
|
|
|
return $candidates[$varName]; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function addVar(Variable $var) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->variables[$var->getName()] = $var; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getFunctions() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->functions; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getFunction($functionName) |
|
62
|
|
|
{ |
|
63
|
|
|
if (array_key_exists($functionName, $this->functions)) { |
|
64
|
|
|
return $this->functions[$functionName]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function addFunction(FunctionData $function) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->functions[$function->name] = $function; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** @return string[] */ |
|
74
|
|
|
public function getConstants() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->constants; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getConstant($constName) |
|
80
|
|
|
{ |
|
81
|
|
|
if (array_key_exists($constName, $this->constants)) { |
|
82
|
|
|
return $this->constants[$constName]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function addConstant($constName) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->constants[$constName] = $constName; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function addTypeHints($typeHints) |
|
92
|
|
|
{ |
|
93
|
|
|
if (is_array($typeHints)) { |
|
94
|
|
|
$this->typeHints = array_values($typeHints); |
|
95
|
|
|
} else { |
|
96
|
|
|
$this->typeHints[] = $typeHints; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function _filterTypeHints($startLine) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($startLine < 2) { |
|
103
|
|
|
// PHP file header |
|
104
|
|
|
return []; |
|
105
|
|
|
} |
|
106
|
|
|
$result = array_filter($this->typeHints, function($th) use ($startLine) { |
|
107
|
|
|
/** @var $th TypeHint */ |
|
108
|
|
|
return $th->startLine <= $startLine; |
|
109
|
|
|
}); |
|
110
|
|
|
$returnVal = []; |
|
111
|
|
|
foreach ($result as $th) { |
|
112
|
|
|
$returnVal[$th->getName()] = $th; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $returnVal; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.