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 Nov 11, 2023
1 parent b42aebf commit 54a0dfe
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 4 deletions.
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 54a0dfe

Please sign in to comment.