Skip to content

Commit

Permalink
Generic/RequireStrictTypes: add warning for when value is 0
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Dec 5, 2023
1 parent 9fa78c2 commit 3ff23a3
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ The file documents changes to the PHP_CodeSniffer project.
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Generic.Functions.OpeningFunctionBraceKernighanRitchie will now check the spacing before the opening brace for empty functions
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Generic.PHP.RequireStrictTypes has a new warning for when there is a declare statement, but the strict_types directive is set to 0
- The warning can be turned off by excluding the Generic.PHP.RequireStrictTypes.Disabled error code
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- PSR2.Classes.PropertyDeclaration now enforces that the readonly modifier comes after the visibility modifier
- PSR2 and PSR12 do not have documented rules for this as they pre-date the readonly modifier
- PSR-PER has been used to confirm the order of this keyword so it can be applied to PSR2 and PSR12 correctly
Expand Down
19 changes: 19 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/RequireStrictTypesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ public function process(File $phpcsFile, $stackPtr)
if ($found === false) {
$error = 'Missing required strict_types declaration';
$phpcsFile->addError($error, $stackPtr, 'MissingDeclaration');

return $phpcsFile->numTokens;
}

// Strict types declaration found, make sure strict types is enabled.
$skip = Tokens::$emptyTokens;
$skip[] = T_EQUAL;
$valuePtr = $phpcsFile->findNext($skip, ($next + 1), null, true);

if ($valuePtr !== false
&& $tokens[$valuePtr]['code'] === T_LNUMBER
&& $tokens[$valuePtr]['content'] === '0'
) {
$error = 'Required strict_types declaration found, but strict types is disabled. Set the value to 1 to enable';
$fix = $phpcsFile->addFixableWarning($error, $valuePtr, 'Disabled');

if ($fix === true) {
$phpcsFile->fixer->replaceToken($valuePtr, '1');
}
}

// Skip the rest of the file so we don't pick up additional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=0);

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare( strict_types = /*comment*/ 0 );

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare( strict_types = /*comment*/ 1 );

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
// Safeguard sniff handles parse error/live coding correctly.
declare(strict_types=
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare( strict_types = 0, encoding='utf-8' );

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare( strict_types = 1, encoding='utf-8' );

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(encoding='utf-8', strict_types=0);

// some code
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(encoding='utf-8', strict_types=1);

// some code
16 changes: 12 additions & 4 deletions src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ public function getErrorList($testFile='')
/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
* @param string $testFile The name of the file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [];
switch ($testFile) {
case 'RequireStrictTypesUnitTest.11.inc':
case 'RequireStrictTypesUnitTest.12.inc':
case 'RequireStrictTypesUnitTest.14.inc':
case 'RequireStrictTypesUnitTest.15.inc':
return [3 => 1];

default:
return [];
}

}//end getWarningList()

Expand Down

0 comments on commit 3ff23a3

Please sign in to comment.