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 |
||
| 11 | class MySql extends AbstractSqlTranslator { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Resolve the given value as an identifier. |
||
| 15 | * |
||
| 16 | * @param mixed $identifier |
||
| 17 | * @return mixed |
||
| 18 | */ |
||
| 19 | protected function resolveIdentifier($identifier) { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 35 | * |
||
| 36 | * @param int $limit [optional] |
||
| 37 | * @param int $offset [optional] |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | protected function prepareLimit($limit = 0, $offset = 0) { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 59 | * options. |
||
| 60 | * |
||
| 61 | * @param string $table |
||
| 62 | * @param array|string $columns |
||
| 63 | * @param string $joins [optional] |
||
| 64 | * @param string $where [optional] |
||
| 65 | * @param string $order [optional] |
||
| 66 | * @param string $limit [optional] |
||
| 67 | * @param bool $distinct [optional] |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | View Code Duplication | protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 80 | * |
||
| 81 | * @param string $table |
||
| 82 | * @param array $data |
||
| 83 | * @param string $where [optional] |
||
| 84 | * @param string $limit [optional] |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | View Code Duplication | protected function prepareUpdate($table, $data, $where = null, $limit = null) { |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Prepare a DELETE statement with the given table and clauses. |
||
| 103 | * |
||
| 104 | * @param string $table |
||
| 105 | * @param string $where [optional] |
||
| 106 | * @param string $limit [optional] |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | View Code Duplication | protected function prepareDelete($table, $where = null, $limit = null) { |
|
| 118 | |||
| 119 | } |
||
| 120 |
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.