Skip to content

Commit

Permalink
LineAfter sniff - fixed case when next content is doc-block
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbundyra committed May 1, 2019
1 parent d0d09ff commit 6a97e0f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/WebimpressCodingStandard/Sniffs/Methods/LineAfterSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@

use function in_array;
use function max;
use function strpos;

use const T_ANON_CLASS;
use const T_CLASS;
use const T_CLOSE_CURLY_BRACKET;
use const T_DOC_COMMENT;
use const T_DOC_COMMENT_CLOSE_TAG;
use const T_DOC_COMMENT_OPEN_TAG;
use const T_DOC_COMMENT_STAR;
use const T_DOC_COMMENT_STRING;
use const T_DOC_COMMENT_TAG;
use const T_DOC_COMMENT_WHITESPACE;
use const T_FUNCTION;
use const T_INTERFACE;
use const T_SEMICOLON;
Expand Down Expand Up @@ -42,17 +50,33 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop
$closer = $phpcsFile->findNext(T_SEMICOLON, $tokens[$stackPtr]['parenthesis_closer'] + 1);
}

$emptyTokens = Tokens::$emptyTokens;
unset(
$emptyTokens[T_DOC_COMMENT],
$emptyTokens[T_DOC_COMMENT_STAR],
$emptyTokens[T_DOC_COMMENT_WHITESPACE],
$emptyTokens[T_DOC_COMMENT_TAG],
$emptyTokens[T_DOC_COMMENT_OPEN_TAG],
$emptyTokens[T_DOC_COMMENT_CLOSE_TAG],
$emptyTokens[T_DOC_COMMENT_STRING]
);

$lastInLine = $closer;
while ($tokens[$lastInLine + 1]['line'] === $tokens[$closer]['line']
&& in_array($tokens[$lastInLine + 1]['code'], Tokens::$emptyTokens, true)
&& in_array($tokens[$lastInLine + 1]['code'], $emptyTokens, true)
) {
++$lastInLine;
}
while ($tokens[$lastInLine]['code'] === T_WHITESPACE) {
--$lastInLine;
}

$contentAfter = $phpcsFile->findNext(T_WHITESPACE, $lastInLine + 1, null, true);
if ($tokens[$lastInLine]['code'] === T_DOC_COMMENT_OPEN_TAG) {
$contentAfter = $lastInLine;
} else {
$contentAfter = $phpcsFile->findNext(T_WHITESPACE, $lastInLine + 1, null, true);
}

if ($contentAfter !== false
&& $tokens[$contentAfter]['line'] - $tokens[$closer]['line'] !== 2
&& $tokens[$contentAfter]['code'] !== T_CLOSE_CURLY_BRACKET
Expand All @@ -64,8 +88,18 @@ protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScop

if ($fix) {
if ($found) {
$skip = 2;

$phpcsFile->fixer->beginChangeset();
for ($i = $closer + 1; $i < $contentAfter - 1; $i++) {
for ($i = $contentAfter - 1; $i > $closer; --$i) {
if ($skip) {
if (strpos($tokens[$i]['content'], $phpcsFile->eolChar) !== false) {
--$skip;
}

continue;
}

$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->endChangeset();
Expand Down
19 changes: 19 additions & 0 deletions test/Sniffs/Methods/LineAfterUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ abstract class LineAfter
} /* another comment */public function inTheSameLine()
{
} # comment

public function nextWithDocBlock()
{
}


/**
* @return int
*/
public function returnInt()
{
return 0;
}/**
* @return string
*/
public function returnString()
{
return '';
}
}
20 changes: 20 additions & 0 deletions test/Sniffs/Methods/LineAfterUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ private function method7(){}
public function inTheSameLine()
{
} # comment

public function nextWithDocBlock()
{
}

/**
* @return int
*/
public function returnInt()
{
return 0;
}

/**
* @return string
*/
public function returnString()
{
return '';
}
}
2 changes: 2 additions & 0 deletions test/Sniffs/Methods/LineAfterUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ protected function getErrorList(string $testFile = '') : array
24 => 1,
29 => 1,
32 => 1,
38 => 1,
47 => 1,
];
}

Expand Down

0 comments on commit 6a97e0f

Please sign in to comment.