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 FileInputStream implements InputStream, Resettable, 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 the access is locked |
||
| 39 | * |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | private $locked; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new FileInputStream |
||
| 46 | * |
||
| 47 | * @param string $file |
||
| 48 | * The absolute (or relative) path to the file to open |
||
| 49 | * @throws FileNotFoundException |
||
| 50 | */ |
||
| 51 | 23 | public function __construct($file) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Cleanup (e.g. release lock) |
||
| 72 | */ |
||
| 73 | 22 | public function __destruct() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritDoc} |
||
| 86 | * @see \Generics\Streams\Stream::close() |
||
| 87 | */ |
||
| 88 | public function close() |
||
| 95 | 16 | ||
| 96 | /** |
||
| 97 | * {@inheritDoc} |
||
| 98 | * @see \Generics\Streams\Stream::ready() |
||
| 99 | */ |
||
| 100 | public function ready() |
||
| 104 | 22 | ||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | * @see \Generics\Streams\InputStream::read() |
||
| 108 | */ |
||
| 109 | public function read($length = 1, $offset = null) |
||
| 123 | |||
| 124 | 17 | /** |
|
| 125 | * {@inheritDoc} |
||
| 126 | * @see \Countable::count() |
||
| 127 | */ |
||
| 128 | public function count() |
||
| 133 | |||
| 134 | 3 | /** |
|
| 135 | 3 | *{@inheritDoc} |
|
| 136 | * @see \Generics\Streams\InputStream::reset() |
||
| 137 | */ |
||
| 138 | public function reset() |
||
| 142 | |||
| 143 | 2 | /** |
|
| 144 | * {@inheritDoc} |
||
| 145 | 2 | * @see \Generics\Lockable::lock() |
|
| 146 | 2 | */ |
|
| 147 | View Code Duplication | public function lock() |
|
| 154 | |||
| 155 | 2 | /** |
|
| 156 | 1 | * {@inheritDoc} |
|
| 157 | * @see \Generics\Lockable::unlock() |
||
| 158 | 2 | */ |
|
| 159 | 2 | View Code Duplication | public function unlock() |
| 166 | 2 | ||
| 167 | /** |
||
| 168 | 2 | * {@inheritDoc} |
|
| 169 | * @see \Generics\Lockable::isLocked() |
||
| 170 | */ |
||
| 171 | 2 | public function isLocked() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Retrieve the file path and name |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function __toString() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * {@inheritDoc} |
||
| 188 | * @see \Generics\Streams\Stream::isOpen() |
||
| 189 | */ |
||
| 190 | public function isOpen() |
||
| 194 | } |
||
| 195 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.