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 |
||
| 8 | class Question extends ActiveRecordModelExtender |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var string $tableName name of the database table. |
||
| 13 | */ |
||
| 14 | protected $tableName = "coffee_questions"; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Columns in the table. |
||
| 18 | * |
||
| 19 | * @var integer $id primary key auto incremented. |
||
| 20 | */ |
||
| 21 | public $id; |
||
| 22 | public $user; |
||
| 23 | |||
| 24 | public $title; |
||
| 25 | public $tags; |
||
| 26 | |||
| 27 | public $created; |
||
| 28 | public $status; # default is active |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Set ups the question |
||
| 32 | * @param object $question |
||
| 33 | * |
||
| 34 | * @return object |
||
| 35 | */ |
||
| 36 | 8 | public function setupQuestion($question) |
|
| 52 | /** |
||
| 53 | * Returns post with markdown and gravatar |
||
| 54 | * @param string $sql |
||
| 55 | * @param array $param |
||
|
|
|||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 8 | View Code Duplication | public function getQuestions($sql = null, $params = null) |
| 60 | { |
||
| 61 | 8 | $questions = []; |
|
| 62 | |||
| 63 | 8 | if ($sql == null) { |
|
| 64 | 2 | $questions = $this->findAll(); |
|
| 65 | 2 | } |
|
| 66 | 8 | if ($sql != null) { |
|
| 67 | 8 | $questions = $this->findAllWhere($sql, $params); |
|
| 68 | 8 | } |
|
| 69 | // array_reverse so latest order question gets returned |
||
| 70 | 8 | return array_reverse(array_map(array($this, 'setupQuestion'), $questions)); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Returns one question with it's own question text and other answers |
||
| 75 | * @param int $id |
||
| 76 | * |
||
| 77 | * @return object |
||
| 78 | */ |
||
| 79 | 7 | public function getQuestion($id) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Returns array of tags, keys are name, value is the integer of how many. |
||
| 87 | * |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | 1 | public function getPopularTags() |
|
| 103 | } |
||
| 104 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.