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  | 
            ||
| 15 | class Select extends Query  | 
            ||
| 16 | { | 
            ||
| 17 | const MODE_ONE = 1;  | 
            ||
| 18 | const MODE_ALL = 2;  | 
            ||
| 19 | |||
| 20 | use ExtendedSelectionTrait;  | 
            ||
| 21 | |||
| 22 | protected $join = [];  | 
            ||
| 23 | protected $orderBy = [];  | 
            ||
| 24 | protected $statement;  | 
            ||
| 25 | protected $mode = 2;  | 
            ||
| 26 | protected $page;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * Change the mode to returns just the first row.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @return self  | 
            ||
| 32 | */  | 
            ||
| 33 | public function one()  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * Change the mode to returns all rows.  | 
            ||
| 42 | *  | 
            ||
| 43 | * @return self  | 
            ||
| 44 | */  | 
            ||
| 45 | public function all()  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * Paginate the results  | 
            ||
| 54 | *  | 
            ||
| 55 | * @param int|string $page  | 
            ||
| 56 | * @param int|null $limit  | 
            ||
| 57 | *  | 
            ||
| 58 | * @return self  | 
            ||
| 59 | */  | 
            ||
| 60 |     public function page($page, $limit = null) { | 
            ||
| 73 | |||
| 74 | /**  | 
            ||
| 75 |      * {@inheritdoc} | 
            ||
| 76 | *  | 
            ||
| 77 | * @return Row|RowCollection|null  | 
            ||
| 78 | */  | 
            ||
| 79 | public function run()  | 
            ||
| 113 | |||
| 114 | /**  | 
            ||
| 115 | * Create a row and insert the joined rows if exist.  | 
            ||
| 116 | *  | 
            ||
| 117 | * @param array $data  | 
            ||
| 118 | *  | 
            ||
| 119 | * @return Row  | 
            ||
| 120 | */  | 
            ||
| 121 | public function createRow(array $data)  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Adds an ORDER BY clause.  | 
            ||
| 148 | *  | 
            ||
| 149 | * @param string $orderBy  | 
            ||
| 150 | * @param string|null $direction  | 
            ||
| 151 | *  | 
            ||
| 152 | * @return self  | 
            ||
| 153 | */  | 
            ||
| 154 | public function orderBy($orderBy, $direction = null)  | 
            ||
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Adds a LEFT JOIN clause.  | 
            ||
| 167 | *  | 
            ||
| 168 | * @param string $table  | 
            ||
| 169 | * @param string $on  | 
            ||
| 170 | * @param array|null $marks  | 
            ||
| 171 | *  | 
            ||
| 172 | * @return self  | 
            ||
| 173 | */  | 
            ||
| 174 | public function leftJoin($table, $on = null, $marks = null)  | 
            ||
| 178 | |||
| 179 | /**  | 
            ||
| 180 | * Adds a JOIN clause.  | 
            ||
| 181 | *  | 
            ||
| 182 | * @param string $join  | 
            ||
| 183 | * @param string $table  | 
            ||
| 184 | * @param string $on  | 
            ||
| 185 | * @param array|null $marks  | 
            ||
| 186 | *  | 
            ||
| 187 | * @return self  | 
            ||
| 188 | */  | 
            ||
| 189 | public function join($join, $table, $on = null, $marks = null)  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 |      * {@inheritdoc} | 
            ||
| 220 | */  | 
            ||
| 221 | View Code Duplication | public function __invoke()  | 
            |
| 228 | |||
| 229 | /**  | 
            ||
| 230 |      * {@inheritdoc} | 
            ||
| 231 | */  | 
            ||
| 232 | public function __toString()  | 
            ||
| 277 | |||
| 278 | /**  | 
            ||
| 279 | * Generates the fields/tables part of a SELECT query.  | 
            ||
| 280 | *  | 
            ||
| 281 | * @param string $table  | 
            ||
| 282 | * @param array $fields  | 
            ||
| 283 | * @param bool $rename  | 
            ||
| 284 | *  | 
            ||
| 285 | * @return string  | 
            ||
| 286 | */  | 
            ||
| 287 | protected static function buildFields($table, array $fields, $rename = false)  | 
            ||
| 301 | }  | 
            ||
| 302 | 
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.