1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPSemVerChecker\Analyzer; |
4
|
|
|
|
5
|
|
|
use PHPSemVerChecker\Comparator\Signature; |
6
|
|
|
use PHPSemVerChecker\Operation\FunctionAdded; |
7
|
|
|
use PHPSemVerChecker\Operation\FunctionImplementationChanged; |
8
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterChanged; |
9
|
|
|
use PHPSemVerChecker\Operation\FunctionParameterNameChanged; |
10
|
|
|
use PHPSemVerChecker\Operation\FunctionRemoved; |
11
|
|
|
use PHPSemVerChecker\Operation\Unknown; |
12
|
|
|
use PHPSemVerChecker\Registry\Registry; |
13
|
|
|
use PHPSemVerChecker\Report\Report; |
14
|
|
|
|
15
|
|
|
class FunctionAnalyzer { |
16
|
|
|
protected $context = 'function'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryBefore |
20
|
|
|
* @param \PHPSemVerChecker\Registry\Registry $registryAfter |
21
|
|
|
* @return \PHPSemVerChecker\Report\Report |
22
|
|
|
*/ |
23
|
10 |
|
public function analyze(Registry $registryBefore, Registry $registryAfter) |
24
|
|
|
{ |
25
|
10 |
|
$report = new Report(); |
26
|
|
|
|
27
|
10 |
|
$keysBefore = array_keys($registryBefore->data['function']); |
28
|
10 |
|
$keysAfter = array_keys($registryAfter->data['function']); |
29
|
10 |
|
$added = array_diff($keysAfter, $keysBefore); |
30
|
10 |
|
$removed = array_diff($keysBefore, $keysAfter); |
31
|
10 |
|
$toVerify = array_intersect($keysBefore, $keysAfter); |
32
|
|
|
|
33
|
10 |
|
foreach ($removed as $key) { |
34
|
1 |
|
$fileBefore = $registryBefore->mapping['function'][$key]; |
35
|
1 |
|
$functionBefore = $registryBefore->data['function'][$key]; |
36
|
|
|
|
37
|
1 |
|
$data = new FunctionRemoved($fileBefore, $functionBefore); |
38
|
1 |
|
$report->addFunction($data); |
39
|
|
|
} |
40
|
|
|
|
41
|
10 |
|
foreach ($toVerify as $key) { |
42
|
7 |
|
$fileBefore = $registryBefore->mapping['function'][$key]; |
43
|
7 |
|
$functionBefore = $registryBefore->data['function'][$key]; |
44
|
7 |
|
$fileAfter = $registryAfter->mapping['function'][$key]; |
45
|
7 |
|
$functionAfter = $registryAfter->data['function'][$key]; |
46
|
|
|
|
47
|
|
|
// Leave non-strict comparison here |
48
|
7 |
|
if ($functionBefore != $functionAfter) { |
49
|
4 |
|
$paramsBefore = $functionBefore->params; |
50
|
4 |
|
$paramsAfter = $functionAfter->params; |
51
|
|
|
// Signature |
52
|
|
|
|
53
|
4 |
View Code Duplication |
if ( ! Signature::isSameTypehints($paramsBefore, $paramsAfter)) { |
|
|
|
|
54
|
2 |
|
$data = new FunctionParameterChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter); |
55
|
2 |
|
$report->addFunction($data); |
56
|
2 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
View Code Duplication |
if ( ! Signature::isSameVariables($paramsBefore, $paramsAfter)) { |
|
|
|
|
60
|
1 |
|
$data = new FunctionParameterNameChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter); |
61
|
1 |
|
$report->addFunction($data); |
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Different length (considering params with defaults) |
66
|
|
|
|
67
|
|
|
// Difference in source code |
68
|
1 |
|
if ($functionBefore->stmts != $functionAfter->stmts) { |
69
|
1 |
|
$data = new FunctionImplementationChanged($fileBefore, $functionBefore, $fileAfter, $functionAfter); |
70
|
1 |
|
$report->addFunction($data); |
71
|
4 |
|
continue; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
foreach ($added as $key) { |
77
|
1 |
|
$fileAfter = $registryAfter->mapping['function'][$key]; |
78
|
1 |
|
$functionAfter = $registryAfter->data['function'][$key]; |
79
|
|
|
|
80
|
1 |
|
$data = new FunctionAdded($fileAfter, $functionAfter); |
81
|
1 |
|
$report->addFunction($data); |
82
|
|
|
} |
83
|
|
|
|
84
|
10 |
|
return $report; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.