-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MultiLineFunctionDeclaration): Add new sniff for multi-line func…
…tion declarations and trailing commas
- Loading branch information
Showing
9 changed files
with
223 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
coder_sniffer/Drupal/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* \Drupal\Sniffs\Functions\MultiLineTrailingCommaSniff. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
|
||
namespace Drupal\Sniffs\Functions; | ||
|
||
use PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\OpeningFunctionBraceKernighanRitchieSniff; | ||
use PHP_CodeSniffer\Standards\Squiz\Sniffs\Functions\MultiLineFunctionDeclarationSniff as SquizFunctionDeclarationSniff; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
/** | ||
* Multi-line function declarations need to have a trailing comma on the last | ||
* parameter. Modified from Squiz, whenever there is a function declaration | ||
* closing parenthesis on a new line we treat it as multi-line. | ||
* | ||
* @category PHP | ||
* @package PHP_CodeSniffer | ||
* @link http://pear.php.net/package/PHP_CodeSniffer | ||
*/ | ||
class MultiLineFunctionDeclarationSniff extends SquizFunctionDeclarationSniff | ||
{ | ||
|
||
|
||
/** | ||
* The number of spaces code should be indented. | ||
* | ||
* @var integer | ||
*/ | ||
public $indent = 2; | ||
|
||
|
||
/** | ||
* Processes single-line declarations. | ||
* | ||
* Just uses the Generic Kernighan Ritchie sniff. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* @param array<int, array<string, mixed>> $tokens The stack of tokens that make up | ||
* the file. | ||
* | ||
* @return void | ||
*/ | ||
public function processSingleLineDeclaration($phpcsFile, $stackPtr, $tokens) | ||
{ | ||
$sniff = new OpeningFunctionBraceKernighanRitchieSniff(); | ||
$sniff->checkClosures = true; | ||
$sniff->process($phpcsFile, $stackPtr); | ||
|
||
}//end processSingleLineDeclaration() | ||
|
||
|
||
/** | ||
* Determine if this is a multi-line function declaration. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* @param int $openBracket The position of the opening bracket | ||
* in the stack passed in $tokens. | ||
* @param array<int, array<string, mixed>> $tokens The stack of tokens that make up | ||
* the file. | ||
* | ||
* @return bool | ||
*/ | ||
public function isMultiLineDeclaration($phpcsFile, $stackPtr, $openBracket, $tokens) | ||
{ | ||
$function = $tokens[$stackPtr]; | ||
if ($tokens[$function['parenthesis_opener']]['line'] === $tokens[$function['parenthesis_closer']]['line']) { | ||
return false; | ||
} | ||
|
||
return true; | ||
|
||
}//end isMultiLineDeclaration() | ||
|
||
|
||
/** | ||
* Processes multi-line declarations. | ||
* | ||
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. | ||
* @param int $stackPtr The position of the current token | ||
* in the stack passed in $tokens. | ||
* @param array<int, array<string, mixed>> $tokens The stack of tokens that make up | ||
* the file. | ||
* | ||
* @return void | ||
*/ | ||
public function processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens) | ||
{ | ||
// We do everything the parent sniff does, and a bit more. | ||
parent::processMultiLineDeclaration($phpcsFile, $stackPtr, $tokens); | ||
|
||
// Trailing commas on the last function parameter are only possible in | ||
// PHP 8.0+. | ||
if (version_compare(PHP_VERSION, '8.0.0') < 0) { | ||
return; | ||
} | ||
|
||
$function = $tokens[$stackPtr]; | ||
|
||
$lastTrailingComma = $phpcsFile->findPrevious( | ||
Tokens::$emptyTokens, | ||
($function['parenthesis_closer'] - 1), | ||
$function['parenthesis_opener'], | ||
true | ||
); | ||
if ($tokens[$lastTrailingComma]['code'] !== T_COMMA) { | ||
$error = 'Multi-line function declarations must have a trailing comma after the last parameter'; | ||
$fix = $phpcsFile->addFixableError($error, $lastTrailingComma, 'MissingTrailingComma'); | ||
if ($fix === true) { | ||
$phpcsFile->fixer->addContent($lastTrailingComma, ','); | ||
} | ||
} | ||
|
||
}//end processMultiLineDeclaration() | ||
|
||
|
||
}//end class |
82 changes: 0 additions & 82 deletions
82
coder_sniffer/Drupal/Sniffs/Functions/MultiLineTrailingCommaSniff.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/Drupal/Functions/MultiLineFunctionDeclarationUnitTest.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Some description. | ||
*/ | ||
|
||
/** | ||
* Test. | ||
*/ | ||
function missing_trailing_comma( | ||
$a, | ||
$b | ||
) { | ||
|
||
} | ||
|
||
$foo = 1; | ||
$bar = 2; | ||
$x = function ( | ||
$a, | ||
$b | ||
) use ( | ||
$foo, | ||
$bar | ||
) { | ||
|
||
}; | ||
|
||
/** | ||
* | ||
*/ | ||
class Test { | ||
|
||
/** | ||
* Test. | ||
*/ | ||
public function lookupSource(string $key, string $migrationNames = '', array $options = [ | ||
'all' => NULL, | ||
'group' => NULL, | ||
]): void { | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
tests/Drupal/Functions/MultiLineFunctionDeclarationUnitTest.inc.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Some description. | ||
*/ | ||
|
||
/** | ||
* Test. | ||
*/ | ||
function missing_trailing_comma( | ||
$a, | ||
$b, | ||
) { | ||
|
||
} | ||
|
||
$foo = 1; | ||
$bar = 2; | ||
$x = function ( | ||
$a, | ||
$b, | ||
) use ( | ||
$foo, | ||
$bar | ||
) { | ||
|
||
}; | ||
|
||
/** | ||
* | ||
*/ | ||
class Test { | ||
|
||
/** | ||
* Test. | ||
*/ | ||
public function lookupSource( | ||
string $key, | ||
string $migrationNames = '', | ||
array $options = [ | ||
'all' => NULL, | ||
'group' => NULL, | ||
], | ||
): void { | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.