Completed
Pull Request — master (#230)
by Juliette
03:24
created

PregReplaceEModifierSniffTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 133
Duplicated Lines 8.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 11
loc 133
rs 10
c 0
b 0
f 0

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
 * preg_replace() /e modifier sniff test file
4
 *
5
 * @package PHPCompatibility
6
 */
7
8
9
/**
10
 * preg_replace() /e modifier sniff tests
11
 *
12
 * @uses BaseSniffTest
13
 * @package PHPCompatibility
14
 * @author Jansen Price <[email protected]>
15
 */
16
class PregReplaceEModifierSniffTest extends BaseSniffTest
17
{
18
19
    const TEST_FILE = 'sniff-examples/preg_replace_e_modifier.php';
20
21
    /**
22
     * testDeprecatedEModifier
23
     *
24
     * @group pregReplaceEModifier
25
     *
26
     * @dataProvider dataDeprecatedEModifier
27
     *
28
     * @param int    $line         Line number where the error should occur.
29
     * @param string $functionName Function name.
30
     *
31
     * @return void
32
     */
33
    public function testDeprecatedEModifier($line, $functionName = 'preg_replace')
34
    {
35
        $file = $this->sniffFile(self::TEST_FILE, '5.4');
36
        $this->assertNoViolation($file, $line);
37
38
        $file = $this->sniffFile(self::TEST_FILE, '5.5');
39
        $this->assertError($file, $line, "{$functionName}() - /e modifier is deprecated since PHP 5.5");
40
41
        $file = $this->sniffFile(self::TEST_FILE, '7.0');
42
        $this->assertError($file, $line, "{$functionName}() - /e modifier is forbidden since PHP 7.0");
43
    }
44
45
    /**
46
     * dataDeprecatedEModifier
47
     *
48
     * @see testDeprecatedEModifier()
49
     *
50
     * @return array
51
     */
52
    public function dataDeprecatedEModifier() {
53
        return array(
54
            // preg_replace()
55
            array(50),
56
            array(51),
57
            array(54),
58
            array(55),
59
            array(58),
60
            array(59),
61
            array(60),
62
            array(63),
63
            array(78),
64
            array(84),
65
66
            // Bracket delimiters.
67
            array(99),
68
            array(100),
69
            array(104),
70
            array(106),
71
            array(108),
72
73
            // preg_filter()
74
            array(114, 'preg_filter'),
75
            array(115, 'preg_filter'),
76
            array(118, 'preg_filter'),
77
            array(119, 'preg_filter'),
78
            array(122, 'preg_filter'),
79
            array(123, 'preg_filter'),
80
            array(124, 'preg_filter'),
81
            array(127, 'preg_filter'),
82
            array(142, 'preg_filter'),
83
            array(148, 'preg_filter'),
84
        );
85
    }
86
87
88
    /**
89
     * testNoViolation
90
     *
91
     * @group pregReplaceEModifier
92
     *
93
     * @dataProvider dataNoViolation
94
     *
95
     * @param int $line Line number where no error should occur.
96
     *
97
     * @return void
98
     */
99
    public function testNoViolation($line)
100
    {
101
        $file = $this->sniffFile(self::TEST_FILE, '5.4');
102
        $this->assertNoViolation($file, $line);
103
104
        $file = $this->sniffFile(self::TEST_FILE, '5.5');
105
        $this->assertNoViolation($file, $line);
106
107
        $file = $this->sniffFile(self::TEST_FILE, '7.0');
108
        $this->assertNoViolation($file, $line);
109
    }
110
111
    /**
112
     * dataNoViolation
113
     *
114
     * @see testNoViolation()
115
     *
116
     * @return array
117
     */
118
    public function dataNoViolation() {
119
        return array(
120
            // No or only valid modifiers.
121
            array(9),
122
            array(10),
123
            array(13),
124
            array(14),
125
            array(17),
126
            array(18),
127
            array(21),
128
            array(24),
129
            array(39),
130
            array(45),
131
132
            // Untestable regex (variable, constant, function call).
133
            array(94),
134
            array(95),
135
            array(96),
136
137
            // Bracket delimiters.
138
            array(101),
139
            array(102),
140
            array(103),
141
            array(105),
142
            array(107),
143
            array(109),
144
145
        );
146
    }
147
148
}
149