IfElse::flipElseIf()   C
last analyzed

Complexity

Conditions 16
Paths 12

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 5.5666
c 0
b 0
f 0
cc 16
nc 12
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Bug introduced by
The variable $ifBlockStartIndex does not exist. Did you forget to declare it?

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.

Loading history...
Bug introduced by
The variable $ifBlockEndIndex does not exist. Did you forget to declare it?

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.

Loading history...
31
        [$elseBodyStartIndex, $elseBody, $elseBodyEndIndex] = $elseBody;
0 ignored issues
show
Bug introduced by
The variable $elseBodyStartIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $elseBodyEndIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
32
        [$conditionStartIndex, $condition, $conditionCloseIndex] = $condition;
0 ignored issues
show
Bug introduced by
The variable $conditionStartIndex does not exist. Did you mean $condition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $conditionCloseIndex does not exist. Did you mean $condition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
33
34
        $refactoredTokens = [];
35
        foreach ($tokens as $i => $oldToken) {
36
            // negate the condition
37
            if ($conditionStartIndex == $i) {
0 ignored issues
show
Bug introduced by
The variable $conditionStartIndex does not exist. Did you mean $condition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The variable $conditionStartIndex does not exist. Did you mean $condition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $conditionCloseIndex does not exist. Did you mean $condition?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The variable $elseBodyStartIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
64
                continue;
65
            }
66
67
            // close the body and what was in the else block after it.
68
            if ($i == $elseBodyStartIndex) {
0 ignored issues
show
Bug introduced by
The variable $elseBodyStartIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
69
                $refactoredTokens[] = '}';
70
                foreach ($ifBody as $t) {
71
                    $refactoredTokens[] = $t;
72
                }
73
            }
74
75
            // ignore the else body.
76
            if ($i >= $elseBodyStartIndex && $i <= $elseBodyEndIndex) {
0 ignored issues
show
Bug introduced by
The variable $elseBodyStartIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The variable $elseBodyEndIndex does not exist. Did you mean $elseBody?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
77
                continue;
78
            }
79
80
            $refactoredTokens[] = $oldToken;
81
        }
82
83
        return $refactoredTokens;
84
    }
85
}
86