-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New sniffs for enforcing require strict types & formatting of declare…
… statements (ref #2365)
- Loading branch information
Showing
9 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Standards/Generic/Sniffs/PHP/RequireStrictTypesSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* Checks that the strict_types has been declared. | ||
* | ||
* @author Sertan Danis <sdanis@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\PHP; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
class RequireStrictTypesSniff implements Sniff | ||
{ | ||
|
||
|
||
/** | ||
* Returns an array of tokens this test wants to listen for. | ||
* | ||
* @return array | ||
*/ | ||
public function register() | ||
{ | ||
return [T_OPEN_TAG]; | ||
|
||
}//end register() | ||
|
||
|
||
/** | ||
* Processes this sniff, when one of its tokens is encountered. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token in | ||
* the stack passed in $tokens. | ||
* | ||
* @return int | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$tokens = $phpcsFile->getTokens(); | ||
$declare = $phpcsFile->findNext(T_DECLARE, $stackPtr); | ||
$found = false; | ||
|
||
if ($declare !== false) { | ||
$nextString = $phpcsFile->findNext(T_STRING, $declare); | ||
|
||
if ($nextString !== false) { | ||
if (strtolower($tokens[$nextString]['content']) === 'strict_types') { | ||
// There is a strict types declaration. | ||
$found = true; | ||
} | ||
} | ||
} | ||
|
||
if ($found === false) { | ||
$error = 'Missing required strict_types declaration'; | ||
$phpcsFile->addError($error, $stackPtr, 'MissingDeclaration'); | ||
} | ||
|
||
// Skip the rest of the file so we don't pick up additional | ||
// open tags, typically embedded in HTML. | ||
return $phpcsFile->numTokens; | ||
|
||
}//end process() | ||
|
||
|
||
}//end class |
8 changes: 8 additions & 0 deletions
8
src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.1.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
|
||
declare(strict_types=1); | ||
|
||
declare(ticks=1) { | ||
// some code | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.2.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?php | ||
declare(ticks=1); |
56 changes: 56 additions & 0 deletions
56
src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* Unit test class for the RequireStrictType sniff. | ||
* | ||
* @author Sertan Danis <sdanis@squiz.net> | ||
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600) | ||
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence | ||
*/ | ||
|
||
namespace PHP_CodeSniffer\Standards\Generic\Tests\PHP; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
class RequireStrictTypesUnitTest extends AbstractSniffUnitTest | ||
{ | ||
|
||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* The key of the array should represent the line number and the value | ||
* should represent the number of errors that should occur on that line. | ||
* | ||
* @param string $testFile The name of the file being tested. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getErrorList($testFile='') | ||
{ | ||
switch ($testFile) { | ||
case 'RequireStrictTypesUnitTest.1.inc': | ||
return []; | ||
break; | ||
} | ||
|
||
return [1 => 1]; | ||
|
||
}//end getErrorList() | ||
|
||
|
||
/** | ||
* 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. | ||
* | ||
* @return array<int, int> | ||
*/ | ||
public function getWarningList() | ||
{ | ||
return []; | ||
|
||
}//end getWarningList() | ||
|
||
|
||
}//end class |
Oops, something went wrong.