Skip to content

Commit

Permalink
New sniffs for enforcing require strict types & formatting of declare…
Browse files Browse the repository at this point in the history
… statements (ref #2365)
  • Loading branch information
sertand committed Jul 4, 2019
1 parent 9c965b1 commit a5ac762
Show file tree
Hide file tree
Showing 9 changed files with 579 additions and 0 deletions.
12 changes: 12 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseKeywordSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="RequireStrictTypesSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="SyntaxSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="UpperCaseConstantSniff.php" role="php" />
Expand Down Expand Up @@ -670,6 +671,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="LowerCaseTypeUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NoSilencedErrorsUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="RequireStrictTypesUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="RequireStrictTypesUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="RequireStrictTypesUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SAPIUsageUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="SyntaxUnitTest.inc" role="test" />
Expand Down Expand Up @@ -1039,6 +1043,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<dir name="Classes">
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationSniff.php" role="php" />
</dir>
<dir name="Files">
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementSniff.php" role="php" />
</dir>
<dir name="Functions">
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationSniff.php" role="php" />
</dir>
Expand All @@ -1058,6 +1065,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ClassInstantiationUnitTest.php" role="test" />
</dir>
<dir name="Files">
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementUnitTest.php" role="test" />
</dir>
<dir name="Functions">
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationUnitTest.inc.fixed" role="test" />
Expand Down
69 changes: 69 additions & 0 deletions src/Standards/Generic/Sniffs/PHP/RequireStrictTypesSniff.php
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php


declare(strict_types=1);

declare(ticks=1) {
// some code
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
declare(ticks=1);
56 changes: 56 additions & 0 deletions src/Standards/Generic/Tests/PHP/RequireStrictTypesUnitTest.php
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
Loading

0 comments on commit a5ac762

Please sign in to comment.