|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* New type declarations test file |
|
4
|
|
|
* |
|
5
|
|
|
* @package PHPCompatibility |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* New type declarations test file |
|
11
|
|
|
* |
|
12
|
|
|
* @uses BaseSniffTest |
|
13
|
|
|
* @package PHPCompatibility |
|
14
|
|
|
* @author Wim Godden <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class NewScalarTypeDeclarationsSniffTest extends BaseSniffTest |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
const TEST_FILE = 'sniff-examples/new_scalar_type_declarations.php'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* testNewTypeDeclaration |
|
22
|
|
|
* |
|
23
|
|
|
* @group TypeDeclarations |
|
24
|
|
|
* |
|
25
|
|
|
* @dataProvider dataNewTypeDeclaration |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $type The scalar type. |
|
28
|
|
|
* @param int $line Line number on which to expect an error. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testNewTypeDeclaration($type, $lastVersionBefore, $line, $okVersion) |
|
33
|
|
|
{ |
|
34
|
|
|
$file = $this->sniffFile(self::TEST_FILE, $lastVersionBefore); |
|
35
|
|
|
$this->assertError($file, $line, "'{$type}' type declaration is not present in PHP version {$lastVersionBefore} or earlier"); |
|
36
|
|
|
|
|
37
|
|
|
$file = $this->sniffFile(self::TEST_FILE, $okVersion); |
|
38
|
|
|
$this->assertNoViolation($file, $line); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Data provider. |
|
43
|
|
|
* |
|
44
|
|
|
* @see testNewTypeDeclaration() |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function dataNewTypeDeclaration() |
|
49
|
|
|
{ |
|
50
|
|
|
return array( |
|
51
|
|
|
array('array', '5.0', 4, '5.1'), |
|
52
|
|
|
array('array', '5.0', 5, '5.1'), |
|
53
|
|
|
array('callable', '5.3', 9, '5.4'), |
|
54
|
|
|
array('bool', '5.6', 13, '7.0'), |
|
55
|
|
|
array('int', '5.6', 14, '7.0'), |
|
56
|
|
|
array('float', '5.6', 15, '7.0'), |
|
57
|
|
|
array('string', '5.6', 16, '7.0'), |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* testInvalidTypeDeclaration |
|
64
|
|
|
* |
|
65
|
|
|
* @group TypeDeclarations |
|
66
|
|
|
* |
|
67
|
|
|
* @dataProvider dataInvalidTypeDeclaration |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $type The scalar type. |
|
70
|
|
|
* @param int $line Line number on which to expect an error. |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testInvalidTypeDeclaration($type, $alternative, $line) |
|
75
|
|
|
{ |
|
76
|
|
|
$file = $this->sniffFile(self::TEST_FILE); |
|
77
|
|
|
$this->assertError($file, $line, "'{$type}' is not a valid type declaration. Did you mean {$alternative} ?"); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Data provider. |
|
82
|
|
|
* |
|
83
|
|
|
* @see testInvalidTypeDeclaration() |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function dataInvalidTypeDeclaration() |
|
88
|
|
|
{ |
|
89
|
|
|
return array( |
|
90
|
|
|
array('boolean', 'bool', 20), |
|
91
|
|
|
array('integer', 'int', 21), |
|
92
|
|
|
array('parent', 'self', 24), |
|
93
|
|
|
array('static', 'self', 25), |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* testInvalidSelfTypeDeclaration |
|
100
|
|
|
* |
|
101
|
|
|
* @group TypeDeclarations |
|
102
|
|
|
* |
|
103
|
|
|
* @dataProvider dataInvalidSelfTypeDeclaration |
|
104
|
|
|
* |
|
105
|
|
|
* @param int $line Line number on which to expect an error. |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testInvalidSelfTypeDeclaration($line) |
|
110
|
|
|
{ |
|
111
|
|
|
$file = $this->sniffFile(self::TEST_FILE); |
|
112
|
|
|
$this->assertError($file, $line, "'self' type cannot be used outside of class scope"); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Data provider. |
|
117
|
|
|
* |
|
118
|
|
|
* @see testInvalidSelfTypeDeclaration() |
|
119
|
|
|
* |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
public function dataInvalidSelfTypeDeclaration() |
|
123
|
|
|
{ |
|
124
|
|
|
return array( |
|
125
|
|
|
array(37), |
|
126
|
|
|
array(44), |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* testTypeDeclaration |
|
133
|
|
|
* |
|
134
|
|
|
* @group TypeDeclarations |
|
135
|
|
|
* |
|
136
|
|
|
* @dataProvider dataTypeDeclaration |
|
137
|
|
|
* |
|
138
|
|
|
* @param int $line Line number on which to expect an error. |
|
139
|
|
|
* @param bool $testNoViolation Whether or not to test noViolation for PHP 5.0. |
|
140
|
|
|
* This covers the remaining few cases not covered |
|
141
|
|
|
* by the above tests. |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
|
View Code Duplication |
public function testTypeDeclaration($line, $testNoViolation = false) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '4.4'); |
|
148
|
|
|
$this->assertError($file, $line, 'Type hints were not present in PHP 4.4 or earlier'); |
|
149
|
|
|
|
|
150
|
|
|
if ($testNoViolation === true) { |
|
151
|
|
|
$file = $this->sniffFile(self::TEST_FILE, '5.0'); |
|
152
|
|
|
$this->assertNoViolation($file, $line); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Data provider. |
|
158
|
|
|
* |
|
159
|
|
|
* @see testTypeDeclaration() |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
*/ |
|
163
|
|
|
public function dataTypeDeclaration() |
|
164
|
|
|
{ |
|
165
|
|
|
return array( |
|
166
|
|
|
array(4), |
|
167
|
|
|
array(5), |
|
168
|
|
|
array(9), |
|
169
|
|
|
array(13), |
|
170
|
|
|
array(14), |
|
171
|
|
|
array(15), |
|
172
|
|
|
array(16), |
|
173
|
|
|
array(20), |
|
174
|
|
|
array(21), |
|
175
|
|
|
array(24), |
|
176
|
|
|
array(25), |
|
177
|
|
|
array(29, true), |
|
178
|
|
|
array(34, true), |
|
179
|
|
|
array(37), |
|
180
|
|
|
array(41, true), |
|
181
|
|
|
array(44), |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.