Skip to content

Instantly share code, notes, and snippets.

@akash-coded
Last active February 20, 2020 15:40
Show Gist options
  • Save akash-coded/ce21c29c86cddc3e592e289a72527c22 to your computer and use it in GitHub Desktop.
Save akash-coded/ce21c29c86cddc3e592e289a72527c22 to your computer and use it in GitHub Desktop.
Redundant Boolean literals should be removed from expressions to improve readability and remove code smells.
<?php
// Noncompliant Code Example
if ($booleanVariable == true) { /* ... */ }
if ($booleanVariable != true) { /* ... */ }
if ($booleanVariable || false) { /* ... */ }
doSomething(!false);
$booleanVariable = condition ? true : exp;
$booleanVariable = condition ? false : exp;
$booleanVariable = condition ? exp : true;
$booleanVariable = condition ? exp : false;
// Compliant Solution
if ($booleanVariable) { /* ... */ }
if (!$booleanVariable) { /* ... */ }
if ($booleanVariable) { /* ... */ }
doSomething(true);
$booleanVariable = condition || exp;
$booleanVariable = !condition && exp;
$booleanVariable = !condition || exp;
$booleanVariable = condition && exp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment