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\ASTVisitor; |
15
|
|
|
use Lechimp\Dicto\Indexer\Location; |
16
|
|
|
use Lechimp\Dicto\Indexer\Insert; |
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 implements ASTVisitor { |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
64 |
|
public function name() { |
29
|
64 |
|
return "depend on"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
11 |
|
public function visitorJumpLabels() { |
36
|
|
|
return |
37
|
|
|
[ N\Expr\MethodCall::class => "enterMethodCall" |
38
|
11 |
|
, N\Expr\FuncCall::class => "enterFunctionCall" |
39
|
11 |
|
, N\Stmt\Global_::class => "enterGlobal" |
40
|
11 |
|
, N\Expr\ArrayDimFetch::class => "enterArrayDimFetch" |
41
|
11 |
|
, N\Expr\ErrorSuppress::class => "enterErrorSuppress" |
42
|
11 |
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
View Code Duplication |
public function enterMethodCall(Insert $insert, Location $location, N\Expr\MethodCall $node) { |
|
|
|
|
46
|
|
|
// The 'name' could also be a variable like in $this->$method(); |
47
|
1 |
|
if (is_string($node->name)) { |
48
|
1 |
|
$method_reference = $insert->_method_reference |
49
|
1 |
|
( $node->name |
50
|
1 |
|
, $location->_file() |
51
|
1 |
|
, $location->_line() |
52
|
1 |
|
, $location->_column() |
53
|
1 |
|
); |
54
|
1 |
|
$this->insert_relation_into |
55
|
1 |
|
( $insert |
56
|
1 |
|
, $location |
57
|
1 |
|
, $method_reference |
58
|
1 |
|
, $location->_line() |
|
|
|
|
59
|
1 |
|
); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
2 |
View Code Duplication |
public function enterFunctionCall(Insert $insert, Location $location, N\Expr\FuncCall $node) { |
|
|
|
|
64
|
|
|
// Omit calls to closures, we would not be able to |
65
|
|
|
// analyze them anyway atm. |
66
|
|
|
// Omit functions in arrays, we would not be able to |
67
|
|
|
// analyze them anyway atm. |
68
|
2 |
|
if (!($node->name instanceof N\Expr\Variable || |
69
|
2 |
|
$node->name instanceof N\Expr\ArrayDimFetch)) { |
70
|
2 |
|
$function_reference = $insert->_function_reference |
71
|
2 |
|
( $node->name->parts[0] |
72
|
2 |
|
, $location->_file() |
73
|
2 |
|
, $location->_line() |
74
|
2 |
|
, $location->_column() |
75
|
2 |
|
); |
76
|
2 |
|
$this->insert_relation_into |
77
|
2 |
|
( $insert |
78
|
2 |
|
, $location |
79
|
2 |
|
, $function_reference |
80
|
2 |
|
); |
81
|
2 |
|
} |
82
|
2 |
|
} |
83
|
|
|
|
84
|
6 |
|
public function enterGlobal(Insert $insert, Location $location, N\Stmt\Global_ $node) { |
85
|
6 |
|
foreach ($node->vars as $var) { |
86
|
6 |
|
if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) { |
87
|
|
|
throw new \RuntimeException( |
88
|
|
|
"Expected Variable with string name, found: ".print_r($var, true)); |
89
|
|
|
} |
90
|
6 |
|
$global = $insert->_global($var->name); |
91
|
6 |
|
$this->insert_relation_into |
92
|
6 |
|
( $insert |
93
|
6 |
|
, $location |
94
|
6 |
|
, $global |
95
|
6 |
|
); |
96
|
6 |
|
} |
97
|
6 |
|
} |
98
|
|
|
|
99
|
1 |
|
public function enterArrayDimFetch(Insert $insert, Location $location, N\Expr\ArrayDimFetch $node) { |
100
|
1 |
|
if ($node->var instanceof N\Expr\Variable |
101
|
1 |
|
&& $node->var->name == "GLOBALS" |
102
|
|
|
// Ignore usage of $GLOBALS with variable index. |
103
|
1 |
|
&& !($node->dim instanceof N\Expr\Variable)) { |
104
|
1 |
|
$global = $insert->_global($node->dim->value); |
|
|
|
|
105
|
1 |
|
$this->insert_relation_into |
106
|
1 |
|
( $insert |
107
|
1 |
|
, $location |
108
|
1 |
|
, $global |
109
|
1 |
|
); |
110
|
1 |
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
public function enterErrorSuppress(Insert $insert, Location $location, N\Expr\ErrorSuppress $node) { |
|
|
|
|
114
|
1 |
|
$language_construct = $insert->_language_construct("@"); |
115
|
1 |
|
$this->insert_relation_into |
116
|
1 |
|
( $insert |
117
|
1 |
|
, $location |
118
|
1 |
|
, $language_construct |
119
|
1 |
|
); |
120
|
1 |
|
} |
121
|
|
|
} |
122
|
|
|
|
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.