Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed bug #19354 : Not recognizing references passed to method

  • Loading branch information...
commit 9b44f6d0411de4c7d53328f83f6eda11020dd439 1 parent 8f33675
@gsherwood gsherwood authored
View
14 CodeSniffer/File.php
@@ -2417,6 +2417,20 @@ public function isReference($stackPtr)
}
}
+ $tokenAfter = $this->findNext(
+ PHP_CodeSniffer_Tokens::$emptyTokens,
+ ($stackPtr + 1),
+ null,
+ true
+ );
+
+ if ($this->_tokens[$tokenAfter]['code'] === T_VARIABLE
+ && ($this->_tokens[$tokenBefore]['code'] === T_OPEN_PARENTHESIS
+ || $this->_tokens[$tokenBefore]['code'] === T_COMMA)
+ ) {
+ return true;
+ }
+
return false;
}//end isReference()
View
181 CodeSniffer/Standards/Squiz/Sniffs/WhiteSpace/OperatorSpacingSniff.php
@@ -94,115 +94,118 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
if ($tokens[$stackPtr]['code'] === T_BITWISE_AND) {
- // If its not a reference, then we expect one space either side of the
+ // If it's not a reference, then we expect one space either side of the
// bitwise operator.
- if ($phpcsFile->isReference($stackPtr) === false) {
- // Check there is one space before the & operator.
- if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
- $error = 'Expected 1 space before "&" operator; 0 found';
- $phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeAmp');
- } else {
- if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
- $found = strlen($tokens[($stackPtr - 1)]['content']);
- $error = 'Expected 1 space before "&" operator; %s found';
- $data = array($found);
- $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeAmp', $data);
- }
- }
+ if ($phpcsFile->isReference($stackPtr) === true) {
+ return;
+ }
- // Check there is one space after the & operator.
- if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
- $error = 'Expected 1 space after "&" operator; 0 found';
- $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterAmp');
- } else {
- if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
- $found = strlen($tokens[($stackPtr + 1)]['content']);
- $error = 'Expected 1 space after "&" operator; %s found';
- $data = array($found);
- $phpcsFile->addError($error, $stackPtr, 'SpacingAfterAmp', $data);
- }
- }
- }//end if
- } else {
- if ($tokens[$stackPtr]['code'] === T_MINUS) {
- // Check minus spacing, but make sure we aren't just assigning
- // a minus value or returning one.
- $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
- if ($tokens[$prev]['code'] === T_RETURN) {
- // Just returning a negative value; eg. return -1.
- return;
+ // Check there is one space before the & operator.
+ if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
+ $error = 'Expected 1 space before "&" operator; 0 found';
+ $phpcsFile->addError($error, $stackPtr, 'NoSpaceBeforeAmp');
+ } else {
+ if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
+ $found = strlen($tokens[($stackPtr - 1)]['content']);
+ $error = 'Expected 1 space before "&" operator; %s found';
+ $data = array($found);
+ $phpcsFile->addError($error, $stackPtr, 'SpacingBeforeAmp', $data);
}
+ }
- if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$operators) === true) {
- // Just trying to operate on a negative value; eg. ($var * -1).
- return;
+ // Check there is one space after the & operator.
+ if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
+ $error = 'Expected 1 space after "&" operator; 0 found';
+ $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfterAmp');
+ } else {
+ if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
+ $found = strlen($tokens[($stackPtr + 1)]['content']);
+ $error = 'Expected 1 space after "&" operator; %s found';
+ $data = array($found);
+ $phpcsFile->addError($error, $stackPtr, 'SpacingAfterAmp', $data);
}
+ }
- if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens) === true) {
- // Just trying to compare a negative value; eg. ($var === -1).
- return;
- }
+ return;
+ }//end if
- // A list of tokens that indicate that the token is not
- // part of an arithmetic operation.
- $invalidTokens = array(
- T_COMMA,
- T_OPEN_PARENTHESIS,
- T_OPEN_SQUARE_BRACKET,
- T_DOUBLE_ARROW,
- T_COLON,
- );
-
- if (in_array($tokens[$prev]['code'], $invalidTokens) === true) {
- // Just trying to use a negative value; eg. myFunction($var, -2).
- return;
- }
+ if ($tokens[$stackPtr]['code'] === T_MINUS) {
+ // Check minus spacing, but make sure we aren't just assigning
+ // a minus value or returning one.
+ $prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
+ if ($tokens[$prev]['code'] === T_RETURN) {
+ // Just returning a negative value; eg. return -1.
+ return;
+ }
- $number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
- if (in_array($tokens[$number]['code'], array(T_LNUMBER, T_VARIABLE)) === true) {
- $semi = $phpcsFile->findNext(T_WHITESPACE, ($number + 1), null, true);
- if ($tokens[$semi]['code'] === T_SEMICOLON) {
- if ($prev !== false && (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true)) {
- // This is a negative assignment.
- return;
- }
- }
- }
- }//end if
+ if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$operators) === true) {
+ // Just trying to operate on a negative value; eg. ($var * -1).
+ return;
+ }
- $operator = $tokens[$stackPtr]['content'];
+ if (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$comparisonTokens) === true) {
+ // Just trying to compare a negative value; eg. ($var === -1).
+ return;
+ }
- if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
- $error = "Expected 1 space before \"$operator\"; 0 found";
- $phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore');
- } else if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
- // Don't throw an error for assignments, because other standards allow
- // multiple spaces there to align multiple assignments.
- if (in_array($tokens[$stackPtr]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === false) {
- $found = strlen($tokens[($stackPtr - 1)]['content']);
- $error = 'Expected 1 space before "%s"; %s found';
- $data = array(
- $operator,
- $found,
+ // A list of tokens that indicate that the token is not
+ // part of an arithmetic operation.
+ $invalidTokens = array(
+ T_COMMA,
+ T_OPEN_PARENTHESIS,
+ T_OPEN_SQUARE_BRACKET,
+ T_DOUBLE_ARROW,
+ T_COLON,
);
- $phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
+
+ if (in_array($tokens[$prev]['code'], $invalidTokens) === true) {
+ // Just trying to use a negative value; eg. myFunction($var, -2).
+ return;
+ }
+
+ $number = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
+ if (in_array($tokens[$number]['code'], array(T_LNUMBER, T_VARIABLE)) === true) {
+ $semi = $phpcsFile->findNext(T_WHITESPACE, ($number + 1), null, true);
+ if ($tokens[$semi]['code'] === T_SEMICOLON) {
+ if ($prev !== false && (in_array($tokens[$prev]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true)) {
+ // This is a negative assignment.
+ return;
+ }
}
}
+ }//end if
- if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
- $error = "Expected 1 space after \"$operator\"; 0 found";
- $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfter');
- } else if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
- $found = strlen($tokens[($stackPtr + 1)]['content']);
- $error = 'Expected 1 space after "%s"; %s found';
+ $operator = $tokens[$stackPtr]['content'];
+
+ if ($tokens[($stackPtr - 1)]['code'] !== T_WHITESPACE) {
+ $error = "Expected 1 space before \"$operator\"; 0 found";
+ $phpcsFile->addError($error, $stackPtr, 'NoSpaceBefore');
+ } else if (strlen($tokens[($stackPtr - 1)]['content']) !== 1) {
+ // Don't throw an error for assignments, because other standards allow
+ // multiple spaces there to align multiple assignments.
+ if (in_array($tokens[$stackPtr]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === false) {
+ $found = strlen($tokens[($stackPtr - 1)]['content']);
+ $error = 'Expected 1 space before "%s"; %s found';
$data = array(
$operator,
$found,
);
- $phpcsFile->addError($error, $stackPtr, 'SpacingAfter', $data);
+ $phpcsFile->addError($error, $stackPtr, 'SpacingBefore', $data);
}
+ }
- }//end if
+ if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) {
+ $error = "Expected 1 space after \"$operator\"; 0 found";
+ $phpcsFile->addError($error, $stackPtr, 'NoSpaceAfter');
+ } else if (strlen($tokens[($stackPtr + 1)]['content']) !== 1) {
+ $found = strlen($tokens[($stackPtr + 1)]['content']);
+ $error = 'Expected 1 space after "%s"; %s found';
+ $data = array(
+ $operator,
+ $found,
+ );
+ $phpcsFile->addError($error, $stackPtr, 'SpacingAfter', $data);
+ }
}//end process()
View
2  CodeSniffer/Standards/Squiz/Tests/WhiteSpace/OperatorSpacingUnitTest.inc
@@ -98,6 +98,8 @@ if (index > -1) {
array_walk_recursive($array, function(&$item) use (&$something) {
});
+$var = saveFile(&$model, &$foo);
+
// This is all valid.
$boo = -$foo;
function foo($boo = -1) {}
View
1  package.xml
@@ -37,6 +37,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed an issue in AbstractPatternSniff where comments were not being ignored in some cases
- Fixed bug #19290 : Generic indent sniffer fails for anonymous functions
- Fixed bug #19324 : Setting show_warnings configuration option does not work
+ - Fixed bug #19354 : Not recognizing references passed to method
</notes>
<contents>
<dir name="/">
Please sign in to comment.
Something went wrong with that request. Please try again.