-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
New unneeded ternary sniff #1771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<documentation title="Discourage the usage of the unneeded ternary operator"> | ||
<standard> | ||
<![CDATA[ | ||
When storing a boolean value to a variable, a ternary check is not needed. | ||
]]> | ||
</standard> | ||
<code_comparison> | ||
<code title="Valid: simple test in the assignment."> | ||
<![CDATA[ | ||
$true_variable = $var === 42; | ||
]]> | ||
</code> | ||
<code title="Invalid: ternary usage with boolean in the assignment."> | ||
<![CDATA[ | ||
$true_variable = $var === 42 ? <em>true</em> : <em>false</em>; | ||
]]> | ||
</code> | ||
</code_comparison> | ||
</documentation> |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,77 @@ | ||||||||||||
<?php | ||||||||||||
/** | ||||||||||||
* WordPress Coding Standard. | ||||||||||||
* | ||||||||||||
* @package WPCS\WordPressCodingStandards | ||||||||||||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||||||||||||
* @license https://opensource.org/licenses/MIT MIT | ||||||||||||
*/ | ||||||||||||
|
||||||||||||
namespace WordPressCS\WordPress\Sniffs\PHP; | ||||||||||||
|
||||||||||||
use WordPressCS\WordPress\Sniff; | ||||||||||||
use PHP_CodeSniffer\Util\Tokens; | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Discourage the usage of the unneeded ternary operator. | ||||||||||||
* | ||||||||||||
* This sniff is influenced by the same rule set in the ESLint rules. | ||||||||||||
* When we want to store a boolean value in a variable, using a ternary operator | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this sniff also cover There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the point of the sniff. Wherever there is an unneeded ternary detected, it can be removed. This includes conditionals, return statements or assignment to a variable 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And yet, there are no unit tests which shows that return statements are covered by this sniff. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will add those when I update the sniff 👍 |
||||||||||||
* is not needed, because a test will return a boolean value. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll rephrase this description so that it's not only limited to variable assignments (as is the initial intention of the sniff) 👍 |
||||||||||||
* | ||||||||||||
* @link https://eslint.org/docs/rules/no-unneeded-ternary | ||||||||||||
* | ||||||||||||
* @package WPCS\WordPressCodingStandards | ||||||||||||
* | ||||||||||||
* @since 2.2.0 | ||||||||||||
*/ | ||||||||||||
class NoUnneededTernarySniff extends Sniff { | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Boolean tokens array. | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
* | ||||||||||||
* @since 2.2.0 | ||||||||||||
* | ||||||||||||
* @var array | ||||||||||||
*/ | ||||||||||||
private $boolean_tokens = array( | ||||||||||||
'T_TRUE' => true, | ||||||||||||
'T_FALSE' => true, | ||||||||||||
); | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns an array of tokens this test wants to listen for. | ||||||||||||
* | ||||||||||||
* @since 2.2.0 | ||||||||||||
* | ||||||||||||
* @return array | ||||||||||||
*/ | ||||||||||||
public function register() { | ||||||||||||
return array( \T_INLINE_THEN ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Processes this test, when one of its tokens is encountered. | ||||||||||||
* | ||||||||||||
* @since 2.2.0 | ||||||||||||
* | ||||||||||||
* @param int $stackPtr The position of the current token in the stack. | ||||||||||||
*/ | ||||||||||||
public function process_token( $stackPtr ) { | ||||||||||||
$first_token = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true ); | ||||||||||||
$end_of_statement = $this->phpcsFile->findNext( array( \T_SEMICOLON, \T_CLOSE_TAG ), $stackPtr ); | ||||||||||||
$last_token = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $end_of_statement - 1 ), null, true ); | ||||||||||||
|
||||||||||||
$first_token_type = $this->tokens[ $first_token ]['type']; | ||||||||||||
$last_token_type = $this->tokens[ $last_token ]['type']; | ||||||||||||
|
||||||||||||
if ( isset( $this->boolean_tokens[ $first_token_type ] ) && isset( $this->boolean_tokens[ $last_token_type ] ) ) { | ||||||||||||
$this->phpcsFile->addError( | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this warrants a hard error which breaks build. A soft warning should be enough.
Suggested change
|
||||||||||||
'Don\'t use ternary epression to store a boolean value in a variable.', | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still not happy with this phrase as IMO it's still unclear.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ESLint will throw |
||||||||||||
$stackPtr, | ||||||||||||
'Detected' | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default error code is
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't aware of the |
||||||||||||
); | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
$var = 'a'; | ||
$trueVariable = $var === 'a' ? true : false; // Bad. | ||
// Can be written as. | ||
$trueVariable = $var === 'a'; // Ok. | ||
|
||
$test_var = $boolean ? true : false; // Bad. | ||
|
||
$test = $var === 'a' ? // Bad. | ||
true : | ||
false; | ||
|
||
$test = $var === 'a' | ||
? true // Bad. | ||
: false; | ||
|
||
$isTrue = $var === 4 ? false : true; // Bad. | ||
|
||
$isTrue = $var === 4 ? 'Yes' : 'No'; // Ok. | ||
|
||
$isTrue = $var === 4 ? true : 'No'; // Ok. | ||
|
||
$isTrue = $var === 4 ? 'True' : false; // Ok. | ||
|
||
$isTrue = $var === 4 ? false : 'True'; // Ok. | ||
|
||
$isTrue = $var === 4 ? 'False' : true; // Ok. | ||
|
||
$notFalse = $var !== false; // Ok. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* Unit test class for WordPress Coding Standard. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* @link https://github.com/WordPress/WordPress-Coding-Standards | ||
* @license https://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
namespace WordPressCS\WordPress\Tests\PHP; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
/** | ||
* Unit test class for the PHP_NoUnneededTernary sniff. | ||
* | ||
* @package WPCS\WordPressCodingStandards | ||
* | ||
* @since 2.2.0 | ||
*/ | ||
class NoUnneededTernaryUnitTest extends AbstractSniffUnitTest { | ||
|
||
/** | ||
* Returns the lines where errors should occur. | ||
* | ||
* @return array <int line number> => <int number of errors> | ||
*/ | ||
public function getErrorList() { | ||
return array( | ||
4 => 1, | ||
8 => 1, | ||
10 => 1, | ||
15 => 1, | ||
18 => 1, | ||
); | ||
} | ||
|
||
/** | ||
* Returns the lines where warnings should occur. | ||
* | ||
* @return array <int line number> => <int number of warnings> | ||
*/ | ||
public function getWarningList() { | ||
return array(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just be a title, not a description.