Skip to content

Commit

Permalink
Allow assignment in ternary operation condition
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Dec 9, 2010
1 parent c41a54c commit 3e3c962
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Expand Up @@ -64,13 +64,16 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
T_PLUS_EQUAL,
T_SL_EQUAL,
T_SR_EQUAL,
T_XOR_EQUAL
T_XOR_EQUAL,

// To detect end of previous statement
T_SEMICOLON,
);

$startPtr = $phpcsFile->findPrevious($allowed, $stackPtr - 1, NULL, FALSE, NULL, TRUE);
$startPtr = $this->_find_previous($allowed, $tokens, $stackPtr - 1, 0, NULL);
$endPtr = $phpcsFile->findNext(array(T_CLOSE_TAG, T_SEMICOLON), $stackPtr + 1);

if ($startPtr === FALSE)
if ($tokens[$startPtr]['code'] === T_SEMICOLON)
{
$error = 'Ternary operation must occur within assignment, echo or return statement';
$phpcsFile->addError($error, $stackPtr, 'TernaryStart');
Expand Down
Expand Up @@ -12,6 +12,7 @@ $foo = $bar ? (string) $foobar : $bar;
$foo = $bar ? ($baz ? $baz : FALSE) : $foobar;
$foo = $bar ? array() : $baz;
$foo = $bar ? new MyClass : FALSE;
$foo = ($bar = NULL) ? $foo : $bar;
$foo = new DateTimeZone($bar ? $bar : date_default_timezone_get());
$foo = new DateTime($bar, new DateTimeZone($baz ? $baz : date_default_timezone_get()));
$foo = $bar['a'][0] ? $baz->foo : FALSE;
Expand Down

0 comments on commit 3e3c962

Please sign in to comment.