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 |
||
| 12 | class EmojiController |
||
| 13 | { |
||
| 14 | private $auth; |
||
| 15 | |||
| 16 | public function __construct(Oauth $auth) |
||
| 20 | |||
| 21 | /** |
||
| 22 | * This method list all emojis. |
||
| 23 | * |
||
| 24 | * @param $response |
||
| 25 | * |
||
| 26 | * @return json $emojis |
||
| 27 | */ |
||
| 28 | public function listAllEmoji(Response $response) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This method get a single emoji. |
||
| 43 | * |
||
| 44 | * @param $response |
||
| 45 | * @param $args |
||
| 46 | * |
||
| 47 | * @return json $emoji |
||
| 48 | */ |
||
| 49 | public function getSingleEmoji(Response $response, $args) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * This method creates a new emoji. |
||
| 66 | * |
||
| 67 | * @param $args |
||
| 68 | * |
||
| 69 | * @return json $response; |
||
| 70 | */ |
||
| 71 | public function createEmoji(Request $request, Response $response) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * This method updates an emoji. |
||
| 104 | * |
||
| 105 | * @param $request |
||
| 106 | * @param $response |
||
| 107 | * |
||
| 108 | * @return json |
||
| 109 | */ |
||
| 110 | public function updateEmojiByPutVerb(Request $request, Response $response, $args) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * This method updates an emoji partially. |
||
| 135 | * |
||
| 136 | * @param $request |
||
| 137 | * @param $response |
||
| 138 | * |
||
| 139 | * @return json |
||
| 140 | */ |
||
| 141 | public function updateEmojiByPatchVerb(Request $request, Response $response, $args) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * This method deletes an emoji. |
||
| 163 | * |
||
| 164 | * @param $request |
||
| 165 | * @param $response |
||
| 166 | * @param $args |
||
| 167 | * |
||
| 168 | * @return json |
||
| 169 | */ |
||
| 170 | public function deleteEmoji(Request $request, Response $response, $args) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * This method creates emoji keywords. |
||
| 188 | * |
||
| 189 | * @param $request |
||
| 190 | * @param $response |
||
| 191 | * @param $args |
||
| 192 | * |
||
| 193 | * @return $id |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function createEmojiKeywords($emoji_id, $keywords) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * This method format emoji result. |
||
| 216 | * |
||
| 217 | * @param $emojis |
||
| 218 | * |
||
| 219 | * @return array $emojis |
||
| 220 | */ |
||
| 221 | public function formatEmoji(array $emojis) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * This method authenticate and return user id. |
||
| 234 | */ |
||
| 235 | public function getCurrentUserId($request, $response) |
||
| 260 | } |
||
| 261 |
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.