Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding short arrays is not a unary operation #7

Merged
merged 2 commits into from
Feb 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,93 @@ class Barracuda_Sniffs_Formatting_SpaceUnaryOperatorSniff implements PHP_CodeSni
{


/**
* Returns an array of tokens this test wants to listen for.
*/
public function register()
{
return array(
T_DEC,
T_INC,
T_MINUS,
T_PLUS,
T_BOOLEAN_NOT,
);
/**
* Returns an array of tokens this test wants to listen for.
*/
public function register()
{
return array(
T_DEC,
T_INC,
T_MINUS,
T_PLUS,
T_BOOLEAN_NOT,
);

} // end register()
} // end register()


/**
* Processes this test, when one of its tokens is encountered.
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
/**
* Processes this test, when one of its tokens is encountered.
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

// Check decrement / increment.
// Check decrement / increment.
if ($tokens[$stackPtr]['code'] === T_DEC || $tokens[$stackPtr]['code'] === T_INC)
{
$modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$' ||
$tokens[($stackPtr + 1)]['content'] === ';';
$modifyLeft = substr($tokens[($stackPtr - 1)]['content'], 0, 1) === '$' ||
$tokens[($stackPtr + 1)]['content'] === ';';

if ($modifyLeft === true && $tokens[($stackPtr - 1)]['code'] === T_WHITESPACE)
{
$error = 'There must not be a single space before a unary operator statement';
$phpcsFile->addError($error, $stackPtr, 'IncDecLeft');
return;
}
$error = 'There must not be a single space before a unary operator statement';
$phpcsFile->addError($error, $stackPtr, 'IncDecLeft');
return;
}

if ($modifyLeft === false && !in_array(substr($tokens[($stackPtr + 1)]['content'], 0, 1), array('$', ',')))
{
$error = 'A unary operator statement must not be followed by a single space';
$phpcsFile->addError($error, $stackPtr, 'IncDecRight');
return;
}
}
$error = 'A unary operator statement must not be followed by a single space';
$phpcsFile->addError($error, $stackPtr, 'IncDecRight');
return;
}
}

// Check "!" operator.
// Check "!" operator.
if ($tokens[$stackPtr]['code'] === T_BOOLEAN_NOT && $tokens[$stackPtr + 1]['code'] === T_WHITESPACE)
{
$error = 'A unary operator statement must not be followed by a space';
$phpcsFile->addError($error, $stackPtr, 'BooleanNot');
return;
}
$error = 'A unary operator statement must not be followed by a space';
$phpcsFile->addError($error, $stackPtr, 'BooleanNot');
return;
}

// Find the last syntax item to determine if this is an unary operator.
$lastSyntaxItem = $phpcsFile->findPrevious(
array(T_WHITESPACE),
$stackPtr - 1,
($tokens[$stackPtr]['column']) * -1,
true,
null,
true
);
$operatorSuffixAllowed = in_array(
$tokens[$lastSyntaxItem]['code'],
array(
T_LNUMBER,
T_DNUMBER,
T_CLOSE_PARENTHESIS,
T_CLOSE_CURLY_BRACKET,
T_CLOSE_SQUARE_BRACKET,
T_VARIABLE,
T_STRING,
)
);
// Find the last syntax item to determine if this is an unary operator.
$lastSyntaxItem = $phpcsFile->findPrevious(
array(T_WHITESPACE),
$stackPtr - 1,
($tokens[$stackPtr]['column']) * -1,
true,
null,
true
);
$operatorSuffixAllowed = in_array(
$tokens[$lastSyntaxItem]['code'],
array(
T_LNUMBER,
T_DNUMBER,
T_CLOSE_PARENTHESIS,
T_CLOSE_CURLY_BRACKET,
T_CLOSE_SQUARE_BRACKET,
T_CLOSE_SHORT_ARRAY,
T_VARIABLE,
T_STRING,
)
);

// Check plus / minus value assignments or comparisons.
// Check plus / minus value assignments or comparisons.
if ($tokens[$stackPtr]['code'] === T_MINUS || $tokens[$stackPtr]['code'] === T_PLUS)
{
if ($operatorSuffixAllowed === false
&& $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
if ($operatorSuffixAllowed === false
&& $tokens[($stackPtr + 1)]['code'] === T_WHITESPACE
)
{
$error = 'A unary operator statement must not be followed by a space';
$phpcsFile->addError($error, $stackPtr);
}
}
$error = 'A unary operator statement must not be followed by a space';
$phpcsFile->addError($error, $stackPtr);
}
}

} // end process()
} // end process()
// end class
}