Skip to content

Commit bc8b4c8

Browse files
committed
Further clarification has revealed that the original requirement for two newlines at the end of a file was actually correct for the PSR2 standard. So this sniff had been added back, but with one minor change; it only applies for files that only contain PHP code and nothing else (like HTML).
1 parent 9f76a00 commit bc8b4c8

9 files changed

+205
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Generic_Sniffs_Files_EndFileNewlineSniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Generic_Sniffs_Files_EndFileNewlineSniff.
17+
*
18+
* Ensures the file ends with a newline character.
19+
*
20+
* @category PHP
21+
* @package PHP_CodeSniffer
22+
* @author Greg Sherwood <[email protected]>
23+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
24+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
25+
* @version Release: @package_version@
26+
* @link http://pear.php.net/package/PHP_CodeSniffer
27+
*/
28+
class PSR2_Sniffs_Files_EndFileNewlineSniff implements PHP_CodeSniffer_Sniff
29+
{
30+
31+
/**
32+
* Returns an array of tokens this test wants to listen for.
33+
*
34+
* @return array
35+
*/
36+
public function register()
37+
{
38+
return array(T_OPEN_TAG);
39+
40+
}//end register()
41+
42+
43+
/**
44+
* Processes this sniff, when one of its tokens is encountered.
45+
*
46+
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
47+
* @param int $stackPtr The position of the current token in
48+
* the stack passed in $tokens.
49+
*
50+
* @return void
51+
*/
52+
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53+
{
54+
// We are only interested if this is the first open tag and in a file
55+
// that only contains PHP code.
56+
if ($stackPtr !== 0) {
57+
if ($phpcsFile->findPrevious(array(T_OPEN_TAG, T_INLINE_HTML), ($stackPtr - 1)) !== false) {
58+
return;
59+
}
60+
}
61+
62+
if ($phpcsFile->findNext(T_INLINE_HTML, ($stackPtr + 1)) !== false) {
63+
return;
64+
}
65+
66+
// Skip to the end of the file.
67+
$tokens = $phpcsFile->getTokens();
68+
$stackPtr = ($phpcsFile->numTokens - 1);
69+
70+
// Go looking for the last non-empty line.
71+
$lastLine = $tokens[$stackPtr]['line'];
72+
while ($tokens[$stackPtr]['code'] === T_WHITESPACE) {
73+
$stackPtr--;
74+
}
75+
76+
$lastCodeLine = $tokens[$stackPtr]['line'];
77+
$blankLines = $lastLine - $lastCodeLine;
78+
if ($blankLines === 0) {
79+
$error = 'Expected 1 blank line at end of file; 0 found';
80+
$data = array($blankLines);
81+
$phpcsFile->addError($error, $stackPtr, 'NotFound', $data);
82+
} else if ($blankLines > 1) {
83+
$error = 'Expected 1 blank line at end of file; "%s" found';
84+
$data = array($blankLines);
85+
$phpcsFile->addError($error, $stackPtr, 'TooMany', $data);
86+
}
87+
88+
}//end process()
89+
90+
91+
}//end class
92+
93+
?>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
echo 'foo';
3+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
echo 'foo';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
echo 'foo';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
echo 'foo';
3+
?>
4+
</body>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
echo 'foo';
3+
?>
4+
</body>
5+
<?php
6+
echo 'bar';
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Unit test class for the EndFileNewline sniff.
4+
*
5+
* PHP version 5
6+
*
7+
* @category PHP
8+
* @package PHP_CodeSniffer
9+
* @author Greg Sherwood <[email protected]>
10+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
11+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12+
* @link http://pear.php.net/package/PHP_CodeSniffer
13+
*/
14+
15+
/**
16+
* Unit test class for the EndFileNewline sniff.
17+
*
18+
* A sniff unit test checks a .inc file for expected violations of a single
19+
* coding standard. Expected errors and warnings are stored in this class.
20+
*
21+
* @category PHP
22+
* @package PHP_CodeSniffer
23+
* @author Greg Sherwood <[email protected]>
24+
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
25+
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
26+
* @version Release: @package_version@
27+
* @link http://pear.php.net/package/PHP_CodeSniffer
28+
*/
29+
class PSR2_Tests_Files_EndFileNewlineUnitTest extends AbstractSniffUnitTest
30+
{
31+
32+
33+
/**
34+
* Returns the lines where errors should occur.
35+
*
36+
* The key of the array should represent the line number and the value
37+
* should represent the number of errors that should occur on that line.
38+
*
39+
* @param string $testFile The name of the file being tested.
40+
*
41+
* @return array(int => int)
42+
*/
43+
public function getErrorList($testFile='')
44+
{
45+
switch ($testFile) {
46+
case 'EndFileNewlineUnitTest.2.inc':
47+
case 'EndFileNewlineUnitTest.3.inc':
48+
return array(
49+
2 => 1,
50+
);
51+
break;
52+
default:
53+
return array();
54+
break;
55+
}//end switch
56+
57+
}//end getErrorList()
58+
59+
60+
/**
61+
* Returns the lines where warnings should occur.
62+
*
63+
* The key of the array should represent the line number and the value
64+
* should represent the number of warnings that should occur on that line.
65+
*
66+
* @param string $testFile The name of the file being tested.
67+
*
68+
* @return array(int => int)
69+
*/
70+
public function getWarningList($testFile='')
71+
{
72+
return array();
73+
74+
}//end getWarningList()
75+
76+
77+
}//end class
78+
79+
?>

CodeSniffer/Standards/PSR2/ruleset.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</rule>
2020

2121
<!-- All PHP files MUST end with a single blank line. -->
22-
<rule ref="Generic.Files.EndFileNewline"/>
22+
<!-- checked in Files/EndFileNewlineSniff -->
2323

2424
<!-- The closing ?> tag MUST be omitted from files containing only PHP. -->
2525
<rule ref="Zend.Files.ClosingTag"/>

package.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
945945
<tasks:replace from="@package_version@" to="version" type="package-info" />
946946
</file>
947947
</dir>
948+
<dir name="Files">
949+
<file baseinstalldir="PHP" name="EndFileNewlineSniff.php" role="php">
950+
<tasks:replace from="@package_version@" to="version" type="package-info" />
951+
</file>
952+
</dir>
948953
<dir name="Methods">
949954
<file baseinstalldir="PHP" name="MethodDeclarationSniff.php" role="php">
950955
<tasks:replace from="@package_version@" to="version" type="package-info" />
@@ -976,6 +981,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
976981
<tasks:replace from="@package_version@" to="version" type="package-info" />
977982
</file>
978983
</dir>
984+
<dir name="Files">
985+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.1.inc" role="test" />
986+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.2.inc" role="test" />
987+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.3.inc" role="test" />
988+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.4.inc" role="test" />
989+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.5.inc" role="test" />
990+
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.php" role="test">
991+
<tasks:replace from="@package_version@" to="version" type="package-info" />
992+
</file>
993+
</dir>
979994
<dir name="Methods">
980995
<file baseinstalldir="PHP" name="MethodDeclarationUnitTest.inc" role="test" />
981996
<file baseinstalldir="PHP" name="MethodDeclarationUnitTest.php" role="test">

0 commit comments

Comments
 (0)