Skip to content

Commit 9fa78c2

Browse files
committed
Generic/RequireStrictTypes: bug fix - allow for multi directive statements
PHP allows for multiple directives to be passed in one `declare()` statement. The sniff, however, did not allow for this, which could lead to false positives. Fixed now. Includes unit tests.
1 parent f9998fe commit 9fa78c2

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ The file documents changes to the PHP_CodeSniffer project.
111111
- Thanks to Choraimy Kroonstuiver (@axlon) for the patch
112112
- Fixed bug #3720 : Generic/RequireStrictTypes : will now bow out silently in case of parse errors/live coding instead of throwing false positives/false negatives
113113
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
114+
- Fixed bug #3720 : Generic/RequireStrictTypes : did not handle multi-directive declare statements
115+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
114116
- Fixed bug #3722 : Potential "Uninitialized string offset 1" in octal notation backfill
115117
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
116118
- Fixed bug #3728 : PHP 8.2 | PSR1/SideEffects: allow for readonly classes

src/Standards/Generic/Sniffs/PHP/RequireStrictTypesSniff.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,27 @@ public function process(File $phpcsFile, $stackPtr)
5151
return $phpcsFile->numTokens;
5252
}
5353

54-
$next = $phpcsFile->findNext(
55-
Tokens::$emptyTokens,
56-
($tokens[$declare]['parenthesis_opener'] + 1),
57-
$tokens[$declare]['parenthesis_closer'],
58-
true
59-
);
60-
61-
if ($next !== false
62-
&& $tokens[$next]['code'] === T_STRING
63-
&& strtolower($tokens[$next]['content']) === 'strict_types'
64-
) {
65-
// There is a strict types declaration.
66-
$found = true;
67-
}
54+
$next = $tokens[$declare]['parenthesis_opener'];
55+
56+
do {
57+
$next = $phpcsFile->findNext(
58+
Tokens::$emptyTokens,
59+
($next + 1),
60+
$tokens[$declare]['parenthesis_closer'],
61+
true
62+
);
63+
64+
if ($next !== false
65+
&& $tokens[$next]['code'] === T_STRING
66+
&& strtolower($tokens[$next]['content']) === 'strict_types'
67+
) {
68+
// There is a strict types declaration.
69+
$found = true;
70+
break;
71+
}
72+
73+
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $tokens[$declare]['parenthesis_closer']);
74+
} while ($next !== false && $next < $tokens[$declare]['parenthesis_closer']);
6875
}//end if
6976

7077
if ($found === false) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(encoding='utf-8', ticks=1);
4+
5+
// some code
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(strict_types=1, encoding='utf-8');
4+
5+
// some code
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
declare(encoding='utf-8', strict_types=1);
4+
5+
// some code

src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function getErrorList($testFile='')
3131
case 'RequireStrictTypesUnitTest.2.inc':
3232
case 'RequireStrictTypesUnitTest.5.inc':
3333
case 'RequireStrictTypesUnitTest.6.inc':
34+
case 'RequireStrictTypesUnitTest.10.inc':
3435
return [1 => 1];
3536

3637
default:

0 commit comments

Comments
 (0)