Ifs::readCondition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Analyzers;
4
5
use Imanghafoori\LaravelMicroscope\Refactors\IfElse;
6
use Imanghafoori\LaravelMicroscope\Refactors\NestedIf;
7
8
class Ifs
9
{
10
    public static function mergeIfs($tokens, $i)
11
    {
12
        $token = $tokens[$i];
13
14
        if ($token[0] !== T_IF) {
15
            return null;
16
        }
17
        $condition1 = self::readCondition($tokens, $i);
18
19
        [$char, $if1BlockStartIndex] = TokenManager::getNextToken($tokens, $condition1[2]);
0 ignored issues
show
Bug introduced by
The variable $char 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 $if1BlockStartIndex 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...
20
        // if with no curly brace.
21
        if ($char[0] == T_IF) {
22
            [$char,] = TokenManager::getNextToken($tokens, $if1BlockStartIndex);
23
            $condition2 = self::readCondition($tokens, $if1BlockStartIndex);
24
25
            $if2Body = self::readBody($tokens, $condition2[2]);
26
27
            $afterSecondIf = TokenManager::getNextToken($tokens, $if2Body[2]);
28
29
            if (T_ELSEIF !== $afterSecondIf[0][0] && T_ELSE !== $afterSecondIf[0][0]) {
30
                return NestedIf::merge($tokens, $condition1[2], $condition2[0], -1);
31
            }
32
        }
33
34
        // if with no curly brace.
35
        if ($char[0] !== '{') {
36
            return null;
37
        }
38
39
        $if2index = self::forwardTo($tokens, $if1BlockStartIndex);
40
41
        if ($tokens[$if2index][0] !== T_IF) {
42
            return null;
43
        }
44
45
        $condition2 = self::readCondition($tokens, $if2index);
46
47
        $if2Body = self::readBody($tokens, $condition2[2]);
48
        [, $if1BodyCloseIndex] = TokenManager::readBody($tokens, $if1BlockStartIndex);
0 ignored issues
show
Bug introduced by
The variable $if1BodyCloseIndex 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...
49
        $if1closeIndexCandid = self::forwardTo($tokens, $if2Body[2]);
50
51
        if ($if1closeIndexCandid !== $if1BodyCloseIndex) {
52
            return null;
53
        }
54
55
        $afterFirstIf = TokenManager::getNextToken($tokens, $if1BodyCloseIndex);
56
57
        if (T_ELSEIF == $afterFirstIf[0][0] || T_ELSE == $afterFirstIf[0][0]) {
58
            return null;
59
        }
60
61
        return NestedIf::merge($tokens, $condition1[2], $condition2[0], $if2Body[2]);
62
    }
63
64
    public static function else_If($tokens, $i)
65
    {
66
        $token = $tokens[$i];
67
        if ($token[0] !== T_IF) {
68
            return null;
69
        }
70
71
        $condition = self::readCondition($tokens, $i);
72
73
        $ifBody = self::readBody($tokens, $condition[2]);
74
        if (! $ifBody[0]) {
75
            return null;
76
        }
77
78
        [$afterIf, $afterIfIndex] = TokenManager::getNextToken($tokens, $ifBody[2]);
0 ignored issues
show
Bug introduced by
The variable $afterIf 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 $afterIfIndex 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...
79
80
        // in order to cover both   } else {   and   else:   syntax.
81
        if (T_ELSE !== $afterIf[0] && $tokens[$ifBody[2]][0] !== T_ELSE) {
82
            return null;
83
        }
84
85
        $elseBody = self::readBody($tokens, $afterIfIndex);
86
87
        if (! $elseBody[0]) {
88
            return null;
89
        }
90
91
        return IfElse::refactorElseIf($tokens, $ifBody, $elseBody, $condition);
92
    }
93
94
    public static function readCondition($tokens, $i)
95
    {
96
        [, $conditionStartIndex] = TokenManager::forwardTo($tokens, $i, ['(']);
0 ignored issues
show
Bug introduced by
The variable $conditionStartIndex 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...
97
        [$condition, $conditionCloseIndex] = TokenManager::readBody($tokens, $conditionStartIndex, ')');
0 ignored issues
show
Bug introduced by
The variable $condition 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 $conditionCloseIndex 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...
98
99
        return [$conditionStartIndex, $condition, $conditionCloseIndex];
100
    }
101
102
    private static function readBody($tokens, $afterIfIndex)
103
    {
104
        [$char, $elseBodyStartIndex] = TokenManager::getNextToken($tokens, $afterIfIndex);
0 ignored issues
show
Bug introduced by
The variable $char 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 $elseBodyStartIndex 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...
105
        // if with no curly brace.
106
        if ($char[0] !== '{') {
107
            return [null, null, null];
108
        }
109
110
        [$elseBody, $elseBodyEndIndex] = TokenManager::readBody($tokens, $elseBodyStartIndex);
0 ignored issues
show
Bug introduced by
The variable $elseBody 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 $elseBodyEndIndex 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...
111
112
        return [$elseBodyStartIndex, $elseBody, $elseBodyEndIndex];
113
    }
114
115
    private static function forwardTo($tokens, $index)
116
    {
117
        while (\in_array($tokens[++$index][0], [T_WHITESPACE, T_COMMENT, ';'])) {
118
        }
119
120
        return $index;
121
    }
122
}
123