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

ValidIntegersSniffTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 153
Duplicated Lines 15.69 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 24
loc 153
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
 * Valid Integers Sniff test file
4
 *
5
 * @package PHPCompatibility
6
 */
7
8
9
/**
10
 * Valid Integers Sniff tests
11
 *
12
 * @uses    BaseSniffTest
13
 * @package PHPCompatibility
14
 * @author  Juliette Reinders Folmer <[email protected]>
15
 */
16
class ValidIntegersSniffTest extends BaseSniffTest
17
{
18
19
    const TEST_FILE = 'sniff-examples/valid_integers.php';
20
21
    /**
22
     * Sniffed file
23
     *
24
     * @var PHP_CodeSniffer_File
25
     */
26
    protected $_sniffFile;
27
28
    /**
29
     * setUp
30
     *
31
     * @return void
32
     */
33
    public function setUp()
34
    {
35
        parent::setUp();
36
37
        $this->_sniffFile = $this->sniffFile(self::TEST_FILE);
38
    }
39
40
41
    /**
42
     * testBinaryInteger
43
     *
44
     * @group ValidIntegers
45
     *
46
     * @dataProvider dataBinaryInteger
47
     *
48
     * @param int    $line  Line number where the error should occur.
49
     * @param string $octal (Start of) Binary number as a string.
50
     * @param bool   $testNoViolation Whether or not to test for noViolation.
51
     *               Defaults to true. Set to false if another error is
52
     *               expected on the same line (invalid binary)
53
     *
54
     * @return void
55
     */
56
    public function testBinaryInteger($line, $binary, $testNoViolation = true)
57
    {
58
		
59
        $file = $this->sniffFile(self::TEST_FILE, '5.3');
60
        $this->assertError($file, $line, 'Binary integer literals were not present in PHP version 5.3 or earlier. Found: ' . $binary);
61
62
63
		if ($testNoViolation === true) {
64
	        $file = $this->sniffFile(self::TEST_FILE, '5.4');
65
	        $this->assertNoViolation($file, $line);
66
		}
67
    }
68
69
    /**
70
     * dataBinaryInteger
71
     *
72
     * @see testBinaryInteger()
73
     *
74
     * @return array
75
     */
76
    public function dataBinaryInteger() {
77
        return array(
78
            array(3, '0b001001101', true),
79
            array(4, '0b01', false),
80
        );
81
    }
82
83
84
    /**
85
     * testInvalidBinaryInteger
86
     *
87
     * @group ValidIntegers
88
     *
89
     * @return void
90
     */
91
    public function testInvalidBinaryInteger()
92
    {
93
        $this->assertError($this->_sniffFile, 4, 'Invalid binary integer detected. Found: 0b0123456');
94
    }
95
96
97
    /**
98
     * testInvalidOctalInteger
99
     *
100
     * @group ValidIntegers
101
     *
102
     * @dataProvider dataInvalidOctalInteger
103
     *
104
     * @param int    $line  Line number where the error should occur.
105
     * @param string $octal Octal number as a string.
106
     *
107
     * @return void
108
     */
109
    public function testInvalidOctalInteger($line, $octal)
110
    {
111
        $error = 'Invalid octal integer detected. Prior to PHP 7 this would lead to a truncated number. From PHP 7 onwards this causes a parse error. Found: ' . $octal;
112
113
        $file = $this->sniffFile(self::TEST_FILE, '5.4');
114
        $this->assertWarning($file, $line, $error);
115
116
        $file = $this->sniffFile(self::TEST_FILE, '7.0');
117
        $this->assertError($file, $line, $error);
118
    }
119
120
    /**
121
     * dataInvalidOctalInteger
122
     *
123
     * @see testInvalidOctalInteger()
124
     *
125
     * @return array
126
     */
127
    public function dataInvalidOctalInteger() {
128
        return array(
129
            array(7, '08'),
130
            array(8, '038'),
131
            array(9, '091'),
132
        );
133
    }
134
135
136
    /**
137
     * testValidOctalInteger
138
     *
139
     * @group ValidIntegers
140
     *
141
     * @return void
142
     */
143
    public function testValidOctalInteger() {
144
        $this->assertNoViolation($this->_sniffFile, 6);
145
    }
146
147
148
    /**
149
     * testHexNumericString
150
     *
151
     * @group ValidIntegers
152
     *
153
     * @return void
154
     */
155
    public function testHexNumericString()
156
    {
157
        $error = 'The behaviour of hexadecimal numeric strings was inconsistent prior to PHP 7 and support has been removed in PHP 7. Found: \'0xaa78b5\'';
158
159
        $file = $this->sniffFile(self::TEST_FILE, '5.0');
160
        $this->assertWarning($file, 11, $error);
161
        $this->assertNoViolation($file, 12);
162
163
        $file = $this->sniffFile(self::TEST_FILE, '7.0');
164
        $this->assertError($file, 11, $error);
165
        $this->assertNoViolation($file, 12);
166
    }
167
168
}
169