|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope\Refactors; |
|
4
|
|
|
|
|
5
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor; |
|
6
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\TokenManager; |
|
7
|
|
|
|
|
8
|
|
|
class IfElse |
|
9
|
|
|
{ |
|
10
|
|
|
public static function refactorElseIf($tokens, $ifBody, $elseBody, $condition) |
|
11
|
|
|
{ |
|
12
|
|
|
if (Refactor::isBlocky($elseBody[1]) && self::shouldBeFlipped(\count($elseBody[1]), \count($ifBody[1]))) { |
|
13
|
|
|
return self::flipElseIf($tokens, $condition, $ifBody, $elseBody); |
|
14
|
|
|
} elseif (Refactor::isBlocky($ifBody[1])) { |
|
15
|
|
|
return TokenManager::removeTokens($tokens, $ifBody[2], $elseBody[0], $elseBody[2]); |
|
16
|
|
|
} else { |
|
17
|
|
|
return null; |
|
18
|
|
|
} |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
private static function shouldBeFlipped($elseCount, $ifBody) |
|
22
|
|
|
{ |
|
23
|
|
|
$ifIsLonger = ($elseCount + 10) < $ifBody; |
|
24
|
|
|
|
|
25
|
|
|
return $ifIsLonger || ($elseCount < $ifBody * 0.7); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private static function flipElseIf($tokens, $condition, $ifBody, $elseBody) |
|
29
|
|
|
{ |
|
30
|
|
|
[$ifBlockStartIndex, $ifBody, $ifBlockEndIndex] = $ifBody; |
|
|
|
|
|
|
31
|
|
|
[$elseBodyStartIndex, $elseBody, $elseBodyEndIndex] = $elseBody; |
|
|
|
|
|
|
32
|
|
|
[$conditionStartIndex, $condition, $conditionCloseIndex] = $condition; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
$refactoredTokens = []; |
|
35
|
|
|
foreach ($tokens as $i => $oldToken) { |
|
36
|
|
|
// negate the condition |
|
37
|
|
|
if ($conditionStartIndex == $i) { |
|
|
|
|
|
|
38
|
|
|
$refactoredTokens[] = '('; |
|
39
|
|
|
$negatedConditionTokens = Condition::negate($condition); |
|
40
|
|
|
foreach ($negatedConditionTokens as $t) { |
|
41
|
|
|
$refactoredTokens[] = $t; |
|
42
|
|
|
} |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if ($i >= $conditionStartIndex && $i < $conditionCloseIndex) { |
|
|
|
|
|
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($i == $ifBlockStartIndex) { |
|
51
|
|
|
$refactoredTokens[] = '{'; |
|
52
|
|
|
foreach ($elseBody as $t) { |
|
53
|
|
|
$refactoredTokens[] = $t; |
|
54
|
|
|
} |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($i > $ifBlockStartIndex && $i < $ifBlockEndIndex) { |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// removes: } else { |
|
63
|
|
|
if ($i >= $ifBlockEndIndex && $i < $elseBodyStartIndex) { |
|
|
|
|
|
|
64
|
|
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// close the body and what was in the else block after it. |
|
68
|
|
|
if ($i == $elseBodyStartIndex) { |
|
|
|
|
|
|
69
|
|
|
$refactoredTokens[] = '}'; |
|
70
|
|
|
foreach ($ifBody as $t) { |
|
71
|
|
|
$refactoredTokens[] = $t; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// ignore the else body. |
|
76
|
|
|
if ($i >= $elseBodyStartIndex && $i <= $elseBodyEndIndex) { |
|
|
|
|
|
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$refactoredTokens[] = $oldToken; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $refactoredTokens; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.