Skip to content

Commit 859ae3d

Browse files
committed
Merge branch 'feature/squiz-functionspacing-enhancement' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents dfd9eb1 + f362965 commit 859ae3d

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

src/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,26 @@ public function process(File $phpcsFile, $stackPtr)
112112
$isFirst = false;
113113
$isLast = false;
114114

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

117117
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
118+
if ($tokens[$prev]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
119+
// Skip past function docblocks.
120+
$prev = $phpcsFile->findPrevious($ignore, ($tokens[$prev]['comment_opener'] - 1), null, true);
121+
}
122+
118123
if ($tokens[$prev]['code'] === T_OPEN_CURLY_BRACKET) {
119124
$isFirst = true;
120125
}
121126

122127
$next = $phpcsFile->findNext($ignore, ($closer + 1), null, true);
128+
if (isset(Tokens::$emptyTokens[$tokens[$next]['code']]) === true
129+
&& $tokens[$next]['line'] === $tokens[$closer]['line']
130+
) {
131+
// Skip past "end" comments.
132+
$next = $phpcsFile->findNext($ignore, ($next + 1), null, true);
133+
}
134+
123135
if ($tokens[$next]['code'] === T_CLOSE_CURLY_BRACKET) {
124136
$isLast = true;
125137
}

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,51 @@ interface OneBlankLineBeforeFirstFunctionClassInterface
495495
public function interfaceMethod();
496496
}
497497

498+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 1
499+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 0
500+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 0
501+
502+
class MyClass {
503+
504+
// phpcs:disable Stnd.Cat.Sniff -- For reasons.
505+
506+
/**
507+
* Description.
508+
*/
509+
function a(){}
510+
511+
/**
512+
* Description.
513+
*/
514+
function b(){}
515+
516+
/**
517+
* Description.
518+
*/
519+
function c(){}
520+
521+
// phpcs:enable
522+
}
523+
524+
class MyClass {
525+
// Some unrelated comment
526+
/**
527+
* Description.
528+
*/
529+
function a(){}
530+
531+
/**
532+
* Description.
533+
*/
534+
function b(){}
535+
536+
/**
537+
* Description.
538+
*/
539+
function c(){}
540+
// function d() {}
541+
}
542+
498543
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
499544
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
500545
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc.fixed

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,53 @@ interface OneBlankLineBeforeFirstFunctionClassInterface
577577
public function interfaceMethod();
578578
}
579579

580+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 1
581+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 0
582+
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 0
583+
584+
class MyClass {
585+
586+
// phpcs:disable Stnd.Cat.Sniff -- For reasons.
587+
588+
/**
589+
* Description.
590+
*/
591+
function a(){}
592+
593+
/**
594+
* Description.
595+
*/
596+
function b(){}
597+
598+
/**
599+
* Description.
600+
*/
601+
function c(){}
602+
603+
// phpcs:enable
604+
}
605+
606+
class MyClass {
607+
// Some unrelated comment
608+
609+
/**
610+
* Description.
611+
*/
612+
function a(){}
613+
614+
/**
615+
* Description.
616+
*/
617+
function b(){}
618+
619+
/**
620+
* Description.
621+
*/
622+
function c(){}
623+
624+
// function d() {}
625+
}
626+
580627
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
581628
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
582629
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2

src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function getErrorList($testFile='')
8888
479 => 1,
8989
483 => 2,
9090
495 => 1,
91+
529 => 1,
92+
539 => 1,
9193
];
9294

9395
case 'FunctionSpacingUnitTest.2.inc':

0 commit comments

Comments
 (0)