Skip to content
Open
Show file tree
Hide file tree
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
70 changes: 70 additions & 0 deletions HM/Sniffs/PHP/TernarySniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace HM\Sniffs\PHP;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

/**
* Sniff to check for ternary usage.
*/
class TernarySniff implements Sniff {
public function register() {
return array( T_INLINE_ELSE );
}

public function process( File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();

$inline_then_token = $phpcsFile->findPrevious( T_INLINE_THEN, $stackPtr );

$value_if_true_tokens = $this->get_nonempty_tokens( $tokens, $inline_then_token + 1, $stackPtr - 1 );
if ( count( $value_if_true_tokens ) !== 1 ) {
// No single value.
return;
}

if ( ! $this->is_boolean_token( $value_if_true_tokens[0] ) ) {
// No Boolean value.
return;
}

$ternary_end_token = $phpcsFile->findNext( array( T_CLOSE_CURLY_BRACKET, T_CLOSE_PARENTHESIS, T_COMMA, T_SEMICOLON ), $stackPtr + 1 );

$value_if_false_tokens = $this->get_nonempty_tokens( $tokens, $stackPtr + 1, $ternary_end_token - 1 );
if ( count( $value_if_false_tokens ) !== 1 ) {
// No single value.
return;
}

if ( ! $this->is_boolean_token( $value_if_false_tokens[0] ) ) {
// No Boolean value.
return;
}

$warning = 'Unnecessary ternary found: Instead of "$expr ? %s : %s", use "%s"';
$data = array(
$value_if_true_tokens[0]['content'],
$value_if_false_tokens[0]['content'],
$value_if_true_tokens[0]['content'] === 'true' ? '(bool) $expr' : '! $expr'
);
$phpcsFile->addWarning( $warning, $stackPtr, 'UnnecessaryTernary', $data );
}

private function get_nonempty_tokens( array $tokens, $start, $end ) {
$tokens = array_slice( $tokens, $start, $end - $start + 1 );

return array_values( array_filter(
$tokens,
array( $this, 'is_nonempty_token' )
) );
}

private function is_boolean_token( array $token ) {
return in_array( $token['code'], array( T_FALSE, T_TRUE ), true );
}

private function is_nonempty_token( array $token ) {
return ! in_array( $token['code'], Tokens::$emptyTokens, true );
}
}
1 change: 1 addition & 0 deletions tests/FixtureTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function setUp() {
'HM.Namespaces.NoLeadingSlashOnUse',
'HM.Performance.SlowMetaQuery',
'HM.Performance.SlowOrderBy',
'HM.PHP.Ternary',
'HM.Security.EscapeOutput',
'HM.Security.NonceVerification',
'HM.Security.ValidatedSanitizedInput',
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/fail/ternary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$result = $expr ? true : false;
$result = $expr ? false : true;
$result = ( $expr ? true : false );
$result = ( $expr ? false : true );

{
// Missing semicolon on purpose.
return $expr ? true : false
}

print( $expr ? true : false, 'foo' );
38 changes: 38 additions & 0 deletions tests/fixtures/fail/ternary.php.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"3": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
],
"4": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
],
"5": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
],
"6": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
],
"10": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
],
"13": [
{
"source": "HM.PHP.Ternary.UnnecessaryTernary",
"type": "warning"
}
]
}
14 changes: 14 additions & 0 deletions tests/fixtures/pass/ternary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

// Short ternaries.
$result = $expr ?: true;
$result = $expr ?: false;
$result = $expr ? : true;
$result = $expr ? : false;

// Only one Boolean values.
$result = $expr ? true : null;
$result = $expr ? '' : false;

// Nested ternaries. We don't want to do this, but the sniff should not flag this, at least.
$result = $expr ? true : $other ? false : null;