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 |
||
| 10 | class Post extends Model{ |
||
|
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * get all posts |
||
| 14 | * |
||
| 15 | * @access public |
||
| 16 | * @param integer $pageNum |
||
| 17 | * @return array Associative array of the posts, and Pagination Object. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | View Code Duplication | public function getAll($pageNum = 1){ |
|
| 41 | |||
| 42 | /** |
||
| 43 | * append number of comments to the array of posts for each post. |
||
| 44 | * |
||
| 45 | * @access private |
||
| 46 | * @param array |
||
| 47 | * |
||
| 48 | */ |
||
| 49 | private function appendNumberOfComments(&$posts){ |
||
| 64 | |||
| 65 | /** |
||
| 66 | * get post by Id. |
||
| 67 | * |
||
| 68 | * @access public |
||
| 69 | * @param integer $postId |
||
| 70 | * @return array Array holds the data of the post |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function getById($postId){ |
|
| 87 | |||
| 88 | /** |
||
| 89 | * create post |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @param integer $userId |
||
| 93 | * @param string $title |
||
| 94 | * @param string $content |
||
| 95 | * @return bool |
||
| 96 | * @throws Exception If post couldn't be created |
||
| 97 | * |
||
| 98 | */ |
||
| 99 | public function create($userId, $title, $content){ |
||
| 124 | |||
| 125 | /** |
||
| 126 | * update Post |
||
| 127 | * |
||
| 128 | * @access public |
||
| 129 | * @static static method |
||
| 130 | * @param string $postId |
||
| 131 | * @param string $title |
||
| 132 | * @param string $content |
||
| 133 | * @return array Array of the updated post |
||
| 134 | * @throws Exception If post couldn't be updated |
||
| 135 | * |
||
| 136 | */ |
||
| 137 | public function update($postId, $title, $content){ |
||
| 163 | |||
| 164 | } |
||
| 165 |
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.