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 |
||
14 | class ExtraParams implements ArrayAccess, Arrayable, Jsonable, JsonSerializable |
||
15 | { |
||
16 | /** |
||
17 | * @var array|stdClass|string |
||
18 | */ |
||
19 | protected $extraParams; |
||
20 | |||
21 | /** |
||
22 | * Jsonable constructor. |
||
23 | * |
||
24 | * @param $extraParams |
||
25 | */ |
||
26 | public function __construct($extraParams) |
||
34 | |||
35 | /** |
||
36 | * Convert the model instance to JSON. |
||
37 | * |
||
38 | * @param int $options |
||
39 | * @return string |
||
40 | */ |
||
41 | public function toJson($options = 0) |
||
45 | |||
46 | /** |
||
47 | * Convert the object into something JSON serializable. |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | public function jsonSerialize() |
||
55 | |||
56 | /** |
||
57 | * @return array |
||
58 | */ |
||
59 | public function toArray() |
||
68 | |||
69 | /** |
||
70 | * The __toString method allows a class to decide how it will react when it is converted to a string. |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function __toString() |
||
84 | |||
85 | /** |
||
86 | * Check if the extra param |
||
87 | * exists. |
||
88 | * |
||
89 | * @param $name |
||
90 | * @return bool |
||
91 | */ |
||
92 | public function has($name) |
||
98 | |||
99 | /** |
||
100 | * is utilized for reading data from inaccessible members. |
||
101 | * |
||
102 | * @param $name string |
||
103 | * @return mixed |
||
104 | */ |
||
105 | public function __get($name) |
||
113 | |||
114 | /** |
||
115 | * Whether a offset exists. |
||
116 | * |
||
117 | * @param mixed $offset |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function offsetExists($offset) |
||
124 | |||
125 | /** |
||
126 | * @param mixed $offset |
||
127 | * @return mixed Can return all value types. |
||
128 | */ |
||
129 | public function offsetGet($offset) |
||
133 | |||
134 | /** |
||
135 | * @param mixed $offset |
||
136 | * @param mixed $value |
||
137 | */ |
||
138 | public function offsetSet($offset, $value) |
||
142 | |||
143 | /** |
||
144 | * @param mixed $offset |
||
145 | */ |
||
146 | public function offsetUnset($offset) |
||
150 | |||
151 | /** |
||
152 | * Check if the value |
||
153 | * is a json string. |
||
154 | * |
||
155 | * @param $value |
||
156 | * @return bool |
||
157 | */ |
||
158 | View Code Duplication | public function isJson($value) |
|
168 | } |
||
169 |
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.