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 |
||
12 | class Assets extends BaseAssets implements AssetsContract |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $globalVars = []; |
||
18 | |||
19 | /** |
||
20 | * Gets or sets javascript assets. |
||
21 | * |
||
22 | * @param bool|string $handle |
||
23 | * @param string $src Asset source |
||
24 | * @param array|string $dependency Dependencies |
||
25 | * @param bool $footer Whether to show in header or footer |
||
26 | * |
||
27 | 285 | * @return AssetElementInterface Setting returns asset array, getting returns asset HTML |
|
28 | */ |
||
29 | 285 | public function addJs($handle = false, $src = null, $dependency = null, $footer = true) |
|
33 | |||
34 | /** |
||
35 | * @param AssetElementInterface[] $assets |
||
36 | * @return array|bool|static |
||
37 | */ |
||
38 | protected function sort($assets) |
||
58 | |||
59 | /** |
||
60 | * @param AssetElement $asset |
||
61 | * @param Collection $mainAssets |
||
62 | * @param Collection $assets |
||
63 | * @return Collection |
||
64 | */ |
||
65 | protected function insertOn(AssetElement $asset, Collection &$mainAssets, Collection $assets) |
||
90 | |||
91 | /** |
||
92 | * Inserts a new key/value before the key in the array. |
||
93 | * |
||
94 | * @param $key |
||
95 | * The key to insert before. |
||
96 | * @param $array |
||
97 | * An array to insert in to. |
||
98 | * @param $new_key |
||
99 | * The key to insert. |
||
100 | * @param $new_value |
||
101 | * An value to insert. |
||
102 | * @return array|bool The new array if the key exists, FALSE otherwise. |
||
103 | * @see array_insert_after() |
||
104 | */ |
||
105 | View Code Duplication | protected function insertBefore($key, &$array, $new_key, $new_value) |
|
121 | |||
122 | /** |
||
123 | * Inserts a new key/value after the key in the array. |
||
124 | * |
||
125 | * @param $key |
||
126 | * The key to insert after. |
||
127 | * @param $array |
||
128 | * An array to insert in to. |
||
129 | * @param $new_key |
||
130 | * The key to insert. |
||
131 | * @param $new_value |
||
132 | * An value to insert. |
||
133 | * @return Collection|bool The new array if the key exists, FALSE otherwise. |
||
134 | */ |
||
135 | View Code Duplication | protected function insertAfter($key, Collection &$array, $new_key, $new_value) |
|
151 | |||
152 | /** |
||
153 | * Добавление глобальной переменной. |
||
154 | * |
||
155 | * @param string $key |
||
156 | * @param mixed $value |
||
157 | * |
||
158 | * @return self |
||
159 | */ |
||
160 | public function putGlobalVar($key, $value) |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function globalVars() |
||
174 | |||
175 | /** |
||
176 | * @return string |
||
177 | */ |
||
178 | public function render() |
||
182 | |||
183 | /** |
||
184 | * @return string |
||
185 | */ |
||
186 | public function renderGlobalVars() |
||
192 | } |
||
193 |
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.