@@ -2,101 +2,101 @@ |
||
2 | 2 | |
3 | 3 | function nestingOne() |
4 | 4 | { |
5 | - if ($condition) { |
|
6 | - echo 'hi'; |
|
7 | - } |
|
5 | + if ($condition) { |
|
6 | + echo 'hi'; |
|
7 | + } |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | function nestingFive() |
11 | 11 | { |
12 | - if ($condition) { |
|
13 | - echo 'hi'; |
|
14 | - switch ($condition) |
|
15 | - { |
|
16 | - case '1': |
|
17 | - if ($condition === '1') { |
|
18 | - if ($cond) { |
|
19 | - echo 'hi'; |
|
20 | - } |
|
21 | - } |
|
22 | - break; |
|
23 | - } |
|
24 | - } |
|
12 | + if ($condition) { |
|
13 | + echo 'hi'; |
|
14 | + switch ($condition) |
|
15 | + { |
|
16 | + case '1': |
|
17 | + if ($condition === '1') { |
|
18 | + if ($cond) { |
|
19 | + echo 'hi'; |
|
20 | + } |
|
21 | + } |
|
22 | + break; |
|
23 | + } |
|
24 | + } |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | function nestingSix() |
28 | 28 | { |
29 | - if ($condition) { |
|
30 | - echo 'hi'; |
|
31 | - switch ($condition) |
|
32 | - { |
|
33 | - case '1': |
|
34 | - if ($condition === '1') { |
|
35 | - if ($cond) { |
|
36 | - foreach ($conds as $cond) { |
|
37 | - echo 'hi'; |
|
38 | - } |
|
39 | - } |
|
40 | - } |
|
41 | - break; |
|
42 | - } |
|
43 | - } |
|
29 | + if ($condition) { |
|
30 | + echo 'hi'; |
|
31 | + switch ($condition) |
|
32 | + { |
|
33 | + case '1': |
|
34 | + if ($condition === '1') { |
|
35 | + if ($cond) { |
|
36 | + foreach ($conds as $cond) { |
|
37 | + echo 'hi'; |
|
38 | + } |
|
39 | + } |
|
40 | + } |
|
41 | + break; |
|
42 | + } |
|
43 | + } |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | function nestingTen() |
47 | 47 | { |
48 | - if ($condition) { |
|
49 | - echo 'hi'; |
|
50 | - switch ($condition) |
|
51 | - { |
|
52 | - case '1': |
|
53 | - if ($condition === '1') { |
|
54 | - if ($cond) { |
|
55 | - switch ($cond) { |
|
56 | - case '1': |
|
57 | - if ($cond === '1') { |
|
58 | - foreach ($conds as $cond) { |
|
59 | - if ($cond === 'hi') { |
|
60 | - echo 'hi'; |
|
61 | - } |
|
62 | - } |
|
63 | - } |
|
64 | - break; |
|
65 | - } |
|
66 | - } |
|
67 | - } |
|
68 | - break; |
|
69 | - } |
|
70 | - } |
|
48 | + if ($condition) { |
|
49 | + echo 'hi'; |
|
50 | + switch ($condition) |
|
51 | + { |
|
52 | + case '1': |
|
53 | + if ($condition === '1') { |
|
54 | + if ($cond) { |
|
55 | + switch ($cond) { |
|
56 | + case '1': |
|
57 | + if ($cond === '1') { |
|
58 | + foreach ($conds as $cond) { |
|
59 | + if ($cond === 'hi') { |
|
60 | + echo 'hi'; |
|
61 | + } |
|
62 | + } |
|
63 | + } |
|
64 | + break; |
|
65 | + } |
|
66 | + } |
|
67 | + } |
|
68 | + break; |
|
69 | + } |
|
70 | + } |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | function nestingEleven() |
74 | 74 | { |
75 | - if ($condition) { |
|
76 | - echo 'hi'; |
|
77 | - switch ($condition) |
|
78 | - { |
|
79 | - case '1': |
|
80 | - if ($condition === '1') { |
|
81 | - if ($cond) { |
|
82 | - switch ($cond) { |
|
83 | - case '1': |
|
84 | - if ($cond === '1') { |
|
85 | - foreach ($conds as $cond) { |
|
86 | - if ($cond === 'hi') { |
|
87 | - if ($cond !== 'bye') { |
|
88 | - echo 'hi'; |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - break; |
|
94 | - } |
|
95 | - } |
|
96 | - } |
|
97 | - break; |
|
98 | - } |
|
99 | - } |
|
75 | + if ($condition) { |
|
76 | + echo 'hi'; |
|
77 | + switch ($condition) |
|
78 | + { |
|
79 | + case '1': |
|
80 | + if ($condition === '1') { |
|
81 | + if ($cond) { |
|
82 | + switch ($cond) { |
|
83 | + case '1': |
|
84 | + if ($cond === '1') { |
|
85 | + foreach ($conds as $cond) { |
|
86 | + if ($cond === 'hi') { |
|
87 | + if ($cond !== 'bye') { |
|
88 | + echo 'hi'; |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + break; |
|
94 | + } |
|
95 | + } |
|
96 | + } |
|
97 | + break; |
|
98 | + } |
|
99 | + } |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | ?> |
@@ -1,18 +1,18 @@ discard block |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | |
3 | -function nestingOne() |
|
4 | -{ |
|
5 | - if ($condition) { |
|
6 | - echo 'hi'; |
|
7 | - } |
|
3 | + function nestingOne() |
|
4 | + { |
|
5 | + if ($condition) { |
|
6 | + echo 'hi'; |
|
7 | + } |
|
8 | 8 | } |
9 | 9 | |
10 | -function nestingFive() |
|
11 | -{ |
|
12 | - if ($condition) { |
|
13 | - echo 'hi'; |
|
14 | - switch ($condition) |
|
15 | - { |
|
10 | + function nestingFive() |
|
11 | + { |
|
12 | + if ($condition) { |
|
13 | + echo 'hi'; |
|
14 | + switch ($condition) |
|
15 | + { |
|
16 | 16 | case '1': |
17 | 17 | if ($condition === '1') { |
18 | 18 | if ($cond) { |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | } |
21 | 21 | } |
22 | 22 | break; |
23 | - } |
|
23 | + } |
|
24 | 24 | } |
25 | 25 | } |
26 | 26 |
@@ -15,37 +15,37 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Returns the lines where errors should occur. |
|
20 | - * |
|
21 | - * The key of the array should represent the line number and the value |
|
22 | - * should represent the number of errors that should occur on that line. |
|
23 | - * |
|
24 | - * @return array<int, int> |
|
25 | - */ |
|
26 | - public function getErrorList() |
|
27 | - { |
|
28 | - return [73 => 1]; |
|
29 | - |
|
30 | - }//end getErrorList() |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * Returns the lines where warnings should occur. |
|
35 | - * |
|
36 | - * The key of the array should represent the line number and the value |
|
37 | - * should represent the number of warnings that should occur on that line. |
|
38 | - * |
|
39 | - * @return array<int, int> |
|
40 | - */ |
|
41 | - public function getWarningList() |
|
42 | - { |
|
43 | - return [ |
|
44 | - 27 => 1, |
|
45 | - 46 => 1, |
|
46 | - ]; |
|
47 | - |
|
48 | - }//end getWarningList() |
|
18 | + /** |
|
19 | + * Returns the lines where errors should occur. |
|
20 | + * |
|
21 | + * The key of the array should represent the line number and the value |
|
22 | + * should represent the number of errors that should occur on that line. |
|
23 | + * |
|
24 | + * @return array<int, int> |
|
25 | + */ |
|
26 | + public function getErrorList() |
|
27 | + { |
|
28 | + return [73 => 1]; |
|
29 | + |
|
30 | + }//end getErrorList() |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * Returns the lines where warnings should occur. |
|
35 | + * |
|
36 | + * The key of the array should represent the line number and the value |
|
37 | + * should represent the number of warnings that should occur on that line. |
|
38 | + * |
|
39 | + * @return array<int, int> |
|
40 | + */ |
|
41 | + public function getWarningList() |
|
42 | + { |
|
43 | + return [ |
|
44 | + 27 => 1, |
|
45 | + 46 => 1, |
|
46 | + ]; |
|
47 | + |
|
48 | + }//end getWarningList() |
|
49 | 49 | |
50 | 50 | |
51 | 51 | }//end class |
@@ -1,4 +1,4 @@ discard block |
||
1 | -<?php |
|
1 | + <?php |
|
2 | 2 | /** |
3 | 3 | * Unit test class for the NestingLevel sniff. |
4 | 4 | * |
@@ -7,15 +7,15 @@ discard block |
||
7 | 7 | * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence |
8 | 8 | */ |
9 | 9 | |
10 | -namespace PHP_CodeSniffer\Standards\Generic\Tests\Metrics; |
|
10 | + namespace PHP_CodeSniffer\Standards\Generic\Tests\Metrics; |
|
11 | 11 | |
12 | -use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; |
|
12 | + use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; |
|
13 | 13 | |
14 | -class NestingLevelUnitTest extends AbstractSniffUnitTest |
|
15 | -{ |
|
14 | + class NestingLevelUnitTest extends AbstractSniffUnitTest |
|
15 | + { |
|
16 | 16 | |
17 | 17 | |
18 | - /** |
|
18 | + /** |
|
19 | 19 | * Returns the lines where errors should occur. |
20 | 20 | * |
21 | 21 | * The key of the array should represent the line number and the value |
@@ -23,14 +23,14 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @return array<int, int> |
25 | 25 | */ |
26 | - public function getErrorList() |
|
27 | - { |
|
28 | - return [73 => 1]; |
|
26 | + public function getErrorList() |
|
27 | + { |
|
28 | + return [73 => 1]; |
|
29 | 29 | |
30 | - }//end getErrorList() |
|
30 | + }//end getErrorList() |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
33 | + /** |
|
34 | 34 | * Returns the lines where warnings should occur. |
35 | 35 | * |
36 | 36 | * The key of the array should represent the line number and the value |
@@ -38,14 +38,14 @@ discard block |
||
38 | 38 | * |
39 | 39 | * @return array<int, int> |
40 | 40 | */ |
41 | - public function getWarningList() |
|
42 | - { |
|
43 | - return [ |
|
44 | - 27 => 1, |
|
45 | - 46 => 1, |
|
46 | - ]; |
|
47 | - |
|
48 | - }//end getWarningList() |
|
41 | + public function getWarningList() |
|
42 | + { |
|
43 | + return [ |
|
44 | + 27 => 1, |
|
45 | + 46 => 1, |
|
46 | + ]; |
|
47 | + |
|
48 | + }//end getWarningList() |
|
49 | 49 | |
50 | 50 | |
51 | 51 | }//end class |
@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace A\B { |
3 | - class MyClass {} |
|
4 | - interface MyInterface {} |
|
3 | + class MyClass {} |
|
4 | + interface MyInterface {} |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | namespace D { |
8 | - class MyClass {} |
|
8 | + class MyClass {} |
|
9 | 9 | } |
10 | 10 | ?> |
11 | 11 | \ No newline at end of file |
@@ -52,30 +52,30 @@ discard block |
||
52 | 52 | |
53 | 53 | // This is OK. |
54 | 54 | class A_Class_With_Really_Long_Name |
55 | - extends Another_Class_With_A_Really_Long_Name { |
|
55 | + extends Another_Class_With_A_Really_Long_Name { |
|
56 | 56 | |
57 | 57 | } |
58 | 58 | |
59 | 59 | // This is OK too. |
60 | 60 | class A_Class_With_Really_Long_Name_2 |
61 | - extends Another_Class_With_A_Really_Long_Name |
|
62 | - implements Some_Interface_With_A_Long_Name, |
|
63 | - Another_Interface_With_A_Long_Name { |
|
61 | + extends Another_Class_With_A_Really_Long_Name |
|
62 | + implements Some_Interface_With_A_Long_Name, |
|
63 | + Another_Interface_With_A_Long_Name { |
|
64 | 64 | |
65 | 65 | } |
66 | 66 | |
67 | 67 | // But this is not. |
68 | 68 | class A_Class_With_Really_Long_Name_3 |
69 | - extends Another_Class_With_A_Really_Long_Name |
|
69 | + extends Another_Class_With_A_Really_Long_Name |
|
70 | 70 | { |
71 | 71 | |
72 | 72 | } |
73 | 73 | |
74 | 74 | // Nor is this. |
75 | 75 | class A_Class_With_Really_Long_Name_4 |
76 | - extends Another_Class_With_A_Really_Long_Name |
|
77 | - implements Some_Interface_With_A_Long_Name, |
|
78 | - Another_Interface_With_A_Long_Name |
|
76 | + extends Another_Class_With_A_Really_Long_Name |
|
77 | + implements Some_Interface_With_A_Long_Name, |
|
78 | + Another_Interface_With_A_Long_Name |
|
79 | 79 | { |
80 | 80 | |
81 | 81 | } |
@@ -86,6 +86,6 @@ discard block |
||
86 | 86 | |
87 | 87 | // And this is not. |
88 | 88 | class Test_Class_Bad_G |
89 | - /*some comment*/ |
|
89 | + /*some comment*/ |
|
90 | 90 | { |
91 | 91 | } |
@@ -35,16 +35,16 @@ |
||
35 | 35 | } |
36 | 36 | |
37 | 37 | // These should all be flagged for wrong whitespace before opening brace. |
38 | -interface Test_Interface_Bad_C { |
|
38 | +interface Test_Interface_Bad_C { |
|
39 | 39 | } |
40 | 40 | |
41 | -class Test_Class_Bad_G { |
|
41 | +class Test_Class_Bad_G { |
|
42 | 42 | } |
43 | 43 | |
44 | -class Test_Class_Bad_H extends Test_Class_Bad_G { |
|
44 | +class Test_Class_Bad_H extends Test_Class_Bad_G { |
|
45 | 45 | } |
46 | 46 | |
47 | -class Test_Class_Bad_I implements Test_Interface_Bad_C{ |
|
47 | +class Test_Class_Bad_I implements Test_Interface_Bad_C { |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | // This one should be flagged as a potential parse error. |
@@ -40,7 +40,7 @@ |
||
40 | 40 | * |
41 | 41 | * @return array<int, int> |
42 | 42 | */ |
43 | - public function getWarningList($testFile='') |
|
43 | + public function getWarningList($testFile = '') |
|
44 | 44 | { |
45 | 45 | switch ($testFile) { |
46 | 46 | case 'DuplicateClassNameUnitTest.1.inc': |
@@ -15,65 +15,65 @@ |
||
15 | 15 | { |
16 | 16 | |
17 | 17 | |
18 | - /** |
|
19 | - * Returns the lines where errors should occur. |
|
20 | - * |
|
21 | - * The key of the array should represent the line number and the value |
|
22 | - * should represent the number of errors that should occur on that line. |
|
23 | - * |
|
24 | - * @return array<int, int> |
|
25 | - */ |
|
26 | - public function getErrorList() |
|
27 | - { |
|
28 | - return []; |
|
18 | + /** |
|
19 | + * Returns the lines where errors should occur. |
|
20 | + * |
|
21 | + * The key of the array should represent the line number and the value |
|
22 | + * should represent the number of errors that should occur on that line. |
|
23 | + * |
|
24 | + * @return array<int, int> |
|
25 | + */ |
|
26 | + public function getErrorList() |
|
27 | + { |
|
28 | + return []; |
|
29 | 29 | |
30 | - }//end getErrorList() |
|
30 | + }//end getErrorList() |
|
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Returns the lines where warnings should occur. |
|
35 | - * |
|
36 | - * The key of the array should represent the line number and the value |
|
37 | - * should represent the number of warnings that should occur on that line. |
|
38 | - * |
|
39 | - * @param string $testFile The name of the file being tested. |
|
40 | - * |
|
41 | - * @return array<int, int> |
|
42 | - */ |
|
43 | - public function getWarningList($testFile='') |
|
44 | - { |
|
45 | - switch ($testFile) { |
|
46 | - case 'DuplicateClassNameUnitTest.1.inc': |
|
47 | - return [ |
|
48 | - 10 => 1, |
|
49 | - 11 => 1, |
|
50 | - 12 => 1, |
|
51 | - 13 => 1, |
|
52 | - ]; |
|
53 | - break; |
|
54 | - case 'DuplicateClassNameUnitTest.2.inc': |
|
55 | - return [ |
|
56 | - 2 => 1, |
|
57 | - 3 => 1, |
|
58 | - 4 => 1, |
|
59 | - 5 => 1, |
|
60 | - ]; |
|
61 | - break; |
|
62 | - case 'DuplicateClassNameUnitTest.5.inc': |
|
63 | - return [ |
|
64 | - 3 => 1, |
|
65 | - 7 => 1, |
|
66 | - ]; |
|
67 | - break; |
|
68 | - case 'DuplicateClassNameUnitTest.6.inc': |
|
69 | - return [10 => 1]; |
|
70 | - break; |
|
71 | - default: |
|
72 | - return []; |
|
73 | - break; |
|
74 | - }//end switch |
|
33 | + /** |
|
34 | + * Returns the lines where warnings should occur. |
|
35 | + * |
|
36 | + * The key of the array should represent the line number and the value |
|
37 | + * should represent the number of warnings that should occur on that line. |
|
38 | + * |
|
39 | + * @param string $testFile The name of the file being tested. |
|
40 | + * |
|
41 | + * @return array<int, int> |
|
42 | + */ |
|
43 | + public function getWarningList($testFile='') |
|
44 | + { |
|
45 | + switch ($testFile) { |
|
46 | + case 'DuplicateClassNameUnitTest.1.inc': |
|
47 | + return [ |
|
48 | + 10 => 1, |
|
49 | + 11 => 1, |
|
50 | + 12 => 1, |
|
51 | + 13 => 1, |
|
52 | + ]; |
|
53 | + break; |
|
54 | + case 'DuplicateClassNameUnitTest.2.inc': |
|
55 | + return [ |
|
56 | + 2 => 1, |
|
57 | + 3 => 1, |
|
58 | + 4 => 1, |
|
59 | + 5 => 1, |
|
60 | + ]; |
|
61 | + break; |
|
62 | + case 'DuplicateClassNameUnitTest.5.inc': |
|
63 | + return [ |
|
64 | + 3 => 1, |
|
65 | + 7 => 1, |
|
66 | + ]; |
|
67 | + break; |
|
68 | + case 'DuplicateClassNameUnitTest.6.inc': |
|
69 | + return [10 => 1]; |
|
70 | + break; |
|
71 | + default: |
|
72 | + return []; |
|
73 | + break; |
|
74 | + }//end switch |
|
75 | 75 | |
76 | - }//end getWarningList() |
|
76 | + }//end getWarningList() |
|
77 | 77 | |
78 | 78 | |
79 | 79 | }//end class |
@@ -43,34 +43,34 @@ |
||
43 | 43 | public function getWarningList($testFile='') |
44 | 44 | { |
45 | 45 | switch ($testFile) { |
46 | - case 'DuplicateClassNameUnitTest.1.inc': |
|
47 | - return [ |
|
48 | - 10 => 1, |
|
49 | - 11 => 1, |
|
50 | - 12 => 1, |
|
51 | - 13 => 1, |
|
52 | - ]; |
|
53 | - break; |
|
54 | - case 'DuplicateClassNameUnitTest.2.inc': |
|
55 | - return [ |
|
56 | - 2 => 1, |
|
57 | - 3 => 1, |
|
58 | - 4 => 1, |
|
59 | - 5 => 1, |
|
60 | - ]; |
|
61 | - break; |
|
62 | - case 'DuplicateClassNameUnitTest.5.inc': |
|
63 | - return [ |
|
64 | - 3 => 1, |
|
65 | - 7 => 1, |
|
66 | - ]; |
|
67 | - break; |
|
68 | - case 'DuplicateClassNameUnitTest.6.inc': |
|
69 | - return [10 => 1]; |
|
70 | - break; |
|
71 | - default: |
|
72 | - return []; |
|
73 | - break; |
|
46 | + case 'DuplicateClassNameUnitTest.1.inc': |
|
47 | + return [ |
|
48 | + 10 => 1, |
|
49 | + 11 => 1, |
|
50 | + 12 => 1, |
|
51 | + 13 => 1, |
|
52 | + ]; |
|
53 | + break; |
|
54 | + case 'DuplicateClassNameUnitTest.2.inc': |
|
55 | + return [ |
|
56 | + 2 => 1, |
|
57 | + 3 => 1, |
|
58 | + 4 => 1, |
|
59 | + 5 => 1, |
|
60 | + ]; |
|
61 | + break; |
|
62 | + case 'DuplicateClassNameUnitTest.5.inc': |
|
63 | + return [ |
|
64 | + 3 => 1, |
|
65 | + 7 => 1, |
|
66 | + ]; |
|
67 | + break; |
|
68 | + case 'DuplicateClassNameUnitTest.6.inc': |
|
69 | + return [10 => 1]; |
|
70 | + break; |
|
71 | + default: |
|
72 | + return []; |
|
73 | + break; |
|
74 | 74 | }//end switch |
75 | 75 | |
76 | 76 | }//end getWarningList() |
@@ -1,7 +1,7 @@ |
||
1 | 1 | <?php |
2 | 2 | class Test{ |
3 | - public function testOne(){ |
|
4 | - ?> |
|
3 | + public function testOne(){ |
|
4 | + ?> |
|
5 | 5 | <p>some html here</p> |
6 | 6 | <?php |
7 | 7 | } |
@@ -1,12 +1,12 @@ |
||
1 | 1 | <?php |
2 | -class Test{ |
|
3 | - public function testOne(){ |
|
2 | +class Test { |
|
3 | + public function testOne() { |
|
4 | 4 | ?> |
5 | 5 | <p>some html here</p> |
6 | 6 | <?php |
7 | 7 | } |
8 | 8 | } |
9 | 9 | |
10 | -class Test{ |
|
10 | +class Test { |
|
11 | 11 | |
12 | 12 | } |
@@ -34,68 +34,68 @@ |
||
34 | 34 | { |
35 | 35 | |
36 | 36 | |
37 | - /** |
|
38 | - * Registers the tokens that this sniff wants to listen for. |
|
39 | - * |
|
40 | - * @return int[] |
|
41 | - */ |
|
42 | - public function register() |
|
43 | - { |
|
44 | - return [T_FOR]; |
|
45 | - |
|
46 | - }//end register() |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Processes this test, when one of its tokens is encountered. |
|
51 | - * |
|
52 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
53 | - * @param int $stackPtr The position of the current token |
|
54 | - * in the stack passed in $tokens. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function process(File $phpcsFile, $stackPtr) |
|
59 | - { |
|
60 | - $tokens = $phpcsFile->getTokens(); |
|
61 | - $token = $tokens[$stackPtr]; |
|
62 | - |
|
63 | - // Skip invalid statement. |
|
64 | - if (isset($token['parenthesis_opener']) === false) { |
|
65 | - return; |
|
66 | - } |
|
67 | - |
|
68 | - $next = ++$token['parenthesis_opener']; |
|
69 | - $end = --$token['parenthesis_closer']; |
|
70 | - |
|
71 | - $position = 0; |
|
72 | - |
|
73 | - for (; $next <= $end; ++$next) { |
|
74 | - $code = $tokens[$next]['code']; |
|
75 | - if ($code === T_SEMICOLON) { |
|
76 | - ++$position; |
|
77 | - } |
|
78 | - |
|
79 | - if ($position < 1) { |
|
80 | - continue; |
|
81 | - } else if ($position > 1) { |
|
82 | - break; |
|
83 | - } else if ($code !== T_VARIABLE && $code !== T_STRING) { |
|
84 | - continue; |
|
85 | - } |
|
86 | - |
|
87 | - // Find next non empty token, if it is a open curly brace we have a |
|
88 | - // function call. |
|
89 | - $index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
90 | - |
|
91 | - if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) { |
|
92 | - $error = 'Avoid function calls in a FOR loop test part'; |
|
93 | - $phpcsFile->addWarning($error, $stackPtr, 'NotAllowed'); |
|
94 | - break; |
|
95 | - } |
|
96 | - }//end for |
|
97 | - |
|
98 | - }//end process() |
|
37 | + /** |
|
38 | + * Registers the tokens that this sniff wants to listen for. |
|
39 | + * |
|
40 | + * @return int[] |
|
41 | + */ |
|
42 | + public function register() |
|
43 | + { |
|
44 | + return [T_FOR]; |
|
45 | + |
|
46 | + }//end register() |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Processes this test, when one of its tokens is encountered. |
|
51 | + * |
|
52 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
53 | + * @param int $stackPtr The position of the current token |
|
54 | + * in the stack passed in $tokens. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function process(File $phpcsFile, $stackPtr) |
|
59 | + { |
|
60 | + $tokens = $phpcsFile->getTokens(); |
|
61 | + $token = $tokens[$stackPtr]; |
|
62 | + |
|
63 | + // Skip invalid statement. |
|
64 | + if (isset($token['parenthesis_opener']) === false) { |
|
65 | + return; |
|
66 | + } |
|
67 | + |
|
68 | + $next = ++$token['parenthesis_opener']; |
|
69 | + $end = --$token['parenthesis_closer']; |
|
70 | + |
|
71 | + $position = 0; |
|
72 | + |
|
73 | + for (; $next <= $end; ++$next) { |
|
74 | + $code = $tokens[$next]['code']; |
|
75 | + if ($code === T_SEMICOLON) { |
|
76 | + ++$position; |
|
77 | + } |
|
78 | + |
|
79 | + if ($position < 1) { |
|
80 | + continue; |
|
81 | + } else if ($position > 1) { |
|
82 | + break; |
|
83 | + } else if ($code !== T_VARIABLE && $code !== T_STRING) { |
|
84 | + continue; |
|
85 | + } |
|
86 | + |
|
87 | + // Find next non empty token, if it is a open curly brace we have a |
|
88 | + // function call. |
|
89 | + $index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); |
|
90 | + |
|
91 | + if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) { |
|
92 | + $error = 'Avoid function calls in a FOR loop test part'; |
|
93 | + $phpcsFile->addWarning($error, $stackPtr, 'NotAllowed'); |
|
94 | + break; |
|
95 | + } |
|
96 | + }//end for |
|
97 | + |
|
98 | + }//end process() |
|
99 | 99 | |
100 | 100 | |
101 | 101 | }//end class |
@@ -30,56 +30,56 @@ |
||
30 | 30 | { |
31 | 31 | |
32 | 32 | |
33 | - /** |
|
34 | - * Registers the tokens that this sniff wants to listen for. |
|
35 | - * |
|
36 | - * @return int[] |
|
37 | - */ |
|
38 | - public function register() |
|
39 | - { |
|
40 | - return [T_CLASS]; |
|
41 | - |
|
42 | - }//end register() |
|
43 | - |
|
44 | - |
|
45 | - /** |
|
46 | - * Processes this test, when one of its tokens is encountered. |
|
47 | - * |
|
48 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | - * @param int $stackPtr The position of the current token |
|
50 | - * in the stack passed in $tokens. |
|
51 | - * |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function process(File $phpcsFile, $stackPtr) |
|
55 | - { |
|
56 | - $tokens = $phpcsFile->getTokens(); |
|
57 | - $token = $tokens[$stackPtr]; |
|
58 | - |
|
59 | - // Skip for-statements without body. |
|
60 | - if (isset($token['scope_opener']) === false) { |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - // Fetch previous token. |
|
65 | - $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
66 | - |
|
67 | - // Skip for non final class. |
|
68 | - if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) { |
|
69 | - return; |
|
70 | - } |
|
71 | - |
|
72 | - $next = ++$token['scope_opener']; |
|
73 | - $end = --$token['scope_closer']; |
|
74 | - |
|
75 | - for (; $next <= $end; ++$next) { |
|
76 | - if ($tokens[$next]['code'] === T_FINAL) { |
|
77 | - $error = 'Unnecessary FINAL modifier in FINAL class'; |
|
78 | - $phpcsFile->addWarning($error, $next, 'Found'); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - }//end process() |
|
33 | + /** |
|
34 | + * Registers the tokens that this sniff wants to listen for. |
|
35 | + * |
|
36 | + * @return int[] |
|
37 | + */ |
|
38 | + public function register() |
|
39 | + { |
|
40 | + return [T_CLASS]; |
|
41 | + |
|
42 | + }//end register() |
|
43 | + |
|
44 | + |
|
45 | + /** |
|
46 | + * Processes this test, when one of its tokens is encountered. |
|
47 | + * |
|
48 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
49 | + * @param int $stackPtr The position of the current token |
|
50 | + * in the stack passed in $tokens. |
|
51 | + * |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function process(File $phpcsFile, $stackPtr) |
|
55 | + { |
|
56 | + $tokens = $phpcsFile->getTokens(); |
|
57 | + $token = $tokens[$stackPtr]; |
|
58 | + |
|
59 | + // Skip for-statements without body. |
|
60 | + if (isset($token['scope_opener']) === false) { |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + // Fetch previous token. |
|
65 | + $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
|
66 | + |
|
67 | + // Skip for non final class. |
|
68 | + if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) { |
|
69 | + return; |
|
70 | + } |
|
71 | + |
|
72 | + $next = ++$token['scope_opener']; |
|
73 | + $end = --$token['scope_closer']; |
|
74 | + |
|
75 | + for (; $next <= $end; ++$next) { |
|
76 | + if ($tokens[$next]['code'] === T_FINAL) { |
|
77 | + $error = 'Unnecessary FINAL modifier in FINAL class'; |
|
78 | + $phpcsFile->addWarning($error, $next, 'Found'); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + }//end process() |
|
83 | 83 | |
84 | 84 | |
85 | 85 | }//end class |
@@ -36,99 +36,99 @@ |
||
36 | 36 | { |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * Registers the tokens that this sniff wants to listen for. |
|
41 | - * |
|
42 | - * @return int[] |
|
43 | - */ |
|
44 | - public function register() |
|
45 | - { |
|
46 | - return [T_FOR]; |
|
47 | - |
|
48 | - }//end register() |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * Processes this test, when one of its tokens is encountered. |
|
53 | - * |
|
54 | - * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
55 | - * @param int $stackPtr The position of the current token |
|
56 | - * in the stack passed in $tokens. |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function process(File $phpcsFile, $stackPtr) |
|
61 | - { |
|
62 | - $tokens = $phpcsFile->getTokens(); |
|
63 | - $token = $tokens[$stackPtr]; |
|
64 | - |
|
65 | - // Skip for-loop without body. |
|
66 | - if (isset($token['scope_opener']) === false) { |
|
67 | - return; |
|
68 | - } |
|
69 | - |
|
70 | - // Find incrementors for outer loop. |
|
71 | - $outer = $this->findIncrementers($tokens, $token); |
|
72 | - |
|
73 | - // Skip if empty. |
|
74 | - if (count($outer) === 0) { |
|
75 | - return; |
|
76 | - } |
|
77 | - |
|
78 | - // Find nested for loops. |
|
79 | - $start = ++$token['scope_opener']; |
|
80 | - $end = --$token['scope_closer']; |
|
81 | - |
|
82 | - for (; $start <= $end; ++$start) { |
|
83 | - if ($tokens[$start]['code'] !== T_FOR) { |
|
84 | - continue; |
|
85 | - } |
|
86 | - |
|
87 | - $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
88 | - $diff = array_intersect($outer, $inner); |
|
89 | - |
|
90 | - if (count($diff) !== 0) { |
|
91 | - $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
92 | - $data = [join(', ', $diff)]; |
|
93 | - $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - }//end process() |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * Get all used variables in the incrementer part of a for statement. |
|
102 | - * |
|
103 | - * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
104 | - * @param array(string=>mixed) $token Current for loop token |
|
105 | - * |
|
106 | - * @return string[] List of all found incrementer variables. |
|
107 | - */ |
|
108 | - protected function findIncrementers(array $tokens, array $token) |
|
109 | - { |
|
110 | - // Skip invalid statement. |
|
111 | - if (isset($token['parenthesis_opener']) === false) { |
|
112 | - return []; |
|
113 | - } |
|
114 | - |
|
115 | - $start = ++$token['parenthesis_opener']; |
|
116 | - $end = --$token['parenthesis_closer']; |
|
117 | - |
|
118 | - $incrementers = []; |
|
119 | - $semicolons = 0; |
|
120 | - for ($next = $start; $next <= $end; ++$next) { |
|
121 | - $code = $tokens[$next]['code']; |
|
122 | - if ($code === T_SEMICOLON) { |
|
123 | - ++$semicolons; |
|
124 | - } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
125 | - $incrementers[] = $tokens[$next]['content']; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return $incrementers; |
|
130 | - |
|
131 | - }//end findIncrementers() |
|
39 | + /** |
|
40 | + * Registers the tokens that this sniff wants to listen for. |
|
41 | + * |
|
42 | + * @return int[] |
|
43 | + */ |
|
44 | + public function register() |
|
45 | + { |
|
46 | + return [T_FOR]; |
|
47 | + |
|
48 | + }//end register() |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * Processes this test, when one of its tokens is encountered. |
|
53 | + * |
|
54 | + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. |
|
55 | + * @param int $stackPtr The position of the current token |
|
56 | + * in the stack passed in $tokens. |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function process(File $phpcsFile, $stackPtr) |
|
61 | + { |
|
62 | + $tokens = $phpcsFile->getTokens(); |
|
63 | + $token = $tokens[$stackPtr]; |
|
64 | + |
|
65 | + // Skip for-loop without body. |
|
66 | + if (isset($token['scope_opener']) === false) { |
|
67 | + return; |
|
68 | + } |
|
69 | + |
|
70 | + // Find incrementors for outer loop. |
|
71 | + $outer = $this->findIncrementers($tokens, $token); |
|
72 | + |
|
73 | + // Skip if empty. |
|
74 | + if (count($outer) === 0) { |
|
75 | + return; |
|
76 | + } |
|
77 | + |
|
78 | + // Find nested for loops. |
|
79 | + $start = ++$token['scope_opener']; |
|
80 | + $end = --$token['scope_closer']; |
|
81 | + |
|
82 | + for (; $start <= $end; ++$start) { |
|
83 | + if ($tokens[$start]['code'] !== T_FOR) { |
|
84 | + continue; |
|
85 | + } |
|
86 | + |
|
87 | + $inner = $this->findIncrementers($tokens, $tokens[$start]); |
|
88 | + $diff = array_intersect($outer, $inner); |
|
89 | + |
|
90 | + if (count($diff) !== 0) { |
|
91 | + $error = 'Loop incrementor (%s) jumbling with inner loop'; |
|
92 | + $data = [join(', ', $diff)]; |
|
93 | + $phpcsFile->addWarning($error, $stackPtr, 'Found', $data); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + }//end process() |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * Get all used variables in the incrementer part of a for statement. |
|
102 | + * |
|
103 | + * @param array(integer=>array) $tokens Array with all code sniffer tokens. |
|
104 | + * @param array(string=>mixed) $token Current for loop token |
|
105 | + * |
|
106 | + * @return string[] List of all found incrementer variables. |
|
107 | + */ |
|
108 | + protected function findIncrementers(array $tokens, array $token) |
|
109 | + { |
|
110 | + // Skip invalid statement. |
|
111 | + if (isset($token['parenthesis_opener']) === false) { |
|
112 | + return []; |
|
113 | + } |
|
114 | + |
|
115 | + $start = ++$token['parenthesis_opener']; |
|
116 | + $end = --$token['parenthesis_closer']; |
|
117 | + |
|
118 | + $incrementers = []; |
|
119 | + $semicolons = 0; |
|
120 | + for ($next = $start; $next <= $end; ++$next) { |
|
121 | + $code = $tokens[$next]['code']; |
|
122 | + if ($code === T_SEMICOLON) { |
|
123 | + ++$semicolons; |
|
124 | + } else if ($semicolons === 2 && $code === T_VARIABLE) { |
|
125 | + $incrementers[] = $tokens[$next]['content']; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return $incrementers; |
|
130 | + |
|
131 | + }//end findIncrementers() |
|
132 | 132 | |
133 | 133 | |
134 | 134 | }//end class |