Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | class ObjectToItems extends ObjectToArray implements ResponseTransformerInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * |
||
| 14 | * @var type |
||
| 15 | */ |
||
| 16 | protected $data = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * |
||
| 20 | * @var type |
||
| 21 | */ |
||
| 22 | protected $items = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * @param type $response |
||
| 27 | * @return type |
||
| 28 | */ |
||
| 29 | public function transform($response) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * |
||
| 75 | * @param type $response |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | View Code Duplication | protected function get_items($response) |
|
| 79 | { |
||
| 80 | if( isset($response['Items']['Item']) AND is_array($response['Items']['Item']) ) |
||
| 81 | { |
||
| 82 | return $this->items = $response['Items']['Item']; |
||
| 83 | } |
||
| 84 | else |
||
| 85 | { |
||
| 86 | return FALSE; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * |
||
| 93 | * @param type $data |
||
| 94 | * @param type $key1 |
||
| 95 | * @param type $key2 |
||
| 96 | * @param type $key3 |
||
| 97 | */ |
||
| 98 | protected function set($i, $data, $key1, $key2=NULL, $key3=NULL) |
||
| 99 | { |
||
| 100 | if($key3) |
||
| 101 | { |
||
| 102 | if( isset($this->items[$i][$key1][$key2][$key3]) ) |
||
| 103 | { |
||
| 104 | $this->data[$i][$data] = $this->items[$i][$key1][$key2][$key3]; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | elseif($key2) |
||
| 108 | { |
||
| 109 | if( isset($this->items[$i][$key1][$key2]) ) |
||
| 110 | { |
||
| 111 | $this->data[$i][$data] = $this->items[$i][$key1][$key2]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | else |
||
| 115 | { |
||
| 116 | if( isset($this->items[$i][$key1]) ) |
||
| 117 | { |
||
| 118 | $this->data[$i][$data] = $this->items[$i][$key1]; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | protected function set_array($i, $data, $key1, $key2=NULL, $key3=NULL) |
||
| 131 | |||
| 132 | private function get_price($i) |
||
| 133 | { |
||
| 134 | $list_price = isset( $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] ) ? $this->items[$i]['ItemAttributes']['ListPrice']['Amount'] : NULL; |
||
| 135 | $amazon_price = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['Price']['Amount'] : NULL; |
||
| 136 | $saved = isset( $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved'] ) ? $this->items[$i]['Offers']['Offer']['OfferListing']['AmountSaved']['Amount'] : NULL; |
||
| 137 | $price = ($list_price) ? $list_price : ($amazon_price ? ($amazon_price + $saved) : NULL ); |
||
| 138 | $this->data[$i]['price'] = ($price) ? $price : (isset($this->data[$i]['lowest_new_price']) ? $this->data[$i]['lowest_new_price'] : NULL); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @param type $i |
||
| 144 | */ |
||
| 145 | private function get_description($i) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * |
||
| 156 | */ |
||
| 157 | private function get_image_sets($i) |
||
| 181 | |||
| 182 | private function get_category($i) |
||
| 198 | |||
| 199 | View Code Duplication | private function get_ancestor($node) |
|
| 210 | } |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.