Skip to content

Commit

Permalink
PHP 8.0 | Squiz.WhiteSpace.FunctionSpacing: add support for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
javer committed Mar 31, 2021
1 parent 1030040 commit cf0a195
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Standards/Squiz/Sniffs/WhiteSpace/FunctionSpacingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public function process(File $phpcsFile, $stackPtr)
$ignore = ([T_WHITESPACE => T_WHITESPACE] + Tokens::$methodPrefixes);

$prev = $phpcsFile->findPrevious($ignore, ($stackPtr - 1), null, true);

while ($tokens[$prev]['code'] === T_ATTRIBUTE_END) {
// Skip past function attributes.
$prev = $phpcsFile->findPrevious($ignore, ($tokens[$prev]['attribute_opener'] - 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);
Expand Down Expand Up @@ -241,6 +247,14 @@ public function process(File $phpcsFile, $stackPtr)
return;
}

while ($tokens[$prevContent]['code'] === T_ATTRIBUTE_END
&& $tokens[$prevContent]['line'] === ($currentLine - 1)
) {
// Account for function attributes.
$currentLine = $tokens[$tokens[$prevContent]['attribute_opener']]['line'];
$prevContent = $phpcsFile->findPrevious(T_WHITESPACE, ($tokens[$prevContent]['attribute_opener'] - 1), null, true);
}

if ($tokens[$prevContent]['code'] === T_DOC_COMMENT_CLOSE_TAG
&& $tokens[$prevContent]['line'] === ($currentLine - 1)
) {
Expand Down
31 changes: 31 additions & 0 deletions src/Standards/Squiz/Tests/WhiteSpace/FunctionSpacingUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,37 @@ class MyClass {
// function d() {}
}

class ClassWithAttributes {

#[Attribute1]
#[Attribute2]
function a(){}


#[Attribute3]
function b(){}
#[Attribute4]
function c(){}


/**
* Description.
*/
#[Attribute5]
function d(){}
/**
* Description.
*/
#[Attribute6]
#[Attribute7]
function e(){}


#[Attribute8]
#[Attribute9]
function f(){}
}

// 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 @@ -624,6 +624,35 @@ class MyClass {
// function d() {}
}

class ClassWithAttributes {
#[Attribute1]
#[Attribute2]
function a(){}

#[Attribute3]
function b(){}

#[Attribute4]
function c(){}

/**
* Description.
*/
#[Attribute5]
function d(){}

/**
* Description.
*/
#[Attribute6]
#[Attribute7]
function e(){}

#[Attribute8]
#[Attribute9]
function f(){}
}

// 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 @@ -90,6 +90,11 @@ public function getErrorList($testFile='')
495 => 1,
529 => 1,
539 => 1,
547 => 2,
551 => 1,
553 => 1,
560 => 1,
566 => 1,
];

case 'FunctionSpacingUnitTest.2.inc':
Expand Down

0 comments on commit cf0a195

Please sign in to comment.