Skip to content

Commit

Permalink
PEAR FunctionCallSignatureSniff (and the Squiz and PSR2 sniffs that u…
Browse files Browse the repository at this point in the history
…se it) now correctly check the first argument (ref bug #698)
  • Loading branch information
gsherwood committed Feb 5, 2016
1 parent f33834c commit fae43ae
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,21 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,
}

// Each line between the parenthesis should be indented n spaces.
$lastLine = $tokens[$openBracket]['line'];
$lastLine = ($tokens[$openBracket]['line'] - 1);
$argStart = null;
$argEnd = null;
$inArg = false;
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {

// Start processing at the first argument.
$i = $phpcsFile->findNext(T_WHITESPACE, ($openBracket + 1), null, true);
if ($tokens[($i - 1)]['code'] === T_WHITESPACE
&& $tokens[($i - 1)]['line'] === $tokens[$i]['line']
) {
// Make sure we check the indent.
$i--;
}

for ($i; $i < $closeBracket; $i++) {
if ($i > $argStart && $i < $argEnd) {
$inArg = true;
} else {
Expand All @@ -384,76 +394,80 @@ public function processMultiLineCall(PHP_CodeSniffer_File $phpcsFile, $stackPtr,
continue;
}

// We changed lines, so this should be a whitespace indent token, but first make
// sure it isn't a blank line because we don't need to check indent unless there
// is actually some code to indent.
if ($tokens[$i]['code'] === T_WHITESPACE) {
$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
if ($tokens[$nextCode]['line'] !== $lastLine) {
if ($inArg === false) {
$error = 'Empty lines are not allowed in multi-line function calls';
$fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($i, '');
if ($tokens[$i]['line'] !== $tokens[$openBracket]['line']) {
// We changed lines, so this should be a whitespace indent token, but first make
// sure it isn't a blank line because we don't need to check indent unless there
// is actually some code to indent.
if ($tokens[$i]['code'] === T_WHITESPACE) {
$nextCode = $phpcsFile->findNext(T_WHITESPACE, ($i + 1), ($closeBracket + 1), true);
if ($tokens[$nextCode]['line'] !== $lastLine) {
if ($inArg === false) {
$error = 'Empty lines are not allowed in multi-line function calls';
$fix = $phpcsFile->addFixableError($error, $i, 'EmptyLine');
if ($fix === true) {
$phpcsFile->fixer->replaceToken($i, '');
}
}
}

continue;
continue;
}
} else {
$nextCode = $i;
}
} else {
$nextCode = $i;
}

if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
// Closing brace needs to be indented to the same level
// as the function call.
$inArg = false;
$expectedIndent = $functionIndent;
} else {
$expectedIndent = ($functionIndent + $this->indent);
}
if ($tokens[$nextCode]['line'] === $tokens[$closeBracket]['line']) {
// Closing brace needs to be indented to the same level
// as the function call.
$inArg = false;
$expectedIndent = $functionIndent;
} else {
$expectedIndent = ($functionIndent + $this->indent);
}

if ($tokens[$i]['code'] !== T_WHITESPACE
&& $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE
) {
// Just check if it is a multi-line block comment. If so, we can
// calculate the indent from the whitespace before the content.
if ($tokens[$i]['code'] === T_COMMENT
&& $tokens[($i - 1)]['code'] === T_COMMENT
if ($tokens[$i]['code'] !== T_WHITESPACE
&& $tokens[$i]['code'] !== T_DOC_COMMENT_WHITESPACE
) {
$trimmed = ltrim($tokens[$i]['content']);
$foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
// Just check if it is a multi-line block comment. If so, we can
// calculate the indent from the whitespace before the content.
if ($tokens[$i]['code'] === T_COMMENT
&& $tokens[($i - 1)]['code'] === T_COMMENT
) {
$trimmed = ltrim($tokens[$i]['content']);
$foundIndent = (strlen($tokens[$i]['content']) - strlen($trimmed));
} else {
$foundIndent = 0;
}
} else {
$foundIndent = 0;
$foundIndent = strlen($tokens[$i]['content']);
}
} else {
$foundIndent = strlen($tokens[$i]['content']);
}

if ($foundIndent < $expectedIndent
|| ($inArg === false
&& $expectedIndent !== $foundIndent)
) {
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
$data = array(
$expectedIndent,
$foundIndent,
);

$fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
if ($fix === true) {
$padding = str_repeat(' ', $expectedIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($i, $padding);
} else {
if ($tokens[$i]['code'] === T_COMMENT) {
$comment = $padding.ltrim($tokens[$i]['content']);
$phpcsFile->fixer->replaceToken($i, $comment);
if ($foundIndent < $expectedIndent
|| ($inArg === false
&& $expectedIndent !== $foundIndent)
) {
$error = 'Multi-line function call not indented correctly; expected %s spaces but found %s';
$data = array(
$expectedIndent,
$foundIndent,
);

$fix = $phpcsFile->addFixableError($error, $i, 'Indent', $data);
if ($fix === true) {
$padding = str_repeat(' ', $expectedIndent);
if ($foundIndent === 0) {
$phpcsFile->fixer->addContentBefore($i, $padding);
} else {
$phpcsFile->fixer->replaceToken($i, $padding);
if ($tokens[$i]['code'] === T_COMMENT) {
$comment = $padding.ltrim($tokens[$i]['content']);
$phpcsFile->fixer->replaceToken($i, $comment);
} else {
$phpcsFile->fixer->replaceToken($i, $padding);
}
}
}
}
}//end if
} else {
$nextCode = $i;
}//end if

if ($inArg === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,10 @@ array_filter(
return $i === 0;
}
);

foo(array(
'callback' => function () {
$foo = 'foo';
return;
},
));
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,12 @@ array_filter(
return $i === 0;
}
);

foo(
array(
'callback' => function () {
$foo = 'foo';
return;
},
)
);
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ public function getErrorList($testFile='FunctionCallSignatureUnitTest.inc')
215 => 2,
274 => 1,
275 => 1,
300 => 1,
305 => 1,
);

}//end getErrorList()
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Squiz CSS IndentationSniff no longer assumes the class opening brace is at the end of a line
- Squiz FunctionCommentThrowTagSniff now ignores non-docblock comments
- Squiz ComparisonOperatorUsageSniff now allows conditions like while(true)
- PEAR FunctionCallSignatureSniff (and the Squiz and PSR2 sniffs that use it) now correctly check the first argument
-- Further fix for bug #698
- Fixed bug #872 : Incorrect detection of blank lines between CSS class names
- Fixed bug #879 : Generic InlineControlStructureSniff can create parse error when case/if/elseif/else have mixed brace and braceless definitions
- Fixed bug #883 : PSR2 is not checking for blank lines at the start and end of control structures
Expand Down

0 comments on commit fae43ae

Please sign in to comment.