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:
Complex classes like ObjectToItem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ObjectToItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class ObjectToItem extends ObjectToArray implements ResponseTransformerInterface { |
||
13 | |||
14 | /** |
||
15 | * |
||
16 | * @var type |
||
17 | */ |
||
18 | protected $data = array(); |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | * @var type |
||
23 | */ |
||
24 | protected $item = array(); |
||
25 | |||
26 | /** |
||
27 | * |
||
28 | * @param type $response |
||
29 | * @return type |
||
30 | */ |
||
31 | public function transform($response) |
||
32 | { |
||
33 | $response = parent::transform($response); |
||
34 | |||
35 | if( !$this->get_item( $response ) ) |
||
36 | { |
||
37 | return array(); |
||
|
|||
38 | } |
||
39 | |||
40 | $this->set( 'asin', 'ASIN' ); |
||
41 | $this->set( 'title', 'ItemAttributes', 'Title' ); |
||
42 | $this->set( 'manufacturer', 'ItemAttributes', 'Manufacturer' ); |
||
43 | $this->set( 'isbn', 'ItemAttributes', 'ISBN' ); |
||
44 | $this->set( 'publisher', 'ItemAttributes', 'Publisher' ); |
||
45 | $this->set( 'number_of_pages', 'ItemAttributes', 'NumberOfPages' ); |
||
46 | $this->set( 'number_of_items', 'ItemAttributes', 'NumberOfItems' ); |
||
47 | $this->set( 'number_of_issues', 'ItemAttributes', 'NumberOfIssues' ); |
||
48 | $this->set( 'model', 'ItemAttributes', 'Model' ); |
||
49 | $this->set( 'label', 'ItemAttributes', 'Label' ); |
||
50 | $this->set( 'format', 'ItemAttributes', 'Format' ); |
||
51 | $this->set( 'edition', 'ItemAttributes', 'Edition' ); |
||
52 | $this->set( 'artist', 'ItemAttributes', 'Artist' ); |
||
53 | $this->set( 'description', 'EditorialReviews', 'EditorialReview', 'Content' ); |
||
54 | $this->set( 'lowest_new_price', 'OfferSummary', 'LowestNewPrice', 'Amount' ); |
||
55 | $this->set( 'large_image', 'LargeImage', 'URL' ); |
||
56 | $this->set( 'medium_image', 'MediumImage', 'URL' ); |
||
57 | $this->set( 'small_image', 'SmallImage', 'URL' ); |
||
58 | $this->set_array( 'author', 'ItemAttributes', 'Author' ); |
||
59 | $this->set_array( 'features', 'ItemAttributes', 'Feature' ); |
||
60 | |||
61 | $this->get_reviews(); |
||
62 | $this->get_price(); |
||
63 | $this->get_description(); |
||
64 | $this->get_category(); |
||
65 | $this->get_image_sets(); |
||
66 | |||
67 | return $this->data; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * @param type $response |
||
73 | * @return mixed |
||
74 | */ |
||
75 | protected function get_item($response) |
||
76 | { |
||
77 | if( isset( $response['Items']['Item'] ) AND is_array( $response['Items']['Item'] ) ) |
||
78 | { |
||
79 | if( array_key_exists( 1, $response['Items']['Item'] ) ) |
||
80 | { |
||
81 | return $this->item = $response['Items']['Item'][0]; |
||
82 | } |
||
83 | else |
||
84 | { |
||
85 | return $this->item = $response['Items']['Item']; |
||
86 | } |
||
87 | } |
||
88 | else |
||
89 | { |
||
90 | return FALSE; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | * @param type $data |
||
97 | * @param type $key1 |
||
98 | * @param type $key2 |
||
99 | * @param type $key3 |
||
100 | */ |
||
101 | protected function set($data, $key1, $key2 = NULL, $key3 = NULL, $key4 = NULL) |
||
132 | |||
133 | protected function set_array($data, $key1, $key2 = NULL, $key3 = NULL) |
||
141 | |||
142 | private function get_price() |
||
150 | |||
151 | private function get_description() |
||
166 | |||
167 | /** |
||
168 | * Parses the Amazon reviews iframe to get precise numeric review metrics |
||
169 | */ |
||
170 | private function get_reviews() |
||
218 | |||
219 | /** |
||
220 | * |
||
221 | */ |
||
222 | private function get_image_sets() |
||
245 | |||
246 | private function get_category() |
||
261 | |||
262 | View Code Duplication | private function get_ancestor($node) |
|
273 | |||
274 | } |
||
275 |
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.