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 | 1 | public function __construct(DIInterface $di, $id) |
|
| 28 | { |
||
| 29 | 1 | $this->di = $di; |
|
|
|
|||
| 30 | 1 | $this->comment = $this->getItemDetails($id); |
|
| 31 | |||
| 32 | 1 | $where = "parentid = ?"; |
|
| 33 | 1 | $params = [$id]; |
|
| 34 | 1 | $this->comments = $this->getParentDetails($where, $params); |
|
| 35 | |||
| 36 | 1 | $session = $this->di->get("session"); |
|
| 37 | 1 | $this->sess = $session->get("user"); |
|
| 38 | 1 | } |
|
| 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 | 1 | View Code Duplication | public function getItemDetails($id) |
| 49 | { |
||
| 50 | 1 | $comm = new Comm(); |
|
| 51 | 1 | $comm->setDb($this->di->get("db")); |
|
| 52 | 1 | $comm->find("id", $id); |
|
| 53 | 1 | return $comm; |
|
| 54 | } |
||
| 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 | 1 | public function getParentDetails($where, $params) |
|
| 66 | { |
||
| 67 | 1 | $comm = new Comm(); |
|
| 68 | 1 | $comm->setDb($this->di->get("db")); |
|
| 69 | 1 | return $comm->findAllWhere($where, $params); |
|
| 70 | } |
||
| 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 | 1 | public function setUrlCreator($route) |
|
| 81 | { |
||
| 82 | 1 | $url = $this->di->get("url"); |
|
| 83 | 1 | return call_user_func([$url, "create"], $route); |
|
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns link for gravatar img |
||
| 89 | * |
||
| 90 | * @param object $item |
||
| 91 | * @return string htmlcode |
||
| 92 | */ |
||
| 93 | 1 | public function getGravatar($item) |
|
| 94 | { |
||
| 95 | 1 | $comm = new Comm(); |
|
| 96 | 1 | $gravatar = $comm->getGravatar($item); |
|
| 97 | 1 | return '<img src="' . $gravatar . '" alt=""/>'; |
|
| 98 | } |
||
| 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 | 1 | public function getDecode(Comm $item, $lead = null) |
|
| 108 | { |
||
| 109 | 1 | $comt = json_decode($item->comment); |
|
| 110 | 1 | if ($comt->frontmatter->title) { |
|
| 111 | 1 | $til = $comt->frontmatter->title; |
|
| 112 | 1 | } else { |
|
| 113 | $til = $item->title; |
||
| 114 | } |
||
| 115 | 1 | $comt = $comt->text; |
|
| 116 | 1 | if ($lead) { |
|
| 117 | return '<h3>' . $til . '</h3><p>' . $comt . '</p>'; |
||
| 118 | } |
||
| 119 | 1 | return '<h4>' . $til . '</h4><p>' . $comt . '</p>'; |
|
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * If param met, returns string with edit-links |
||
| 125 | * @param boolean $isadmin |
||
| 126 | * @param object $item |
||
| 127 | * @param string $del |
||
| 128 | * @return string htmlcode |
||
| 129 | */ |
||
| 130 | 1 | public function getCanEdit(Comm $item, $isadmin, $del) |
|
| 131 | { |
||
| 132 | 1 | $canedit = ""; |
|
| 133 | 1 | $update = $this->setUrlCreator("comm/update"); |
|
| 134 | 1 | if ($item->userid == $this->sess['id'] || $isadmin) { |
|
| 135 | 1 | $canedit = '<br /><a href="' . $update . '/' . $item->id . '">Redigera</a>'; |
|
| 136 | 1 | $canedit .= ' | <a href="' . $del . '/' . $item->id . '">Ta bort inlägget</a></p>'; |
|
| 137 | 1 | } else { |
|
| 138 | $canedit .= '</p>'; |
||
| 139 | } |
||
| 140 | 1 | return $canedit; |
|
| 141 | } |
||
| 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 $commid |
||
| 169 | * @param string $htmlcomment, link |
||
| 170 | * |
||
| 171 | * @return string htmlcode |
||
| 172 | */ |
||
| 173 | public function getEdit($isadmin, $userid, $commid, $htmlcomment) |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * If session contains correct id, returns string with edit-links |
||
| 188 | * |
||
| 189 | * @return string htmlcode |
||
| 190 | */ |
||
| 191 | public function getDelete($isadmin, $userid, $del, $commid) |
||
| 199 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns html for each item |
||
| 203 | * |
||
| 204 | * @param object $item |
||
| 205 | * @param string $can |
||
| 206 | * |
||
| 207 | * @return string htmlcod |
||
| 208 | */ |
||
| 209 | 1 | public function getValHtml(Comm $item, $can) |
|
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns all text for the view |
||
| 225 | * |
||
| 226 | * @return string htmlcode |
||
| 227 | */ |
||
| 228 | public function getHTML() |
||
| 258 | } |
||
| 259 |
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: