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 = "ramverk1_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 | * @param array $param |
||
|
|||
34 | * |
||
35 | * @return object |
||
36 | */ |
||
37 | 8 | public function setupQuestion($question) |
|
38 | { |
||
39 | 8 | $user = new User($this->db); |
|
40 | 8 | $post = new Post($this->db); |
|
41 | |||
42 | 8 | $question->img = $user->getGravatar($question->user); |
|
43 | 8 | $question->title = $this->getMD($question->title); |
|
44 | 8 | $question->tags = explode(',', $question->tags); |
|
45 | |||
46 | 8 | $question->question = $post->getPost("questionId = ? AND type = ?", [$question->id, "question"]); |
|
47 | 8 | $question->answers = $post->getAllPosts("questionId = ? AND type = ?", [$question->id, "answer"]); |
|
48 | 8 | $question->answerCount = count($question->answers); |
|
49 | |||
50 | |||
51 | 8 | return $question; |
|
52 | } |
||
53 | /** |
||
54 | * Returns post with markdown and gravatar |
||
55 | * @param string $sql |
||
56 | * @param array $param |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | 8 | View Code Duplication | public function getQuestions($sql = null, $params = null) |
61 | { |
||
62 | 8 | if ($sql == null) { |
|
63 | 2 | $questions = $this->findAll(); |
|
64 | 2 | } |
|
65 | 8 | if ($sql != null) { |
|
66 | 8 | $questions = $this->findAllWhere($sql, $params); |
|
67 | 8 | } |
|
68 | // array_reverse so latest order question gets returned |
||
69 | 8 | return array_map(array($this, 'setupQuestion'), $questions); |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Returns one question with it's own question text and other answers |
||
74 | * @param int $id |
||
75 | * |
||
76 | * @return object |
||
77 | */ |
||
78 | 7 | public function getQuestion($id) |
|
79 | { |
||
80 | 7 | $question = $this->find("id", $id); |
|
81 | 7 | return $this->setupQuestion($question); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Returns array of tags, keys are name, value is the integer of how many. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | 1 | public function getPopularTags() |
|
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.