Completed
Push — master ( 9cb63b...91066a )
by Wim
8s
created

ValidIntegersSniffTest::dataBinaryInteger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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.
0 ignored issues
show
Bug introduced by
There is no parameter named $octal. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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 View Code Duplication
    public function testHexNumericString()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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