Skip to content

Commit

Permalink
Added PSR12.Files.OpenTag to enforce that the open tag is on a line b…
Browse files Browse the repository at this point in the history
…y itself when used at the start of a php-only file (ref squizlabs#750)
  • Loading branch information
gsherwood committed Sep 2, 2019
1 parent 8c9b10a commit e201da5
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- Enforces the order and formatting of file header blocks
- Added PSR12.Files.ImportStatement sniff
-- Enforces the formatting of import statements within a file
- Added PSR12.Files.OpenTag sniff
-- Enforces that the open tag is on a line by itself when used at the start of a php-only file
- Added PSR12.Functions.ReturnTypeDeclaration sniff
-- Enforces the formatting of return type declarations in functions and closures
- Added PSR12.Properties.ConstantVisibility sniff
Expand Down Expand Up @@ -1118,6 +1120,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementSniff.php" role="php" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagSniff.php" role="php" />
</dir>
<dir name="Functions">
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationSniff.php" role="php" />
Expand Down Expand Up @@ -1159,6 +1162,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.2.inc.fixed" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.php" role="test" />
</dir>
<dir name="Functions">
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationUnitTest.inc" role="test" />
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<rule ref="PSR2.Classes.PropertyDeclaration"/>
<rule ref="PSR2.Methods.MethodDeclaration"/>
<rule ref="PSR2.Files.EndFileNewline"/>
<rule ref="PSR12.Files.OpenTag"/>
<rule ref="Zend.Files.ClosingTag"/>

<!-- PEAR uses warnings for inline control structures, so switch back to errors -->
Expand Down
68 changes: 68 additions & 0 deletions src/Standards/PSR12/Sniffs/Files/OpenTagSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Checks that the open tag is defined correctly.
*
* @author Greg Sherwood <gsherwood@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\PSR12\Sniffs\Files;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

class OpenTagSniff 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.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
if ($stackPtr !== 0) {
// This rule only applies if the open tag is on the first line of the file.
return $phpcsFile->numTokens;
}

$next = $phpcsFile->findNext(T_INLINE_HTML, 0);
if ($next !== false) {
// This rule only applies to PHP-only files.
return $phpcsFile->numTokens;
}

$tokens = $phpcsFile->getTokens();
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
$error = 'Opening PHP tag must be on a line by itself';
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone');
if ($fix === true) {
$phpcsFile->fixer->addNewline($stackPtr);
}
}

return $phpcsFile->numTokens;

}//end process()


}//end class
3 changes: 3 additions & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo 'hi';
1 change: 1 addition & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo 'hi';
2 changes: 2 additions & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.2.inc.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
echo 'hi';
3 changes: 3 additions & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php echo 'hi';
?>
hi
2 changes: 2 additions & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<?php echo 'hi';
55 changes: 55 additions & 0 deletions src/Standards/PSR12/Tests/Files/OpenTagUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Unit test class for the OpenTag sniff.
*
* @author Greg Sherwood <gsherwood@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\PSR12\Tests\Files;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class OpenTagUnitTest 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 'OpenTagUnitTest.2.inc':
return [1 => 1];
default:
return [];
}//end switch

}//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

0 comments on commit e201da5

Please sign in to comment.