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 have status. |
||
14 | * |
||
15 | * @param string $status |
||
16 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
17 | */ |
||
18 | public function status(string $status) : PostBuilder |
||
22 | |||
23 | /** |
||
24 | * Return results where Posts have been published. |
||
25 | * |
||
26 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
27 | */ |
||
28 | public function published() : PostBuilder |
||
33 | |||
34 | /** |
||
35 | * Return results where Posts have been scheduled to be published. |
||
36 | * |
||
37 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
38 | */ |
||
39 | public function scheduled() : PostBuilder |
||
46 | |||
47 | /** |
||
48 | * Return results where Posts are drafted. |
||
49 | * |
||
50 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
51 | */ |
||
52 | public function draft() : PostBuilder |
||
59 | |||
60 | /** |
||
61 | * Return results where Posts are not yet published. |
||
62 | * |
||
63 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
64 | */ |
||
65 | public function notPublished() : PostBuilder |
||
73 | |||
74 | /** |
||
75 | * Order Post results by latest published. |
||
76 | * |
||
77 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
78 | */ |
||
79 | public function orderByLatest() : PostBuilder |
||
83 | |||
84 | /** |
||
85 | * Return results where Posts have been published last month. |
||
86 | * |
||
87 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
88 | */ |
||
89 | public function publishedLastMonth() : PostBuilder |
||
95 | |||
96 | /** |
||
97 | * Return results where Posts have been published last week. |
||
98 | * |
||
99 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
100 | */ |
||
101 | public function publishedLastWeek() : PostBuilder |
||
107 | |||
108 | /** |
||
109 | * Return results where Posts are related by the passed in Post Tags. |
||
110 | * |
||
111 | * @param \Chriscreates\Blog\Post $post |
||
112 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
113 | */ |
||
114 | public function relatedByPostTags(Post $post) : PostBuilder |
||
120 | |||
121 | /** |
||
122 | * Return results where Posts are related by the passed in Post Category. |
||
123 | * |
||
124 | * @param \Chriscreates\Blog\Post $post |
||
125 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
126 | */ |
||
127 | public function relatedByPostCategory(Post $post) : PostBuilder |
||
133 | |||
134 | /** |
||
135 | * Return results where Posts contain the Category(s) passed. |
||
136 | * |
||
137 | * @param $categories |
||
138 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
139 | */ |
||
140 | public function whereCategories($categories = null) : PostBuilder |
||
175 | |||
176 | /** |
||
177 | * Return results where Posts contain the Category(s) passed. |
||
178 | * |
||
179 | * @param array $options |
||
180 | * @return \Chriscreates\Blog\Builders\PostBuilder |
||
181 | */ |
||
182 | public function whereCategory(...$options) : PostBuilder |
||
228 | } |
||
229 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: