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 PostBuilder extends Builder |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Return results where Posts are related for the current logged in user. |
||
| 14 | * |
||
| 15 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 16 | */ |
||
| 17 | public function forCurrentUser() : PostBuilder |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Return results where Posts have status. |
||
| 24 | * |
||
| 25 | * @param string $status |
||
| 26 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 27 | */ |
||
| 28 | public function status(string $status) : PostBuilder |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Return results where Posts have been published. |
||
| 35 | * |
||
| 36 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 37 | */ |
||
| 38 | public function published() : PostBuilder |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Return results where Posts have been scheduled to be published. |
||
| 46 | * |
||
| 47 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 48 | */ |
||
| 49 | public function scheduled() : PostBuilder |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return results where Posts are drafted. |
||
| 59 | * |
||
| 60 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 61 | */ |
||
| 62 | public function draft() : PostBuilder |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return results where Posts are not yet published. |
||
| 72 | * |
||
| 73 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 74 | */ |
||
| 75 | public function notPublished() : PostBuilder |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Order Post results by latest published. |
||
| 86 | * |
||
| 87 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 88 | */ |
||
| 89 | public function orderByLatest() : PostBuilder |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Return results where Posts have been published last month. |
||
| 96 | * |
||
| 97 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 98 | */ |
||
| 99 | public function publishedLastMonth() : PostBuilder |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return results where Posts have been published last week. |
||
| 108 | * |
||
| 109 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 110 | */ |
||
| 111 | public function publishedLastWeek() : PostBuilder |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Return results where Posts are related by the passed in Post Tags. |
||
| 120 | * |
||
| 121 | * @param \Chriscreates\Blog\Post $post |
||
| 122 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 123 | */ |
||
| 124 | public function relatedByPostTags(Post $post) : PostBuilder |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return results where Posts are related by the passed in Post Category. |
||
| 133 | * |
||
| 134 | * @param \Chriscreates\Blog\Post $post |
||
| 135 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 136 | */ |
||
| 137 | public function relatedByPostCategory(Post $post) : PostBuilder |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return results where Posts contain the Category(s) passed. |
||
| 146 | * |
||
| 147 | * @param $categories |
||
| 148 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 149 | */ |
||
| 150 | public function whereCategories($categories = null) : PostBuilder |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return results where Posts contain the Category(s) passed. |
||
| 188 | * |
||
| 189 | * @param array $options |
||
| 190 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
| 191 | */ |
||
| 192 | public function whereCategory(...$options) : PostBuilder |
||
| 238 | } |
||
| 239 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: