Skip to content

Commit

Permalink
Update Standards\Squizz\Sniffs\Functions\FunctionDeclarationArgumentS…
Browse files Browse the repository at this point in the history
…pacingSniff

Enable the amount of whitespace between the reference operator and the argument name in function declarations to be defined by a property in XML config.
  • Loading branch information
Darren Edale committed Oct 14, 2023
1 parent 8dfb632 commit b6bf6b1
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class FunctionDeclarationArgumentSpacingSniff implements Sniff
*/
public $requiredSpacesBeforeClose = 0;

/**
* How many spaces should follow the reference operator in by-reference parameter declarations.
*
* @var integer
*/
public $requiredSpacesAfterReferenceOperator = 0;


/**
* Returns an array of tokens this test wants to listen for.
Expand Down Expand Up @@ -77,7 +84,8 @@ public function process(File $phpcsFile, $stackPtr)

$this->equalsSpacing = (int) $this->equalsSpacing;
$this->requiredSpacesAfterOpen = (int) $this->requiredSpacesAfterOpen;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$this->requiredSpacesBeforeClose = (int) $this->requiredSpacesBeforeClose;
$this->requiredSpacesAfterReferenceOperator = (int) $this->requiredSpacesAfterReferenceOperator;

$this->processBracket($phpcsFile, $tokens[$stackPtr]['parenthesis_opener']);

Expand Down Expand Up @@ -148,15 +156,21 @@ public function processBracket($phpcsFile, $openBracket)
$gap = $tokens[($refToken + 1)]['length'];
}

if ($gap !== 0) {
$error = 'Expected 0 spaces after reference operator for argument "%s"; %s found';
if ($gap !== $this->requiredSpacesAfterReferenceOperator) {
$error = 'Expected %s spaces after reference operator for argument "%s"; %s found';
$data = [
$this->requiredSpacesAfterReferenceOperator,
$param['name'],
$gap,
];
$fix = $phpcsFile->addFixableError($error, $refToken, 'SpacingAfterReference', $data);
if ($fix === true) {
$phpcsFile->fixer->replaceToken(($refToken + 1), '');
$padding = str_repeat(' ', $this->requiredSpacesAfterReferenceOperator);
if ($gap === 0) {
$phpcsFile->fixer->addContentBefore(($refToken + 1), $padding);
} else {
$phpcsFile->fixer->replaceToken(($refToken + 1), $padding);
}
}
}
}//end if
Expand Down

0 comments on commit b6bf6b1

Please sign in to comment.