Skip to content

Commit 3ff23a3

Browse files
committed
Generic/RequireStrictTypes: add warning for when value is 0
Strict types is disabled when the value for the `strict_types` directive is `0`. As this sniff is supposed to be about enforcing the use of `strict_types`, `strict_types` declarations which turn the feature off should be flagged as well. Implemented with a separate error code to allow for selectively turning this warning off. Includes dedicated tests.
1 parent 9fa78c2 commit 3ff23a3

12 files changed

+77
-4
lines changed

CHANGELOG.md

Lines changed: 3 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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ public function process(File $phpcsFile, $stackPtr)
7777
if ($found === false) {
7878
$error = 'Missing required strict_types declaration';
7979
$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+
}
8099
}
81100

82101
// 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(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
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=0);
4+
5+
// some code

0 commit comments

Comments
 (0)