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 Com2Update extends FormModel |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Constructor injects with DI container and the id to update. |
||
| 16 | * |
||
| 17 | * @param Anax\DI\DIInterface $di a service container |
||
|
|
|||
| 18 | * @param integer $id to update |
||
| 19 | */ |
||
| 20 | 4 | public function __construct(DIInterface $dis, $id) |
|
| 21 | { |
||
| 22 | 4 | parent::__construct($dis); |
|
| 23 | 4 | $currId = $this->di->get("session")->get("user_id"); |
|
| 24 | 4 | $comment2 = $this->isgetItemDetails($id); |
|
| 25 | 4 | if ($currId != 1) { |
|
| 26 | 4 | if ($comment2->userId != $currId) { |
|
| 27 | 4 | $this->di->get("response")->redirect(""); |
|
| 28 | 4 | } |
|
| 29 | 4 | } |
|
| 30 | 4 | $this->form->create( |
|
| 31 | [ |
||
| 32 | 4 | "id" => __CLASS__, |
|
| 33 | 4 | "legend" => "Update details of the book", |
|
| 34 | 4 | ], |
|
| 35 | [ |
||
| 36 | "id" => [ |
||
| 37 | 4 | "type" => "hidden", |
|
| 38 | 4 | "value" => $comment2->id, |
|
| 39 | 4 | ], |
|
| 40 | |||
| 41 | "Title" => [ |
||
| 42 | 4 | "type" => "text", |
|
| 43 | 4 | "validation" => ["not_empty"], |
|
| 44 | 4 | "value" => $comment2->title, |
|
| 45 | 4 | ], |
|
| 46 | |||
| 47 | "Content" => [ |
||
| 48 | 4 | "type" => "textarea", |
|
| 49 | 4 | "validation" => ["not_empty"], |
|
| 50 | 4 | "value" => $comment2->content, |
|
| 51 | 4 | "class" => "editText", |
|
| 52 | 4 | ], |
|
| 53 | |||
| 54 | "submit" => [ |
||
| 55 | 4 | "type" => "submit", |
|
| 56 | 4 | "value" => "Save", |
|
| 57 | 4 | "callback" => [$this, "callbackSubmit"] |
|
| 58 | 4 | ], |
|
| 59 | |||
| 60 | "reset" => [ |
||
| 61 | 4 | "type" => "reset", |
|
| 62 | 4 | ], |
|
| 63 | ] |
||
| 64 | 4 | ); |
|
| 65 | 4 | } |
|
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Get details on item to load form with. |
||
| 71 | * |
||
| 72 | * @param integer $id get details on item with id. |
||
| 73 | * |
||
| 74 | * @return boolean true if okey, false if something went wrong. |
||
| 75 | */ |
||
| 76 | 4 | View Code Duplication | public function isgetItemDetails($id) |
| 77 | { |
||
| 78 | 4 | $comment2 = new Comment(); |
|
| 79 | 4 | $comment2->setDb($this->di->get("db")); |
|
| 80 | 4 | $comment2->find("id", $id); |
|
| 81 | 4 | return $comment2; |
|
| 82 | } |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Callback for submit-button which should return true if it could |
||
| 88 | * carry out its work and false if something failed. |
||
| 89 | * |
||
| 90 | * @return boolean true if okey, false if something went wrong. |
||
| 91 | */ |
||
| 92 | public function callbackSubmit() |
||
| 102 | } |
||
| 103 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.