Skip to content

Commit 58512dc

Browse files
committed
Avoid (hard-coded) use of sniff name within sniff
1 parent 33a4d33 commit 58512dc

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -378,29 +378,14 @@ public function processMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $
378378
$functionIndent = (int) (floor($foundFunctionIndent / $this->indent) * $this->indent);
379379
$adjustment = ($functionIndent - $foundFunctionIndent);
380380

381-
// This rule doesn't apply to PSR2, but this sniff is used there. We can
382-
// silence the complaint in the PSR2 ruleset.xml, but the unit tests
383-
// don't seem to respect that. If there is a way to make the unit tests
384-
// respect ruleset.xml, then we can remove the `strpos()` call here.
385-
if ($foundFunctionIndent !== $functionIndent && strpos(get_class($this), '\\PSR2\\') === false) {
386-
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
387-
$data = [
381+
if ($foundFunctionIndent !== $functionIndent) {
382+
$this->complainOpenStatementWrongIndent(
383+
$phpcsFile,
384+
$first,
385+
$tokens,
388386
$functionIndent,
389-
$foundFunctionIndent,
390-
];
391-
392-
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
393-
if ($fix === true) {
394-
$padding = str_repeat(' ', $functionIndent);
395-
if ($foundFunctionIndent === 0) {
396-
$phpcsFile->fixer->addContentBefore($first, $padding);
397-
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
398-
$newContent = $padding.ltrim($tokens[$first]['content']);
399-
$phpcsFile->fixer->replaceToken($first, $newContent);
400-
} else {
401-
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
402-
}
403-
}
387+
$foundFunctionIndent
388+
);
404389
}//end if
405390

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

658643

644+
/**
645+
* Add a complaint (and auto-fix) if the function indent is 'wrong'.
646+
*
647+
* @param File $phpcsFile The file being scanned.
648+
* @param int $first Pointer to the first empty token on this line.
649+
* @param array $tokens The stack of tokens that make up the file.
650+
* @param int $functionIndent The expected indent for this function definition.
651+
* @param int $foundFunctionIndent The actual indent for this function definition.
652+
*
653+
* @return void
654+
*/
655+
protected function complainOpenStatementWrongIndent(
656+
$phpcsFile,
657+
$first,
658+
$tokens,
659+
$functionIndent,
660+
$foundFunctionIndent
661+
) {
662+
if ($foundFunctionIndent === $functionIndent) {
663+
return;
664+
}
665+
666+
$error = 'Opening statement of multi-line function call not indented correctly; expected %s spaces but found %s';
667+
$data = [
668+
$functionIndent,
669+
$foundFunctionIndent,
670+
];
671+
672+
$fix = $phpcsFile->addFixableError($error, $first, 'OpeningIndent', $data);
673+
if ($fix !== true) {
674+
return;
675+
}
676+
677+
$padding = str_repeat(' ', $functionIndent);
678+
679+
if ($foundFunctionIndent === 0) {
680+
$phpcsFile->fixer->addContentBefore($first, $padding);
681+
} else if ($tokens[$first]['code'] === T_INLINE_HTML) {
682+
$newContent = $padding.ltrim($tokens[$first]['content']);
683+
$phpcsFile->fixer->replaceToken($first, $newContent);
684+
} else {
685+
$phpcsFile->fixer->replaceToken(($first - 1), $padding);
686+
}
687+
688+
}//end complainOpenStatementWrongIndent()
689+
690+
659691
}//end class

src/Standards/PSR2/Sniffs/Methods/FunctionCallSignatureSniff.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,26 @@ public function isMultiLineCall(File $phpcsFile, $stackPtr, $openBracket, $token
7676
}//end isMultiLineCall()
7777

7878

79+
/**
80+
* No-op; this rule does not apply to PSR-2.
81+
*
82+
* @param File $phpcsFile The file being scanned.
83+
* @param int $first Pointer to the first empty token on this line.
84+
* @param array $tokens The stack of tokens that make up the file.
85+
* @param int $functionIndent The expected indent for this function definition.
86+
* @param int $foundFunctionIndent The actual indent for this function definition.
87+
*
88+
* @return void
89+
*/
90+
protected function complainOpenStatementWrongIndent(
91+
$phpcsFile,
92+
$first,
93+
$tokens,
94+
$functionIndent,
95+
$foundFunctionIndent
96+
) {
97+
98+
}//end complainOpenStatementWrongIndent()
99+
100+
79101
}//end class

0 commit comments

Comments
 (0)