Skip to content

Commit f9998fe

Browse files
committed
Generic/RequireStrictTypes: bug fix - limit token search to the statement
While hopefully unlikely in real life, the sniff should handle parse errors correctly. Along the same lines, live coding should be handled correctly and a `declare()` statement should only be examined once the statement has been completed. This updates the sniff to handle both situations correctly. Without these fixes, no error would be thrown in test case file 5 or 6 (false negative), while an error would be thrown in test case file 7 (false positive). Includes unit tests. Includes minor efficiency tweak to only start looking for a "next" token _after_ the current token.
1 parent fd3fdb5 commit f9998fe

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ The file documents changes to the PHP_CodeSniffer project.
109109
- Thanks to Volker Dusch (@edorian) for the patch
110110
- Fixed bug #3717 : Squiz.Commenting.FunctionComment: fixed false positive for InvalidNoReturn when type is never
111111
- Thanks to Choraimy Kroonstuiver (@axlon) for the patch
112+
- Fixed bug #3720 : Generic/RequireStrictTypes : will now bow out silently in case of parse errors/live coding instead of throwing false positives/false negatives
113+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
112114
- Fixed bug #3722 : Potential "Uninitialized string offset 1" in octal notation backfill
113115
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
114116
- Fixed bug #3728 : PHP 8.2 | PSR1/SideEffects: allow for readonly classes

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PHP_CodeSniffer\Files\File;
1313
use PHP_CodeSniffer\Sniffs\Sniff;
14+
use PHP_CodeSniffer\Util\Tokens;
1415

1516
class RequireStrictTypesSniff implements Sniff
1617
{
@@ -40,19 +41,31 @@ public function register()
4041
public function process(File $phpcsFile, $stackPtr)
4142
{
4243
$tokens = $phpcsFile->getTokens();
43-
$declare = $phpcsFile->findNext(T_DECLARE, $stackPtr);
44-
$found = false;
44+
$declare = $phpcsFile->findNext(T_DECLARE, ($stackPtr + 1));
45+
46+
$found = false;
4547

4648
if ($declare !== false) {
47-
$nextString = $phpcsFile->findNext(T_STRING, $declare);
49+
if (isset($tokens[$declare]['parenthesis_opener'], $tokens[$declare]['parenthesis_closer']) === false) {
50+
// Live coding, ignore for now.
51+
return $phpcsFile->numTokens;
52+
}
4853

49-
if ($nextString !== false) {
50-
if (strtolower($tokens[$nextString]['content']) === 'strict_types') {
51-
// There is a strict types declaration.
52-
$found = true;
53-
}
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;
5467
}
55-
}
68+
}//end if
5669

5770
if ($found === false) {
5871
$error = 'Missing required strict_types declaration';
Lines changed: 6 additions & 0 deletions
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;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// This is a parse error, but the sniff should still handle this correctly.
4+
declare($something = STRICT_TYPES);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Live coding. This should be ignored.
4+
declare(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function getErrorList($testFile='')
2929
{
3030
switch ($testFile) {
3131
case 'RequireStrictTypesUnitTest.2.inc':
32+
case 'RequireStrictTypesUnitTest.5.inc':
33+
case 'RequireStrictTypesUnitTest.6.inc':
3234
return [1 => 1];
3335

3436
default:

0 commit comments

Comments
 (0)