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 licence 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
|
328 |
|
public function name() { |
29
|
328 |
|
return "depend_on"; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
23 |
|
public function register_listeners(ListenerRegistry $registry) { |
36
|
23 |
|
$registry->on_enter_misc |
37
|
23 |
|
( array(N\Expr\MethodCall::class, N\Expr\FuncCall::class, |
38
|
23 |
|
N\Stmt\Global_::class, N\Expr\ArrayDimFetch::class, |
39
|
23 |
|
N\Expr\ErrorSuppress::class) |
40
|
23 |
|
, function(Insert $insert, Location $location, \PhpParser\Node $node) { |
41
|
21 |
|
$ref_ids = array(); |
42
|
21 |
|
if ($node instanceof N\Expr\MethodCall) { |
43
|
|
|
// The 'name' could also be a variable like in $this->$method(); |
44
|
4 |
View Code Duplication |
if (is_string($node->name)) { |
|
|
|
|
45
|
3 |
|
$ref_ids[] = $insert->get_reference |
46
|
3 |
|
( Variable::METHOD_TYPE |
47
|
3 |
|
, $node->name |
48
|
3 |
|
, $location->file_path() |
49
|
3 |
|
, $node->getAttribute("startLine") |
50
|
3 |
|
); |
51
|
3 |
|
} |
52
|
4 |
|
} |
53
|
17 |
View Code Duplication |
elseif($node instanceof N\Expr\FuncCall) { |
|
|
|
|
54
|
|
|
// Omit calls to closures, we would not be able to |
55
|
|
|
// analyze them anyway atm. |
56
|
|
|
// Omit functions in arrays, we would not be able to |
57
|
|
|
// analyze them anyway atm. |
58
|
13 |
|
if (!($node->name instanceof N\Expr\Variable || |
59
|
13 |
|
$node->name instanceof N\Expr\ArrayDimFetch)) { |
60
|
11 |
|
$ref_ids[] = $insert->get_reference |
61
|
11 |
|
( Variable::FUNCTION_TYPE |
62
|
11 |
|
, $node->name->parts[0] |
63
|
11 |
|
, $location->file_path() |
64
|
11 |
|
, $node->getAttribute("startLine") |
65
|
11 |
|
); |
66
|
11 |
|
} |
67
|
13 |
|
} |
68
|
8 |
|
elseif ($node instanceof N\Stmt\Global_) { |
69
|
2 |
|
foreach ($node->vars as $var) { |
70
|
2 |
|
if (!($var instanceof N\Expr\Variable) || !is_string($var->name)) { |
71
|
|
|
throw new \RuntimeException( |
72
|
|
|
"Expected Variable with string name, found: ".print_r($var, true)); |
73
|
|
|
} |
74
|
2 |
|
$ref_ids[] = $insert->get_reference |
75
|
2 |
|
( Variable::GLOBAL_TYPE |
76
|
2 |
|
, $var->name |
77
|
2 |
|
, $location->file_path() |
78
|
2 |
|
, $node->getAttribute("startLine") |
79
|
2 |
|
); |
80
|
2 |
|
} |
81
|
2 |
|
} |
82
|
8 |
|
elseif ($node instanceof N\Expr\ArrayDimFetch) { |
83
|
5 |
|
if ($node->var instanceof N\Expr\Variable && $node->var->name == "GLOBALS") { |
84
|
|
|
// Ignore usage of $GLOBALS with variable index. |
85
|
4 |
|
if (!($node->dim instanceof N\Expr\Variable)) { |
86
|
3 |
|
$ref_ids[] = $insert->get_reference |
87
|
3 |
|
( Variable::GLOBAL_TYPE |
88
|
3 |
|
, $node->dim->value |
|
|
|
|
89
|
3 |
|
, $location->file_path() |
90
|
3 |
|
, $node->getAttribute("startLine") |
91
|
3 |
|
); |
92
|
3 |
|
} |
93
|
4 |
|
} |
94
|
5 |
|
} |
95
|
3 |
|
elseif ($node instanceof N\Expr\ErrorSuppress) { |
96
|
3 |
|
$ref_ids[] = $insert->get_reference |
97
|
3 |
|
( Variable::LANGUAGE_CONSTRUCT_TYPE |
98
|
3 |
|
, "@" |
99
|
3 |
|
, $location->file_path() |
100
|
3 |
|
, $node->getAttribute("startLine") |
101
|
3 |
|
); |
102
|
3 |
|
} |
103
|
21 |
|
foreach ($ref_ids as $ref_id) { |
104
|
17 |
|
$this->insert_relation_into |
105
|
17 |
|
( $insert |
106
|
17 |
|
, $location |
107
|
17 |
|
, $ref_id |
108
|
17 |
|
); |
109
|
21 |
|
} |
110
|
23 |
|
}); |
111
|
23 |
|
} |
112
|
|
|
} |
113
|
|
|
|
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.