PHLAK
PHLAK

Reputation: 22527

Formatting an if statement for readability

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");
}

Upvotes: 3

Views: 737

Answers (8)

Neil Williams
Neil Williams

Reputation: 12578

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");
}

Upvotes: 17

eyelidlessness
eyelidlessness

Reputation: 63529

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");
}

Upvotes: 1

Rabbit
Rabbit

Reputation:

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.

Upvotes: 1

ConroyP
ConroyP

Reputation: 41936

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");
   }
}

Upvotes: 2

Rob Bell
Rob Bell

Reputation: 3695

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");

Upvotes: 2

John Millikin
John Millikin

Reputation: 200886

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);
}

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 994051

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.

Upvotes: 1

Fire Lancer
Fire Lancer

Reputation: 30145

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");
}

Upvotes: 4

Related Questions