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 |
||
9 | class Comment extends Model{ |
||
|
|||
10 | |||
11 | /** |
||
12 | * get all comments of a post |
||
13 | * |
||
14 | * @access public |
||
15 | * @param array $postId |
||
16 | * @param integer $pageNum |
||
17 | * @param integer $commentsCreated |
||
18 | * @return array Associative array of the comments, and Pagination Object(View More). |
||
19 | * |
||
20 | */ |
||
21 | public function getAll($postId, $pageNum = 1, $commentsCreated = 0){ |
||
49 | |||
50 | /** |
||
51 | * get comment by Id |
||
52 | * |
||
53 | * @access public |
||
54 | * @param string $commentId |
||
55 | * @return array Array holds the data of the comment |
||
56 | * |
||
57 | */ |
||
58 | View Code Duplication | public function getById($commentId){ |
|
74 | |||
75 | /** |
||
76 | * create Comment. |
||
77 | * |
||
78 | * @access public |
||
79 | * @param string $userId |
||
80 | * @param string $postId |
||
81 | * @param string $content |
||
82 | * @return array Array holds the created comment |
||
83 | * @throws Exception If comment couldn't be created |
||
84 | * |
||
85 | */ |
||
86 | public function create($userId, $postId, $content){ |
||
111 | |||
112 | /** |
||
113 | * update Comment |
||
114 | * |
||
115 | * @access public |
||
116 | * @param string $commentId |
||
117 | * @param string $content |
||
118 | * @return array Array holds the updated comment |
||
119 | * @throws Exception If comment couldn't be updated |
||
120 | * |
||
121 | */ |
||
122 | View Code Duplication | public function update($commentId, $content){ |
|
144 | |||
145 | /** |
||
146 | * counting the number of comments of a post. |
||
147 | * |
||
148 | * @access public |
||
149 | * @static static method |
||
150 | * @param string $postId |
||
151 | * @return integer number of comments |
||
152 | * |
||
153 | */ |
||
154 | View Code Duplication | public static function countComments($postId){ |
|
163 | |||
164 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.