1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Comparator; |
4
|
|
|
|
5
|
|
|
class Signature { |
6
|
|
|
/** |
7
|
|
|
* @param array $parametersA |
8
|
|
|
* @param array $parametersB |
9
|
|
|
* @return array |
10
|
|
|
*/ |
11
|
15 |
|
public static function analyze(array $parametersA, array $parametersB) |
12
|
|
|
{ |
13
|
15 |
|
$changes = [ |
14
|
15 |
|
'parameter_added' => false, |
15
|
|
|
'parameter_removed' => false, |
16
|
12 |
|
'parameter_renamed' => false, |
17
|
8 |
|
'parameter_typing_added' => false, |
18
|
|
|
'parameter_typing_removed' => false, |
19
|
4 |
|
'parameter_default_added' => false, |
20
|
|
|
'parameter_default_removed' => false, |
21
|
7 |
|
'parameter_default_value_changed' => false, |
22
|
|
|
]; |
23
|
7 |
|
$lengthA = count($parametersA); |
24
|
|
|
$lengthB = count($parametersB); |
25
|
|
|
|
26
|
|
|
// TODO([email protected]): This is only true if newer params do not have defaults |
27
|
7 |
|
if ($lengthA < $lengthB) { |
28
|
7 |
|
$changes['parameter_added'] = true; |
29
|
|
|
} elseif ($lengthA > $lengthB) { |
30
|
|
|
$changes['parameter_removed'] = true; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$iterations = min($lengthA, $lengthB); |
34
|
|
|
for ($i = 0; $i < $iterations; ++$i) { |
35
|
|
|
// Name checking |
36
|
7 |
|
if ($parametersA[$i]->name !== $parametersB[$i]->name) { |
37
|
|
|
$changes['parameter_renamed'] = true; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
|
|
// Type checking |
41
|
|
|
if (Type::get($parametersA[$i]->type) !== Type::get($parametersB[$i]->type)) { |
42
|
7 |
|
// if ($paramsA[$i]->default !== null && $paramsB[$i]->default !== null) { |
|
|
|
|
43
|
7 |
|
// $changes['parameter_default_value_changed'] = true; |
44
|
4 |
|
if ($parametersA[$i]->type !== null) { |
45
|
4 |
|
$changes['parameter_typing_removed'] = true; |
46
|
|
|
} |
47
|
|
|
if ($parametersB[$i]->type !== null) { |
48
|
3 |
|
$changes['parameter_typing_added'] = true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Default checking |
53
|
|
|
if ($parametersA[$i]->default === null && $parametersB[$i]->default === null) { |
|
|
|
|
54
|
|
|
// Do nothing |
55
|
|
|
} elseif ($parametersA[$i]->default !== null && $parametersB[$i]->default === null) { |
56
|
|
|
$changes['parameter_default_removed'] = true; |
57
|
|
|
} elseif ($parametersA[$i]->default === null && $parametersB[$i]->default !== null) { |
58
|
|
|
$changes['parameter_default_added'] = true; |
59
|
|
|
// TODO([email protected]): Not all nodes have a value property |
60
|
|
|
} elseif ( ! Node::isEqual($parametersA[$i]->default, $parametersB[$i]->default)) { |
61
|
|
|
$changes['parameter_default_value_changed'] = true; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $changes; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $paramsA |
70
|
|
|
* @param array $paramsB |
71
|
|
|
* @return string|bool |
72
|
|
|
*/ |
73
|
|
|
public static function requiredParametersChanges(array $paramsA, array $paramsB) |
74
|
|
|
{ |
75
|
|
|
$iterations = min(count($paramsA), count($paramsB)); |
76
|
|
|
// Verify that all params default value match (either they're both null => parameter is required, or both |
77
|
|
|
// set to the same required value) |
78
|
|
|
for ($i = 0; $i < $iterations; ++$i) { |
79
|
|
|
if ($paramsA[$i]->default !== $paramsB[$i]->default) { |
80
|
|
|
return $paramsB[$i]->default ? 'removed' : 'added'; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Only one of these will return its remaining values, the other returning an empty array |
85
|
|
|
$toCheck = array_slice($paramsA, $iterations) + array_slice($paramsB, $iterations); |
86
|
|
|
$operation = count($paramsA) < count($paramsB) ? 'added' : 'removed'; |
87
|
|
|
// If any additional argument does not have a default value, the signature has changed |
88
|
|
|
foreach ($toCheck as $param) { |
89
|
|
|
if ($param->default === null) { |
90
|
|
|
return $operation; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $paramsA |
98
|
|
|
* @param array $paramsB |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public static function isSameTypehints(array $paramsA, array $paramsB) |
102
|
|
|
{ |
103
|
|
|
$iterations = min(count($paramsA), count($paramsB)); |
104
|
|
View Code Duplication |
for ($i = 0; $i < $iterations; ++$i) { |
|
|
|
|
105
|
|
|
// TODO: Allow for contravariance <[email protected]> |
106
|
|
|
if ( ! Type::isSame($paramsA[$i]->type, $paramsB[$i]->type)) { |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
// Only one of these will return its remaining values, the other returning an empty array |
111
|
|
|
$toCheck = array_slice($paramsA, $iterations) + array_slice($paramsB, $iterations); |
112
|
|
|
// If any additional argument does not have a default value, the signature has changed |
113
|
|
|
foreach ($toCheck as $param) { |
114
|
|
|
if ($param->default === null) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $paramsA |
123
|
|
|
* @param array $paramsB |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public static function isSameVariables(array $paramsA, array $paramsB) |
127
|
|
|
{ |
128
|
|
|
if (count($paramsA) !== count($paramsB)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$iterations = min(count($paramsA), count($paramsB)); |
133
|
|
View Code Duplication |
for ($i = 0; $i < $iterations; ++$i) { |
|
|
|
|
134
|
|
|
if ($paramsA[$i]->name != $paramsB[$i]->name) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.