Skip to content

Commit ac6abf6

Browse files
authored
Merge pull request #51 from PHPCSStandards/feature/generic-requirestricttypes-various-bugfixes
Generic/RequireStrictTypes: various bugfixes
2 parents c0e4cfb + 3ff23a3 commit ac6abf6

20 files changed

+159
-14
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ The file documents changes to the PHP_CodeSniffer project.
7777
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
7878
- Generic.Functions.OpeningFunctionBraceKernighanRitchie will now check the spacing before the opening brace for empty functions
7979
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
80+
- Generic.PHP.RequireStrictTypes has a new warning for when there is a declare statement, but the strict_types directive is set to 0
81+
- The warning can be turned off by excluding the Generic.PHP.RequireStrictTypes.Disabled error code
82+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
8083
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier
8184
- PSR2 and PSR12 do not have documented rules for this as they pre-date the readonly modifier
8285
- PSR-PER has been used to confirm the order of this keyword so it can be applied to PSR2 and PSR12 correctly
@@ -109,6 +112,10 @@ The file documents changes to the PHP_CodeSniffer project.
109112
- Thanks to Volker Dusch (@edorian) for the patch
110113
- Fixed bug #3717 : Squiz.Commenting.FunctionComment: fixed false positive for InvalidNoReturn when type is never
111114
- Thanks to Choraimy Kroonstuiver (@axlon) for the patch
115+
- Fixed bug #3720 : Generic/RequireStrictTypes : will now bow out silently in case of parse errors/live coding instead of throwing false positives/false negatives
116+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
117+
- Fixed bug #3720 : Generic/RequireStrictTypes : did not handle multi-directive declare statements
118+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
112119
- Fixed bug #3722 : Potential "Uninitialized string offset 1" in octal notation backfill
113120
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
114121
- Fixed bug #3728 : PHP 8.2 | PSR1/SideEffects: allow for readonly classes

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

Lines changed: 46 additions & 7 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,23 +41,61 @@ 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+
}
53+
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+
);
4863

49-
if ($nextString !== false) {
50-
if (strtolower($tokens[$nextString]['content']) === 'strict_types') {
64+
if ($next !== false
65+
&& $tokens[$next]['code'] === T_STRING
66+
&& strtolower($tokens[$next]['content']) === 'strict_types'
67+
) {
5168
// There is a strict types declaration.
5269
$found = true;
70+
break;
5371
}
54-
}
55-
}
72+
73+
$next = $phpcsFile->findNext(T_COMMA, ($next + 1), $tokens[$declare]['parenthesis_closer']);
74+
} while ($next !== false && $next < $tokens[$declare]['parenthesis_closer']);
75+
}//end if
5676

5777
if ($found === false) {
5878
$error = 'Missing required strict_types declaration';
5979
$phpcsFile->addError($error, $stackPtr, 'MissingDeclaration');
80+
81+
return $phpcsFile->numTokens;
82+
}
83+
84+
// Strict types declaration found, make sure strict types is enabled.
85+
$skip = Tokens::$emptyTokens;
86+
$skip[] = T_EQUAL;
87+
$valuePtr = $phpcsFile->findNext($skip, ($next + 1), null, true);
88+
89+
if ($valuePtr !== false
90+
&& $tokens[$valuePtr]['code'] === T_LNUMBER
91+
&& $tokens[$valuePtr]['content'] === '0'
92+
) {
93+
$error = 'Required strict_types declaration found, but strict types is disabled. Set the value to 1 to enable';
94+
$fix = $phpcsFile->addFixableWarning($error, $valuePtr, 'Disabled');
95+
96+
if ($fix === true) {
97+
$phpcsFile->fixer->replaceToken($valuePtr, '1');
98+
}
6099
}
61100

62101
// Skip the rest of the file so we don't pick up additional
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=0);
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);
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 = /*comment*/ 0 );
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 = /*comment*/ 1 );
4+
5+
// some code
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
// Safeguard sniff handles parse error/live coding correctly.
3+
declare(strict_types=
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 = 0, 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( strict_types = 1, encoding='utf-8' );
4+
5+
// some code

0 commit comments

Comments
 (0)