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 |
||
24 | class Minify extends BaseTask |
||
25 | { |
||
26 | /** |
||
27 | * @var string[] |
||
28 | */ |
||
29 | protected $types = ['css', 'js']; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $text; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $dst; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $type; |
||
45 | |||
46 | /** |
||
47 | * @var bool[] |
||
48 | */ |
||
49 | protected $squeezeOptions = [ |
||
50 | 'singleLine' => true, |
||
51 | 'keepImportantComments' => true, |
||
52 | 'specialVarRx' => false, |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * Constructor. Accepts asset file path or string source. |
||
57 | * |
||
58 | * @param string $input |
||
59 | */ |
||
60 | public function __construct($input) |
||
69 | |||
70 | /** |
||
71 | * Sets destination. Tries to guess type from it. |
||
72 | * |
||
73 | * @param string $dst |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function to($dst) |
||
87 | |||
88 | /** |
||
89 | * Sets type with validation. |
||
90 | * |
||
91 | * @param string $type |
||
92 | * Allowed values: "css", "js". |
||
93 | * |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function type($type) |
||
106 | |||
107 | /** |
||
108 | * Sets text from string source. |
||
109 | * |
||
110 | * @param string $text |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | protected function fromText($text) |
||
121 | |||
122 | /** |
||
123 | * Sets text from asset file path. Tries to guess type and set default destination. |
||
124 | * |
||
125 | * @param string $path |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | protected function fromFile($path) |
||
143 | |||
144 | /** |
||
145 | * Gets file extension from path. |
||
146 | * |
||
147 | * @param string $path |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | protected function getExtension($path) |
||
155 | |||
156 | /** |
||
157 | * Minifies and returns text. |
||
158 | * |
||
159 | * @return string|bool |
||
160 | */ |
||
161 | protected function getMinifiedText() |
||
194 | |||
195 | /** |
||
196 | * Single line option for the JS minimisation. |
||
197 | * |
||
198 | * @param bool $singleLine |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function singleLine($singleLine) |
||
207 | |||
208 | /** |
||
209 | * keepImportantComments option for the JS minimisation. |
||
210 | * |
||
211 | * @param bool $keepImportantComments |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function keepImportantComments($keepImportantComments) |
||
220 | |||
221 | /** |
||
222 | * Set specialVarRx option for the JS minimisation. |
||
223 | * |
||
224 | * @param bool $specialVarRx |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function specialVarRx($specialVarRx) |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function __toString() |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function run() |
||
299 | } |
||
300 |
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.