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 |
||
20 | class FileOutputStream implements OutputStream, Lockable |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * The file handle |
||
25 | * |
||
26 | * @var resource |
||
27 | */ |
||
28 | private $handle; |
||
29 | |||
30 | /** |
||
31 | * The absolute file path and name |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $fileName; |
||
36 | |||
37 | /** |
||
38 | * Whether resource is locked |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $locked; |
||
43 | |||
44 | /** |
||
45 | * Create a new FileOutputStream |
||
46 | * |
||
47 | * @param string $file |
||
48 | * The absolute (or relative) path to file to write into. |
||
49 | * @param boolean $append |
||
50 | * Whether to append the data to an existing file. |
||
51 | * @throws FileExistsException will be thrown in case of file exists and append is set to false. |
||
52 | * @throws NoAccessException will be thrown in case of it is not possible to write to file. |
||
53 | */ |
||
54 | 24 | public function __construct($file, $append = false) |
|
87 | |||
88 | /** |
||
89 | * Cleanup (e.g. release lock) |
||
90 | */ |
||
91 | 23 | public function __destruct() |
|
101 | |||
102 | /** |
||
103 | * {@inheritDoc} |
||
104 | * @see \Generics\Streams\Stream::ready() |
||
105 | */ |
||
106 | public function ready() |
||
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | * @see \Generics\Streams\OutputStream::write() |
||
114 | */ |
||
115 | public function write($buffer) |
||
138 | |||
139 | 18 | /** |
|
140 | * {@inheritDoc} |
||
141 | * @see \Generics\Streams\Stream::close() |
||
142 | */ |
||
143 | public function close() |
||
150 | 20 | ||
151 | 20 | /** |
|
152 | 20 | * {@inheritDoc} |
|
153 | * @see \Countable::count() |
||
154 | */ |
||
155 | public function count() |
||
163 | |||
164 | /** |
||
165 | 1 | * Retrieve the file path and name |
|
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function __toString() |
||
173 | 1 | ||
174 | /** |
||
175 | 1 | * {@inheritDoc} |
|
176 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
177 | */ |
||
178 | public function isWriteable() |
||
182 | |||
183 | 1 | /** |
|
184 | * {@inheritDoc} |
||
185 | 1 | * @see \Generics\Streams\OutputStream::flush() |
|
186 | */ |
||
187 | public function flush() |
||
195 | 15 | ||
196 | 1 | /** |
|
197 | * {@inheritDoc} |
||
198 | * @see \Generics\Lockable::lock() |
||
199 | 14 | */ |
|
200 | 14 | View Code Duplication | public function lock() |
207 | 3 | ||
208 | /** |
||
209 | 3 | * {@inheritDoc} |
|
210 | 1 | * @see \Generics\Lockable::unlock() |
|
211 | */ |
||
212 | 3 | View Code Duplication | public function unlock() |
219 | |||
220 | 4 | /** |
|
221 | * {@inheritDoc} |
||
222 | 4 | * @see \Generics\Lockable::isLocked() |
|
223 | 1 | */ |
|
224 | public function isLocked() |
||
228 | |||
229 | /** |
||
230 | * {@inheritDoc} |
||
231 | * @see \Generics\Streams\Stream::isOpen() |
||
232 | */ |
||
233 | public function isOpen() |
||
237 | } |
||
238 |
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.