akash-coded
2/20/2020 - 3:35 PM

Stop Misusing Boolean Literals in PHP

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;