Skip to content

Commit

Permalink
Fixed bug squizlabs#1141 : Sniffs that check EOF newlines don't detec…
Browse files Browse the repository at this point in the history
…t newlines properly when the last token is a doc block
  • Loading branch information
gsherwood committed Sep 7, 2016
1 parent 7a92eef commit 174cd04
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$stackPtr = ($phpcsFile->numTokens - 1);

if ($phpcsFile->tokenizerType !== 'PHP') {
if ($tokens[$stackPtr]['content'] === '') {
$stackPtr--;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$stackPtr = ($phpcsFile->numTokens - 1);

if ($phpcsFile->tokenizerType !== 'PHP') {
if ($tokens[$stackPtr]['content'] === '') {
$stackPtr--;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getErrorList($testFile='')
{
switch ($testFile) {
case 'EndFileNewlineUnitTest.3.inc':
case 'EndFileNewlineUnitTest.4.inc':
case 'EndFileNewlineUnitTest.3.js':
case 'EndFileNewlineUnitTest.3.css':
return array(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getErrorList($testFile='')
break;
case 'EndFileNoNewlineUnitTest.2.css':
case 'EndFileNoNewlineUnitTest.2.js':
case 'EndFileNoNewlineUnitTest.6.inc':
return array(
2 => 1,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens();
$lastToken = ($phpcsFile->numTokens - 1);

if ($tokens[$lastToken]['content'] === '') {
$lastToken--;
}

// Hard-coding the expected \n in this sniff as it is PSR-2 specific and
// PSR-2 enforces the use of unix style newlines.
if (substr($tokens[$lastToken]['content'], -1) !== "\n") {
Expand All @@ -75,8 +79,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)

// Go looking for the last non-empty line.
$lastLine = $tokens[$lastToken]['line'];
if ($tokens[$lastToken]['code'] === T_WHITESPACE) {
$lastCode = $phpcsFile->findPrevious(T_WHITESPACE, ($lastToken - 1), null, true);
if ($tokens[$lastToken]['code'] === T_WHITESPACE
|| $tokens[$lastToken]['code'] === T_DOC_COMMENT_WHITESPACE) {
$lastCode = $phpcsFile->findPrevious(array(T_WHITESPACE, T_DOC_COMMENT_WHITESPACE), ($lastToken - 1), null, true);
} else {
$lastCode = $lastToken;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
/** Why me?

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
/** Why me?
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function getErrorList($testFile='')
case 'EndFileNewlineUnitTest.3.inc':
case 'EndFileNewlineUnitTest.6.inc':
case 'EndFileNewlineUnitTest.7.inc':
case 'EndFileNewlineUnitTest.9.inc':
case 'EndFileNewlineUnitTest.10.inc':
return array(
2 => 1,
);
Expand Down
10 changes: 10 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
<notes>
- Squiz.ControlStructures.ControlSignature.SpaceAfterCloseParenthesis fix now removes unnecessary whitespace
- Fixed bug #1141 : Sniffs that check EOF newlines don't detect newlines properly when the last token is a doc block
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -573,6 +574,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.3.css" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.3.js" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand All @@ -586,6 +589,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.3.js" role="test" />
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.6.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNoNewlineUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand Down Expand Up @@ -1329,6 +1334,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.4.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.5.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.6.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.7.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.8.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.9.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.10.inc" role="test" />
<file baseinstalldir="PHP" name="EndFileNewlineUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand Down

0 comments on commit 174cd04

Please sign in to comment.