1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Rules; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Variables\Variable; |
14
|
|
|
use Lechimp\Dicto\Indexer\Location; |
15
|
|
|
use Lechimp\Dicto\Indexer\Insert; |
16
|
|
|
use Lechimp\Dicto\Indexer\ListenerRegistry; |
17
|
|
|
use PhpParser\Node as N; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A class or function is considered do depend on something if its body |
21
|
|
|
* of definition makes use of the thing. Language constructs, files or globals |
22
|
|
|
* can't depend on anything. |
23
|
|
|
*/ |
24
|
|
|
class DependOn extends Relation { |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
58 |
|
public function name() { |
29
|
58 |
|
return "depend on"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
11 |
|
public function register_listeners(ListenerRegistry $registry) { |
36
|
11 |
|
$this->register_method_call_listener($registry); |
37
|
11 |
|
$this->register_func_call_listener($registry); |
38
|
11 |
|
$this->register_global_listener($registry); |
39
|
11 |
|
$this->register_array_dim_fetch_listener($registry); |
40
|
11 |
|
$this->register_error_suppressor_listener($registry); |
41
|
11 |
|
} |
42
|
|
|
|
43
|
11 |
View Code Duplication |
protected function register_method_call_listener(ListenerRegistry $registry) { |
|
|
|
|
44
|
11 |
|
$registry->on_enter_misc |
45
|
11 |
|
( [N\Expr\MethodCall::class] |
46
|
|
|
, function(Insert $insert, Location $location, N\Expr\MethodCall $node) { |
47
|
|
|
// The 'name' could also be a variable like in $this->$method(); |
48
|
1 |
|
if (is_string($node->name)) { |
49
|
1 |
|
$method_reference = $insert->_method_reference |
50
|
1 |
|
( $node->name |
51
|
1 |
|
, $location->in_entities()[0][1] |
52
|
1 |
|
, $location->line() |
53
|
1 |
|
, $location->column() |
54
|
1 |
|
); |
55
|
1 |
|
$this->insert_relation_into |
56
|
1 |
|
( $insert |
57
|
1 |
|
, $location |
58
|
1 |
|
, $method_reference |
59
|
1 |
|
, $location->line() |
|
|
|
|
60
|
1 |
|
); |
61
|
1 |
|
} |
62
|
11 |
|
}); |
63
|
11 |
|
} |
64
|
|
|
|
65
|
11 |
View Code Duplication |
protected function register_func_call_listener(ListenerRegistry $registry) { |
|
|
|
|
66
|
11 |
|
$registry->on_enter_misc |
67
|
11 |
|
( [N\Expr\FuncCall::class] |
68
|
|
|
, function(Insert $insert, Location $location, N\Expr\FuncCall $node) { |
69
|
|
|
// Omit calls to closures, we would not be able to |
70
|
|
|
// analyze them anyway atm. |
71
|
|
|
// Omit functions in arrays, we would not be able to |
72
|
|
|
// analyze them anyway atm. |
73
|
2 |
|
if (!($node->name instanceof N\Expr\Variable || |
74
|
2 |
|
$node->name instanceof N\Expr\ArrayDimFetch)) { |
75
|
2 |
|
$function_reference = $insert->_function_reference |
76
|
2 |
|
( $node->name->parts[0] |
77
|
2 |
|
, $location->in_entities()[0][1] |
78
|
2 |
|
, $location->line() |
79
|
2 |
|
, $location->column() |
80
|
2 |
|
); |
81
|
2 |
|
$this->insert_relation_into |
82
|
2 |
|
( $insert |
83
|
2 |
|
, $location |
84
|
2 |
|
, $function_reference |
85
|
2 |
|
); |
86
|
2 |
|
} |
87
|
11 |
|
}); |
88
|
11 |
|
} |
89
|
|
|
|
90
|
11 |
|
protected function register_global_listener(ListenerRegistry $registry) { |
91
|
11 |
|
$registry->on_enter_misc |
92
|
11 |
|
( [N\Stmt\Global_::class] |
93
|
|
|
, function(Insert $insert, Location $location, N\Stmt\Global_ $node) { |
94
|
6 |
|
foreach ($node->vars as $var) { |
95
|
6 |
|
if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) { |
96
|
|
|
throw new \RuntimeException( |
97
|
|
|
"Expected Variable with string name, found: ".print_r($var, true)); |
98
|
|
|
} |
99
|
6 |
|
$global = $insert->_global($var->name); |
100
|
6 |
|
$this->insert_relation_into |
101
|
6 |
|
( $insert |
102
|
6 |
|
, $location |
103
|
6 |
|
, $global |
104
|
6 |
|
); |
105
|
6 |
|
} |
106
|
11 |
|
}); |
107
|
11 |
|
} |
108
|
|
|
|
109
|
11 |
|
protected function register_array_dim_fetch_listener(ListenerRegistry $registry) { |
110
|
11 |
|
$registry->on_enter_misc |
111
|
11 |
|
( [N\Expr\ArrayDimFetch::class] |
112
|
|
|
, function(Insert $insert, Location $location, N\Expr\ArrayDimFetch $node) { |
113
|
1 |
|
if ($node->var instanceof N\Expr\Variable |
114
|
1 |
|
&& $node->var->name == "GLOBALS" |
115
|
|
|
// Ignore usage of $GLOBALS with variable index. |
116
|
1 |
|
&& !($node->dim instanceof N\Expr\Variable)) { |
117
|
1 |
|
$global = $insert->_global($node->dim->value); |
|
|
|
|
118
|
1 |
|
$this->insert_relation_into |
119
|
1 |
|
( $insert |
120
|
1 |
|
, $location |
121
|
1 |
|
, $global |
122
|
1 |
|
); |
123
|
1 |
|
} |
124
|
11 |
|
}); |
125
|
11 |
|
} |
126
|
|
|
|
127
|
11 |
View Code Duplication |
protected function register_error_suppressor_listener(ListenerRegistry $registry) { |
|
|
|
|
128
|
11 |
|
$registry->on_enter_misc |
129
|
11 |
|
( [N\Expr\ErrorSuppress::class] |
130
|
11 |
|
, function(Insert $insert, Location $location, N\Expr\ErrorSuppress $node) { |
|
|
|
|
131
|
1 |
|
$language_construct = $insert->_language_construct("@"); |
132
|
1 |
|
$this->insert_relation_into |
133
|
1 |
|
( $insert |
134
|
1 |
|
, $location |
135
|
1 |
|
, $language_construct |
136
|
1 |
|
); |
137
|
11 |
|
}); |
138
|
11 |
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.