1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Comparator; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node\FunctionLike; |
6
|
|
|
|
7
|
|
|
class Signature |
8
|
|
|
{ |
9
|
|
|
|
10
|
78 |
|
public static function analyze(FunctionLike $functionA, FunctionLike $functionB) |
11
|
|
|
{ |
12
|
|
|
$changes = [ |
13
|
78 |
|
'function_renamed' => false, |
14
|
|
|
'function_renamed_case_only' => false, |
15
|
|
|
'parameter_added' => false, |
16
|
|
|
'parameter_removed' => false, |
17
|
|
|
'parameter_renamed' => false, |
18
|
|
|
'parameter_typing_added' => false, |
19
|
|
|
'parameter_typing_removed' => false, |
20
|
|
|
'parameter_default_added' => false, |
21
|
|
|
'parameter_default_removed' => false, |
22
|
|
|
'parameter_default_value_changed' => false, |
23
|
|
|
]; |
24
|
|
|
|
25
|
78 |
|
$changes = self::detectFunctionNameChanges($changes, $functionA, $functionB); |
26
|
78 |
|
$changes = self::detectParameterChanges($changes, $functionA, $functionB); |
27
|
|
|
|
28
|
78 |
|
return $changes; |
29
|
|
|
} |
30
|
|
|
|
31
|
78 |
|
private static function detectFunctionNameChanges($changes, FunctionLike $functionA, FunctionLike $functionB) |
32
|
|
|
{ |
33
|
78 |
|
if ($functionA->name != $functionB->name) { |
|
|
|
|
34
|
|
|
|
35
|
3 |
|
if (strtolower($functionA->name) == strtolower($functionB->name)) { |
|
|
|
|
36
|
2 |
|
$changes['function_renamed_case_only'] = true; |
37
|
|
|
} else { |
38
|
1 |
|
$changes['function_renamed'] = true; |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
return $changes; |
42
|
|
|
} |
43
|
|
|
|
44
|
75 |
|
return $changes; |
45
|
|
|
} |
46
|
|
|
|
47
|
78 |
|
private static function detectParameterChanges($changes, FunctionLike $functionA, FunctionLike $functionB) |
48
|
|
|
{ |
49
|
78 |
|
$parametersA = $functionA->getParams(); |
50
|
78 |
|
$parametersB = $functionB->getParams(); |
51
|
|
|
|
52
|
78 |
|
$lengthA = count($parametersA); |
53
|
78 |
|
$lengthB = count($parametersB); |
54
|
|
|
|
55
|
|
|
// TODO([email protected]): This is only true if newer params do not have defaults |
56
|
78 |
|
if ($lengthA < $lengthB) { |
57
|
10 |
|
$changes['parameter_added'] = true; |
58
|
68 |
|
} elseif ($lengthA > $lengthB) { |
59
|
8 |
|
$changes['parameter_removed'] = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
78 |
|
$iterations = min($lengthA, $lengthB); |
63
|
78 |
|
for ($i = 0; $i < $iterations; ++$i) { |
64
|
|
|
// Name checking |
65
|
69 |
|
if ($parametersA[$i]->name !== $parametersB[$i]->name) { |
66
|
11 |
|
$changes['parameter_renamed'] = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Type checking |
70
|
69 |
|
if (Type::get($parametersA[$i]->type) !== Type::get($parametersB[$i]->type)) { |
|
|
|
|
71
|
|
|
// if ($paramsA[$i]->default !== null && $paramsB[$i]->default !== null) { |
|
|
|
|
72
|
|
|
// $changes['parameter_default_value_changed'] = true; |
73
|
16 |
|
if ($parametersA[$i]->type !== null) { |
74
|
8 |
|
$changes['parameter_typing_removed'] = true; |
75
|
|
|
} |
76
|
16 |
|
if ($parametersB[$i]->type !== null) { |
77
|
8 |
|
$changes['parameter_typing_added'] = true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Default checking |
82
|
69 |
|
if ($parametersA[$i]->default === null && $parametersB[$i]->default === null) { |
|
|
|
|
83
|
|
|
// Do nothing |
84
|
24 |
|
} elseif ($parametersA[$i]->default !== null && $parametersB[$i]->default === null) { |
85
|
8 |
|
$changes['parameter_default_removed'] = true; |
86
|
16 |
|
} elseif ($parametersA[$i]->default === null && $parametersB[$i]->default !== null) { |
87
|
8 |
|
$changes['parameter_default_added'] = true; |
88
|
|
|
// TODO([email protected]): Not all nodes have a value property |
89
|
8 |
|
} elseif (!Node::isEqual($parametersA[$i]->default, $parametersB[$i]->default)) { |
|
|
|
|
90
|
8 |
|
$changes['parameter_default_value_changed'] = true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
78 |
|
return $changes; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: