Formatting an if statement for readability
-
03-07-2019 - |
Question
What's the best way to format this for readability?
if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false || strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
Solution
I'd extract the "is an image" logic into its own function, which makes the if
more readable and also allows you to centralize the logic.
function is_image($filename) {
$image_extensions = array('png', 'gif', 'jpg');
foreach ($image_extensions as $extension)
if (strrpos($filename, ".$extension") !== FALSE)
return true;
return false;
}
if (is_image($file) && !file_exists("$thumbsdir/$file")) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
OTHER TIPS
if ((strpos($file, '.jpg',1) ||
strpos($file, '.gif',1) ||
strpos($file, '.png',1))
&& file_exists("$thumbsdir/$file") == false)
{
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
function check_thumbnail($file)
{
return (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false ||
strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false ||
strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false);
}
if (check_thumbnail ($file)) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
After extracting the logic to a separate function, you can reduce the duplication:
function check_thumbnail($file)
{
return (strpos($file, '.jpg',1) ||
strpos($file, '.gif',1) ||
strpos($file, '.png',1)) &&
(file_exists("$thumbsdir/$file") == false);
}
I would seperate the ifs as there is some repeating code in there. Also I try to exit a routine as early as possible:
if (!strpos($file, '.jpg',1) && !strpos($file, '.gif',1) && !strpos($file, '.png',1))
{
return;
}
if(file_exists("$thumbsdir/$file"))
{
return;
}
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
The file_exists
check seems to be constant for each of the file types, so don't compare them unless the file_exists
check has been passed.
if (file_exists("$thumbsdir/$file") == false)
{
if(strpos($file, '.jpg',1) ||
strpos($file, '.gif',1) ||
strpos($file, '.png',1)
{
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
}
I would break it up like this, setting aside the redundancy issue:
if (strpos($file, '.jpg',1) && file_exists("$thumbsdir/$file") == false
|| strpos($file, '.gif',1) && file_exists("$thumbsdir/$file") == false
|| strpos($file, '.png',1) && file_exists("$thumbsdir/$file") == false) {
createThumb("$gallerydir/$file", "$thumbsdir/$file",$thumbsize);
fwrite($log,date("Y-m-d")." @ ".date("H:i:s")." CREATED: $thumbsdir/$file\n");
}
@Fire Lancer's answer addresses the redundancy well.
I find the following to be more readable using getimagesize(). I'm writing this off the top of my head so it may require some debugging.
Vertical code is more readable than horizontal, imho.
// Extract image info if possible
// Note: Error suppression is for missing file or non-image
if (@$imageInfo = getimagesize("{$thumbsdir}/{$file}")) {
// Accept the following image types
$acceptTypes = array(
IMAGETYPE_JPEG,
IMAGETYPE_GIF,
IMAGETYPE_PNG,
);
// Proceed if image format is acceptable
if (in_array($imageInfo[2], $acceptTypes)) {
//createThumb(...);
//fwrite(...);
}
}
Peace + happy hacking.
Might as well throw my two cents in.
if(!file_exists($thumbsdir . '/' . $file) && preg_match('/\.(?:jpe?g|png|gif)$/', $file)) {
createThumb($gallerydir . '/' . $file, $thumbsdir . '/' . $file, $thumbsize);
fwrite($log, date('Y-m-d @ H:i:s') . ' CREATED: ' . $thumbsdir . '/' . $file . "\n");
}