Skip to content

Commit

Permalink
Merge branch 'hotfix/5'
Browse files Browse the repository at this point in the history
Close #5
  • Loading branch information
michalbundyra committed May 10, 2019
2 parents d0d09ff + de75d79 commit d4054a6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#5](https://github.com/webimpress/coding-standard/pull/5) `Methods\LineAfterSniff` - fixes adding empty line after method when next content is doc-block

## 1.0.0 - 2019-03-07

Expand Down
33 changes: 31 additions & 2 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,9 +50,20 @@ 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;
}
Expand All @@ -64,8 +83,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 d4054a6

Please sign in to comment.