Skip to content

Commit cf0a195

Browse files
committed
PHP 8.0 | Squiz.WhiteSpace.FunctionSpacing: add support for attributes
1 parent 1030040 commit cf0a195

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ public function process(File $phpcsFile, $stackPtr)
115115
$ignore = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$methodPrefixes);
116116

117117
$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);
118+
119+
while ($tokens[$prev]['code'] === T_ATTRIBUTE_END) {
120+
// Skip past function attributes.
121+
$prev = $phpcsFile->findPrevious($ignore, ($tokens[$prev]['attribute_opener'] - 1), null, true);
122+
}
123+
118124
if ($tokens[$prev]['code'] === T_DOC_COMMENT_CLOSE_TAG) {
119125
// Skip past function docblocks.
120126
$prev = $phpcsFile->findPrevious($ignore, ($tokens[$prev]['comment_opener'] - 1), null, true);
@@ -241,6 +247,14 @@ public function process(File $phpcsFile, $stackPtr)
241247
return;
242248
}
243249

250+
while ($tokens[$prevContent]['code'] === T_ATTRIBUTE_END
251+
&& $tokens[$prevContent]['line'] === ($currentLine - 1)
252+
) {
253+
// Account for function attributes.
254+
$currentLine = $tokens[$tokens[$prevContent]['attribute_opener']]['line'];
255+
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($tokens[$prevContent]['attribute_opener'] - 1), null, true);
256+
}
257+
244258
if ($tokens[$prevContent]['code'] === T_DOC_COMMENT_CLOSE_TAG
245259
&& $tokens[$prevContent]['line'] === ($currentLine - 1)
246260
) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,37 @@ class MyClass {
540540
// function d() {}
541541
}
542542

543+
class ClassWithAttributes {
544+
545+
#[Attribute1]
546+
#[Attribute2]
547+
function a(){}
548+
549+
550+
#[Attribute3]
551+
function b(){}
552+
#[Attribute4]
553+
function c(){}
554+
555+
556+
/**
557+
* Description.
558+
*/
559+
#[Attribute5]
560+
function d(){}
561+
/**
562+
* Description.
563+
*/
564+
#[Attribute6]
565+
#[Attribute7]
566+
function e(){}
567+
568+
569+
#[Attribute8]
570+
#[Attribute9]
571+
function f(){}
572+
}
573+
543574
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
544575
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
545576
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,35 @@ class MyClass {
624624
// function d() {}
625625
}
626626

627+
class ClassWithAttributes {
628+
#[Attribute1]
629+
#[Attribute2]
630+
function a(){}
631+
632+
#[Attribute3]
633+
function b(){}
634+
635+
#[Attribute4]
636+
function c(){}
637+
638+
/**
639+
* Description.
640+
*/
641+
#[Attribute5]
642+
function d(){}
643+
644+
/**
645+
* Description.
646+
*/
647+
#[Attribute6]
648+
#[Attribute7]
649+
function e(){}
650+
651+
#[Attribute8]
652+
#[Attribute9]
653+
function f(){}
654+
}
655+
627656
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacing 2
628657
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingBeforeFirst 2
629658
// phpcs:set Squiz.WhiteSpace.FunctionSpacing spacingAfterLast 2

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public function getErrorList($testFile='')
9090
495 => 1,
9191
529 => 1,
9292
539 => 1,
93+
547 => 2,
94+
551 => 1,
95+
553 => 1,
96+
560 => 1,
97+
566 => 1,
9398
];
9499

95100
case 'FunctionSpacingUnitTest.2.inc':

0 commit comments

Comments
 (0)