Condition   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 88
Duplicated Lines 7.95 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 7
loc 88
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B negate() 4 53 9
A count() 3 13 6
A replace() 0 17 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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