Skip to content

Instantly share code, notes, and snippets.

@adrian-green
Last active March 21, 2016 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrian-green/8db4375b159fa518602e to your computer and use it in GitHub Desktop.
Save adrian-green/8db4375b159fa518602e to your computer and use it in GitHub Desktop.
bad code
<?php
class Zimmermann_Travel_Block_Share_Image extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
$id = $this->getRequest()->getParam("id");
$type = $this->getRequest()->getParam("type");
/* $id and $type should be checked for before proceeding - in all cases $image will be null if $id is missing */
switch($type){
case"zplace" :
$image = Mage::getModel("photogallery/img")->load($id);
$galleryData = Mage::getModel("photogallery/photogallery")->load($image->getPhotogalleryId());
break;
case "zfeed":
$image = Mage::getModel("fishead_tumblr/tumblr")->load($id);
$galleryData = array(); /* Indentation inconsistent */
break;
case "explore":
$image = Mage::getModel("explore/image")->load($id);
$galleryData = Mage::getModel("explore/explore")->load($image->getExploreId()); /* Indentation inconsistent */
break;
case "zvideo":
$image = Mage::getModel("immerse/video")->load($id);
$galleryData = array();
$image->setType(3); /* What is "3"? Why not use a CONSTANT, like self::IMAGE_VIDEO. Indentation inconsistent */
break; /* There should be a default: case to handle bad input*/
}
$this->setImage($image); /* When was $image defined? It could be null here. */
$this->setShareType($type);
$this->setGalleryData($galleryData); /* When was $galleryData defined? It could be null here. Indentation inconsistent */
parent::_prepareLayout();
}
public function getTitle()
{
$type = $this->getRequest()->getParam("type");
switch($type){
case"zplace" :
return $this->getImage()->getImgLabel(); /* in all cases below getImage() could be null */
break; /* unreachable code */
case "zfeed":
return strip_tags($this->getImage()->getCaption());
break;/* unreachable code */
case "explore":
return $this->getImage()->getImgLabel();
break;/* unreachable code */
case "zvideo":
return $this->getImage()->getTitle();
break;/* unreachable code */
}
/* no returned default result */
}
public function getImageUrl()
{
$type = $this->getRequest()->getParam("type");
switch($type){
case"zplace" :
return $this->getImage()->getImagePath(); /* in all cases below getImage() could be null */
break;/* unreachable code */
case "zfeed":
$photos = json_decode($this->getImage()->photos);
return $photos[0]->original_size->url; /* $photos could be empty or null */
break;/* unreachable code */
case"explore" :
if($this->getImage()->getType() == 3){/* Magic number - use a CONSTANT */
return $this->getImage()->getImgName();
}/* Indentation inconsistent */
return $this->getImage()->getImagePath();
break;/* unreachable code */
case "zvideo":
return $this->getImage()->getThumbnail();
break;
} /*no default result returned */
}
public function getDescription()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment