1.2.1340. Use Named Boolean In Argument Definition¶
Boolean values in argument definition are confusing.
It is recommended to use explicit constant names or enumerations, instead. They are more readable. They also allow for easy replacement when the code evolve and has to replace those booleans by strings. This works even also with classes, and class constants.
<?php
function flipImage($im, $horizontal = NO_HORIZONTAL_FLIP, $vertical = NO_VERTICAL_FLIP) { }
// with constants
const HORIZONTAL_FLIP = true;
const NO_HORIZONTAL_FLIP = true;
const VERTICAL_FLIP = true;
const NO_VERTICAL_FLIP = true;
rotateImage($im, HORIZONTAL_FLIP, NO_VERTICAL_FLIP);
// without constants
function flipImage($im, $horizontal = false, $vertical = false) { }
rotateImage($im, true, false);
?>
See also Improve Passing Booleans in PHP, Flag Argument and Improve Passing Booleans in PHP.
1.2.1340.1. Connex PHP features¶
1.2.1340.1.1. Suggestions¶
Use available constants, global or class
Create a constant, global or class, and use it
Use named parameters to clarify the target of the boolean
Use a single-parameter method, so that the value of the boolean is obvious
Use an enumeration
1.2.1340.1.2. Specs¶
Short name |
Functions/AvoidBooleanArgument |
Rulesets |
|
Exakat since |
1.0.6 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
High |
Examples |
|
Available in |