Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,26 @@ public function process(File $phpcsFile, $stackPtr)
$isFirst = false;
$isLast = false;

$ignore = (Tokens::$emptyTokens + Tokens::$methodPrefixes);
$ignore = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$methodPrefixes);

$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
if ($tokens[$prev]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
// Skip past function docblocks.
$prev = $phpcsFile->findPrevious($ignore, ($tokens[$prev]['comment_opener'] - 1), null, true);
}

if ($tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET) {
$isFirst = true;
}

$next = $phpcsFile->findNext($ignore, ($closer + 1), null, true);
if (isset(Tokens::$emptyTokens[$tokens[$next]['code']]) === true
&& $tokens[$next]['line'] === $tokens[$closer]['line']
) {
// Skip past "end" comments.
$next = $phpcsFile->findNext($ignore, ($next + 1), null, true);
}

if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) {
$isLast = true;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,51 @@ interface OneBlankLineBeforeFirstFunctionClassInterface
public function interfaceMethod();
}

// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 1
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 0
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 0

class MyClass {

// phpcs:disable Stnd.Cat.Sniff -- For reasons.

/**
* Description.
*/
function a(){}

/**
* Description.
*/
function b(){}

/**
* Description.
*/
function c(){}

// phpcs:enable
}

class MyClass {
// Some unrelated comment
/**
* Description.
*/
function a(){}

/**
* Description.
*/
function b(){}

/**
* Description.
*/
function c(){}
// function d() {}
}

// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,53 @@ interface OneBlankLineBeforeFirstFunctionClassInterface
public function interfaceMethod();
}

// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 1
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 0
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 0

class MyClass {

// phpcs:disable Stnd.Cat.Sniff -- For reasons.

/**
* Description.
*/
function a(){}

/**
* Description.
*/
function b(){}

/**
* Description.
*/
function c(){}

// phpcs:enable
}

class MyClass {
// Some unrelated comment

/**
* Description.
*/
function a(){}

/**
* Description.
*/
function b(){}

/**
* Description.
*/
function c(){}

// function d() {}
}

// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function getErrorList($testFile='')
479 => 1,
483 => 2,
495 => 1,
529 => 1,
539 => 1,
];

case 'FunctionSpacingUnitTest.2.inc':
Expand Down