Skip to content

Commit 71144b5

Browse files
committed
Rewrite PSR12.Files.DeclareStatement sniff
1 parent a3d11a9 commit 71144b5

14 files changed

+480
-168
lines changed

src/Standards/PSR12/Sniffs/Files/DeclareStatementSniff.php

+285-157
Large diffs are not rendered by default.

src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ declare(/* test */ ticks /* test */ =1);
1313
declare( ticks
1414
=1 );
1515

16-
declare(ticks=1) { }
16+
declare(ticks=1) { $test = true; }
1717

1818
declare(ticks=1)
19-
{ }
19+
{ $test = true; }
2020

2121
declare(ticks=1)
2222
{
@@ -48,3 +48,7 @@ declare(ticks=1) {$test = true;
4848

4949
declare(ticks=1) { $test = true;
5050
}
51+
52+
// Ensure that we handle non-number values properly.
53+
declare(encoding='ISO-8859-1');
54+
declare ( encoding = 'ISO-8859-1' ) ;

src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.1.inc.fixed

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ declare(/* test */ ticks /* test */ =1);
1313
declare(ticks=1);
1414

1515
declare(ticks=1) {
16+
$test = true;
1617
}
1718

1819
declare(ticks=1) {
20+
$test = true;
1921
}
2022

2123
declare(ticks=1) {
@@ -26,7 +28,8 @@ declare(ticks=1) {
2628
}//end comment
2729

2830
declare(ticks=1) {
29-
}$x =1;
31+
}
32+
$x =1;
3033

3134
declare(ticks=1) {
3235
$test = true;
@@ -36,19 +39,24 @@ declare(ticks // phpcs:ignore Standard.Category.SniffName -- fixing is undesirab
3639
=1);
3740

3841
declare(ticks=1) {
39-
}$x =1; // phpcs:ignore Standard.Category.SniffName -- fixing is undesirable
42+
}
43+
$x =1; // phpcs:ignore Standard.Category.SniffName -- fixing is undesirable
4044

4145
declare(ticks=1) { // test
4246
}
4347

4448
declare(ticks=1) {
45-
$test = true;
49+
$test = true;
4650
}
4751

4852
declare(ticks=1) { /* test */ /* test */
49-
$test = true;
53+
$test = true;
5054
}
5155

5256
declare(ticks=1) {
53-
$test = true;
57+
$test = true;
5458
}
59+
60+
// Ensure that we handle non-number values properly.
61+
declare(encoding='ISO-8859-1');
62+
declare(encoding='ISO-8859-1');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing directive value.
3+
declare(ticks=)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing semicolon (or PHP close tag).
3+
declare(ticks=1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing closing curly bracket
3+
declare(ticks=1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// This is a parse error, but the sniff should still handle this correctly.
4+
declare($something = $value);
5+
6+
const STRICT_TYPES = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
declare(ticks=1);
3+
declare (ticks=1);
4+
declare( ticks=1);
5+
declare(ticks =1);
6+
declare(ticks= 1);
7+
declare(ticks=1 );
8+
declare(ticks=1) ;
9+
declare (ticks=1);
10+
declare( ticks=1);
11+
declare(ticks =1);
12+
declare(ticks= 1);
13+
declare(ticks=1 );
14+
declare(ticks=1) ;
15+
declare (ticks=1);
16+
declare( ticks=1);
17+
declare(ticks =1);
18+
declare(ticks= 1);
19+
declare(ticks=1 );
20+
declare(ticks=1) ;
21+
declare
22+
(ticks=1);
23+
declare(
24+
ticks=1);
25+
declare(ticks
26+
=1);
27+
declare(ticks=
28+
1);
29+
declare(ticks=1
30+
);
31+
declare(ticks=1)
32+
;
33+
declare
34+
(ticks=1);
35+
declare(
36+
ticks=1);
37+
declare(ticks
38+
=1);
39+
declare(ticks=
40+
1);
41+
declare(ticks=1
42+
);
43+
declare(ticks=1)
44+
;
45+
declare/* test */(ticks=1);
46+
declare(/* test */ticks=1);
47+
declare(ticks/* test */=1);
48+
declare(ticks=/* test */1);
49+
declare(ticks=1/* test */);
50+
declare(ticks=1)/* test */;
51+
declare(ticks=1);/* test */
52+
declare// test
53+
(ticks=1);
54+
declare(// test
55+
ticks=1);
56+
declare(ticks// test
57+
=1);
58+
declare(ticks=// test
59+
1);
60+
declare(ticks=1// test
61+
);
62+
declare(ticks=1)// test
63+
;
64+
declare(ticks=1);// test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1); ?>
2+
The line above is correct. The line below should complain that it's not the first line.
3+
<?php declare(strict_types=1); ?>
4+
5+
This line should complain. It's included because the optional semicolon is missing here.
6+
<?php declare(strict_types=1) ?>
7+
8+
This should complain that all three parts are not on the first line of the file.
9+
<?php
10+
declare(strict_types=1);
11+
?>
12+
13+
This is fine because it's not strict_types.
14+
<?php declare(ticks=1); ?>
15+
16+
These can be automatically fixed because there is an incorect number of spaces
17+
between the end of the declare statement and the closing PHP tag.
18+
<?php declare(ticks=1)?>
19+
<?php declare(ticks=1) ?>
20+
<?php declare(ticks=1)/* test */?>
21+
22+
This is missing its closing PHP tag. It should be the last test in this file.
23+
<?php declare(strict_types=1);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing opening parenthesis.
3+
declare
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing closing parenthesis.
3+
declare(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing directive.
3+
declare()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Parse error. Missing equals sign.
3+
declare(ticks)

src/Standards/PSR12/Tests/Files/DeclareStatementUnitTest.php

+62-4
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,77 @@ public function getErrorList($testFile='')
4545
10 => 1,
4646
11 => 3,
4747
12 => 2,
48-
13 => 1,
49-
14 => 2,
48+
13 => 2,
49+
14 => 1,
5050
16 => 3,
51+
18 => 1,
5152
19 => 3,
52-
22 => 1,
53+
21 => 1,
5354
24 => 1,
54-
26 => 3,
55+
26 => 2,
5556
28 => 3,
5657
34 => 2,
58+
38 => 1,
5759
43 => 1,
5860
46 => 1,
5961
47 => 1,
6062
49 => 1,
63+
54 => 6,
64+
];
65+
case 'DeclareStatementUnitTest.4.inc':
66+
return [
67+
3 => 1,
68+
4 => 1,
69+
5 => 1,
70+
6 => 1,
71+
7 => 1,
72+
8 => 1,
73+
9 => 1,
74+
10 => 1,
75+
11 => 1,
76+
12 => 1,
77+
13 => 1,
78+
14 => 1,
79+
15 => 1,
80+
16 => 1,
81+
17 => 1,
82+
18 => 1,
83+
19 => 1,
84+
20 => 1,
85+
21 => 1,
86+
23 => 1,
87+
25 => 1,
88+
27 => 1,
89+
29 => 1,
90+
31 => 1,
91+
33 => 1,
92+
35 => 1,
93+
37 => 1,
94+
39 => 1,
95+
41 => 1,
96+
43 => 1,
97+
45 => 1,
98+
46 => 1,
99+
47 => 1,
100+
48 => 1,
101+
49 => 1,
102+
50 => 1,
103+
52 => 1,
104+
54 => 1,
105+
56 => 1,
106+
58 => 1,
107+
60 => 1,
108+
62 => 1,
109+
];
110+
case 'DeclareStatementUnitTest.5.inc':
111+
return [
112+
3 => 1,
113+
6 => 1,
114+
10 => 2,
115+
18 => 1,
116+
19 => 1,
117+
20 => 1,
118+
23 => 1,
61119
];
62120
default:
63121
return [];

0 commit comments

Comments
 (0)