Skip to content

Commit f362965

Browse files
committed
Squiz/FunctionSpacing: improve handling with comments before first/after last
If there would be comments (or commented out code) before the first method or after the last method in an OO structure, the fixer as it was, would - depending on the various `$spacing` settings, potentially remove the space between the method and the comment which would easily conflict with other comment rules. By allowing for comments before the first method/after the last method, this is prevented. The fix now implemented means that if there is anything between the class opener/trait import `use` and the first method, other than a function docblock/comment, the method will be treated as any method and won't get the special "first method" treatment. Similarly, when there is anything between the last method and the class closer, other than a trailing comment, the method will be treated as any method and won't get the special "last" method treatment. Includes unit test.
1 parent 82cd0f8 commit f362965

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)