Skip to content

Instantly share code, notes, and snippets.

@J7mbo
Created January 8, 2014 14:41
Show Gist options
  • Save J7mbo/8317804 to your computer and use it in GitHub Desktop.
Save J7mbo/8317804 to your computer and use it in GitHub Desktop.
Why no indented ifs?
/**
* {@inheritdoc}
*
* @note Image dimensions returned as array(width, height)
*/
public function fetchMetaDataFrom($filePath)
{
if (file_exists($filePath))
{
if ($metadata = @getimagesize($filePath) !== false)
{
list($width, $height, $type, /** Skip $attr **/) = $metadata;
$extension = $this->resolveImageType($type);
if (in_array($extension, $this->allowedImageTypes))
{
return array($width, $height);
}
else
{
throw new MetaDataImageExtensionNotAllowedException(sprintf("File %s wasn't in allowed image types: %s", $extension, print_r($this->allowedImageTypes, true)));
}
}
else
{
throw new MetaDataFileNotReadableException(sprintf("File %s metadata couldn't be read. Permissions problem?", $filePath));
}
}
else
{
throw new MetaDataFileNotFoundException(sprintf("File %s could not be found for metadata retrieval", $filePath));
}
}
@rdlowrey
Copy link

rdlowrey commented Jan 8, 2014

<?php
public function fetchMetaDataFrom($filepath)
{
    if (!file_exists($filepath)) {
        throw new MetaDataFileNotFoundException;
    }

    list($width, $height, /** Skip $attr **/) = $this->getImageMeta($filepath);

    return array($width, $height);
}

private function getImageMeta($filepath) {
    $metadata = @getimagesize($filePath);
    if ($metadata === FALSE) {
        throw new MetaDataFileNotReadableException;
    }

    $type = $metadata[2];
    // I would validate the extension here
    $this->validateImageType($type);

    return $metadata;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment