Condition::negate()   B
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 53

Duplication

Lines 4
Ratio 7.55 %

Importance

Changes 0
Metric Value
cc 9
nc 10
nop 1
dl 4
loc 53
rs 7.4698
c 0
b 0
f 0

How to fix   Long Method   

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
class Condition
6
{
7
    public static function negate($conditionTokens)
8
    {
9
        $found = false;
10
11
        $ops = [
12
            '==',
13
            '===',
14
            '>',
15
            '<',
16
            '>=',
17
            '<=',
18
            '!=',
19
            '!==',
20
        ];
21
22
        $logic = ['&&', '||', 'or', 'and', '?:', '??', '-', '+', '*', '**', '%', '<=>'];
23
24
        $ops = array_merge($ops, $logic);
25
26
        $comparison = [
27
            '!=' => '==',
28
            '!==' => '===',
29
            '<=' => '>',
30
            '>=' => '<',
31
            '<' => '>=',
32
            '>' => '<=',
33
            '==' => '!=',
34
            '===' => '!==',
35
        ];
36
        if (self::count($conditionTokens, $comparison) == 1 &&
37
            self::count($conditionTokens, $logic) == 0) {
38
            return self::replace($conditionTokens, $comparison);
39
        }
40
41
        foreach ($conditionTokens as $t) {
42 View Code Duplication
            if (\in_array($t[1] ?? $t[0], $ops)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
43
                $found = true;
44
                break;
45
            }
46
        }
47
48
        if (! $found && $conditionTokens[0] != '!') {
49
            array_unshift($conditionTokens, '!');
50
        } elseif (! $found && $conditionTokens[0] == '!') {
51
            array_shift($conditionTokens);
52
        } else {
53
            array_unshift($conditionTokens, '(');
54
            array_unshift($conditionTokens, '!');
55
            $conditionTokens[] = ')';
56
        }
57
58
        return $conditionTokens;
59
    }
60
61
    private static function count($conditionTokens, $ops)
62
    {
63
        $level = $found = 0;
64
        foreach ($conditionTokens as $t) {
65
            $t == '(' && $level++;
66
            $t == ')' && $level--;
67 View Code Duplication
            if ($level === 0 && \in_array($t[1] ?? $t[0], $ops)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
68
                $found++;
69
            }
70
        }
71
72
        return $found;
73
    }
74
75
    private static function replace($conditionTokens, $ops)
76
    {
77
        $newTokens = [];
78
        foreach ($conditionTokens as $token) {
79
            $char = \is_array($token) ? $token[1] : $token[0];
80
            if (isset($ops[$char])) {
81
                $r = (array) $token;
82
                $r[1] = $ops[$char];
83
84
                $newTokens[] = $r;
85
            } else {
86
                $newTokens[] = $token;
87
            }
88
        }
89
90
        return $newTokens;
91
    }
92
}
93