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 ShowOneService |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var array $commentitem, the chosen comment. |
||
| 15 | */ |
||
| 16 | protected $comment; |
||
| 17 | protected $comments; |
||
| 18 | protected $sess; |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructor injects with DI container and the id to update. |
||
| 23 | * |
||
| 24 | * @param Anax\DI\DIInterface $di a service container |
||
| 25 | * @param integer $id to show |
||
| 26 | */ |
||
| 27 | public function __construct(DIInterface $di, $id) |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Get details on item to load form with. |
||
| 43 | * |
||
| 44 | * @param integer $id get details on item with id. |
||
| 45 | * |
||
| 46 | * @return Comm |
||
| 47 | */ |
||
| 48 | View Code Duplication | public function getItemDetails($id) |
|
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Get details on item to load form with. |
||
| 59 | * |
||
| 60 | * @param string $where |
||
| 61 | * @param array $params get details on item with id parentid. |
||
| 62 | * |
||
| 63 | * @return Comm |
||
| 64 | */ |
||
| 65 | public function getParentDetails($where, $params) |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Sets the callable to use for creating routes. |
||
| 75 | * |
||
| 76 | * @param callable $urlCreate to create framework urls. |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | public function setUrlCreator($route) |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns link for gravatar img |
||
| 89 | * |
||
| 90 | * @param object $item |
||
| 91 | * @return string htmlcode |
||
| 92 | */ |
||
| 93 | public function getGravatar($item) |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * Returns json_decoded title and text |
||
| 103 | * If lead text, headline is larger font |
||
| 104 | * @param object $item |
||
| 105 | * @return string htmlcode |
||
| 106 | */ |
||
| 107 | public function getDecode($item, $lead = null) |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * If param met, returns string with edit-links |
||
| 125 | * @param boolean $isadmin |
||
| 126 | * @param object $item |
||
| 127 | * @param string $update |
||
| 128 | * @param string $del |
||
| 129 | * @return string htmlcode |
||
| 130 | */ |
||
| 131 | public function getCanEdit($item, $isadmin, $update, $del) |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * If session contains correct id, returns string with edit-links |
||
| 146 | * |
||
| 147 | * @return string htmlcode |
||
| 148 | */ |
||
| 149 | public function getLoginurl() |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * If loggedin allowed to edit |
||
| 165 | * |
||
| 166 | * @param boolean $isadmin |
||
| 167 | * @param string $userid |
||
| 168 | * @param string $update, link |
||
| 169 | * @param string $htmlcomment, link |
||
| 170 | * |
||
| 171 | * @return string htmlcode |
||
| 172 | */ |
||
| 173 | public function getEdit($isadmin, $userid, $update, $commid, $htmlcomment) |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * If session contains correct id, returns string with edit-links |
||
| 187 | * |
||
| 188 | * @return string htmlcode |
||
| 189 | */ |
||
| 190 | public function getDelete($isadmin, $userid, $del, $commid) |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns html for each item |
||
| 201 | * |
||
| 202 | * @param object $item |
||
| 203 | * @param string $can |
||
| 204 | * |
||
| 205 | * @return string htmlcod |
||
| 206 | */ |
||
| 207 | public function getValHtml($item, $can) |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns all text for the view |
||
| 223 | * |
||
| 224 | * @return string htmlcode |
||
| 225 | */ |
||
| 226 | public function getHTML() |
||
| 257 | } |
||
| 258 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: