Skip to content

Commit

Permalink
Avoid (hard-coded) use of sniff name within sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Jul 24, 2023
1 parent 33a4d33 commit 58512dc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 22 deletions.
76 changes: 54 additions & 22 deletions src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,29 +378,14 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
$adjustment = ($functionIndent - $foundFunctionIndent);

// This rule doesn't apply to PSR2, but this sniff is used there. We can
// silence the complaint in the PSR2 ruleset.xml, but the unit tests
// don't seem to respect that. If there is a way to make the unit tests
// respect ruleset.xml, then we can remove the `strpos()` call here.
if ($foundFunctionIndent !== $functionIndent && strpos(get_class($this), '\\PSR2\\') === false) {
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
$data = [
if ($foundFunctionIndent !== $functionIndent) {
$this->complainOpenStatementWrongIndent(
$phpcsFile,
$first,
$tokens,
$functionIndent,
$foundFunctionIndent,
];

$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
if ($fix === true) {
$padding = str_repeat(' ', $functionIndent);
if ($foundFunctionIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
$newContent = $padding.ltrim($tokens[$first]['content']);
$phpcsFile->fixer->replaceToken($first, $newContent);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}
}
$foundFunctionIndent
);
}//end if

$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($openBracket + 1), null, true);
Expand Down Expand Up @@ -656,4 +641,51 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
}//end processMultiLineCall()


/**
* Add a complaint (and auto-fix) if the function indent is 'wrong'.
*
* @param File $phpcsFile The file being scanned.
* @param int $first Pointer to the first empty token on this line.
* @param array $tokens The stack of tokens that make up the file.
* @param int $functionIndent The expected indent for this function definition.
* @param int $foundFunctionIndent The actual indent for this function definition.
*
* @return void
*/
protected function complainOpenStatementWrongIndent(
$phpcsFile,
$first,
$tokens,
$functionIndent,
$foundFunctionIndent
) {
if ($foundFunctionIndent === $functionIndent) {
return;
}

$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
$data = [
$functionIndent,
$foundFunctionIndent,
];

$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
if ($fix !== true) {
return;
}

$padding = str_repeat(' ', $functionIndent);

if ($foundFunctionIndent === 0) {
$phpcsFile->fixer->addContentBefore($first, $padding);
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
$newContent = $padding.ltrim($tokens[$first]['content']);
$phpcsFile->fixer->replaceToken($first, $newContent);
} else {
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
}

}//end complainOpenStatementWrongIndent()


}//end class
22 changes: 22 additions & 0 deletions src/Standards/PSR2/Sniffs/Methods/FunctionCallSignatureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,26 @@ public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $token
}//end isMultiLineCall()


/**
* No-op; this rule does not apply to PSR-2.
*
* @param File $phpcsFile The file being scanned.
* @param int $first Pointer to the first empty token on this line.
* @param array $tokens The stack of tokens that make up the file.
* @param int $functionIndent The expected indent for this function definition.
* @param int $foundFunctionIndent The actual indent for this function definition.
*
* @return void
*/
protected function complainOpenStatementWrongIndent(
$phpcsFile,
$first,
$tokens,
$functionIndent,
$foundFunctionIndent
) {

}//end complainOpenStatementWrongIndent()


}//end class

0 comments on commit 58512dc

Please sign in to comment.