Skip to content

Commit e201da5

Browse files
committed
Added PSR12.Files.OpenTag to enforce that the open tag is on a line by itself when used at the start of a php-only file (ref #750)
1 parent 8c9b10a commit e201da5

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed

package.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
7373
-- Enforces the order and formatting of file header blocks
7474
- Added PSR12.Files.ImportStatement sniff
7575
-- Enforces the formatting of import statements within a file
76+
- Added PSR12.Files.OpenTag sniff
77+
-- Enforces that the open tag is on a line by itself when used at the start of a php-only file
7678
- Added PSR12.Functions.ReturnTypeDeclaration sniff
7779
-- Enforces the formatting of return type declarations in functions and closures
7880
- Added PSR12.Properties.ConstantVisibility sniff
@@ -1118,6 +1120,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
11181120
<file baseinstalldir="PHP/CodeSniffer" name="DeclareStatementSniff.php" role="php" />
11191121
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderSniff.php" role="php" />
11201122
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementSniff.php" role="php" />
1123+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagSniff.php" role="php" />
11211124
</dir>
11221125
<dir name="Functions">
11231126
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationSniff.php" role="php" />
@@ -1159,6 +1162,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
11591162
<file baseinstalldir="PHP/CodeSniffer" name="FileHeaderUnitTest.php" role="test" />
11601163
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.inc" role="test" />
11611164
<file baseinstalldir="PHP/CodeSniffer" name="ImportStatementUnitTest.php" role="test" />
1165+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.1.inc" role="test" />
1166+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.2.inc" role="test" />
1167+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.2.inc.fixed" role="test" />
1168+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.3.inc" role="test" />
1169+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.4.inc" role="test" />
1170+
<file baseinstalldir="PHP/CodeSniffer" name="OpenTagUnitTest.php" role="test" />
11621171
</dir>
11631172
<dir name="Functions">
11641173
<file baseinstalldir="PHP/CodeSniffer" name="NullableTypeDeclarationUnitTest.inc" role="test" />

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<rule ref="PSR2.Classes.PropertyDeclaration"/>
7272
<rule ref="PSR2.Methods.MethodDeclaration"/>
7373
<rule ref="PSR2.Files.EndFileNewline"/>
74+
<rule ref="PSR12.Files.OpenTag"/>
7475
<rule ref="Zend.Files.ClosingTag"/>
7576

7677
<!-- PEAR uses warnings for inline control structures, so switch back to errors -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Checks that the open tag is defined correctly.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Sniffs\Files;
11+
12+
use PHP_CodeSniffer\Sniffs\Sniff;
13+
use PHP_CodeSniffer\Files\File;
14+
15+
class OpenTagSniff implements Sniff
16+
{
17+
18+
19+
/**
20+
* Returns an array of tokens this test wants to listen for.
21+
*
22+
* @return array
23+
*/
24+
public function register()
25+
{
26+
return [T_OPEN_TAG];
27+
28+
}//end register()
29+
30+
31+
/**
32+
* Processes this sniff when one of its tokens is encountered.
33+
*
34+
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
35+
* @param int $stackPtr The position of the current
36+
* token in the stack.
37+
*
38+
* @return void
39+
*/
40+
public function process(File $phpcsFile, $stackPtr)
41+
{
42+
if ($stackPtr !== 0) {
43+
// This rule only applies if the open tag is on the first line of the file.
44+
return $phpcsFile->numTokens;
45+
}
46+
47+
$next = $phpcsFile->findNext(T_INLINE_HTML, 0);
48+
if ($next !== false) {
49+
// This rule only applies to PHP-only files.
50+
return $phpcsFile->numTokens;
51+
}
52+
53+
$tokens = $phpcsFile->getTokens();
54+
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
55+
if ($tokens[$next]['line'] === $tokens[$stackPtr]['line']) {
56+
$error = 'Opening PHP tag must be on a line by itself';
57+
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'NotAlone');
58+
if ($fix === true) {
59+
$phpcsFile->fixer->addNewline($stackPtr);
60+
}
61+
}
62+
63+
return $phpcsFile->numTokens;
64+
65+
}//end process()
66+
67+
68+
}//end class
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo 'hi';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php echo 'hi';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
echo 'hi';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php echo 'hi';
2+
?>
3+
hi
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
<?php echo 'hi';
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Unit test class for the OpenTag sniff.
4+
*
5+
* @author Greg Sherwood <gsherwood@squiz.net>
6+
* @copyright 2006-2019 Squiz Pty Ltd (ABN 77 084 670 600)
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Standards\PSR12\Tests\Files;
11+
12+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
14+
class OpenTagUnitTest extends AbstractSniffUnitTest
15+
{
16+
17+
18+
/**
19+
* Returns the lines where errors should occur.
20+
*
21+
* The key of the array should represent the line number and the value
22+
* should represent the number of errors that should occur on that line.
23+
*
24+
* @param string $testFile The name of the file being tested.
25+
*
26+
* @return array<int, int>
27+
*/
28+
public function getErrorList($testFile='')
29+
{
30+
switch ($testFile) {
31+
case 'OpenTagUnitTest.2.inc':
32+
return [1 => 1];
33+
default:
34+
return [];
35+
}//end switch
36+
37+
}//end getErrorList()
38+
39+
40+
/**
41+
* Returns the lines where warnings should occur.
42+
*
43+
* The key of the array should represent the line number and the value
44+
* should represent the number of warnings that should occur on that line.
45+
*
46+
* @return array<int, int>
47+
*/
48+
public function getWarningList()
49+
{
50+
return [];
51+
52+
}//end getWarningList()
53+
54+
55+
}//end class

0 commit comments

Comments
 (0)