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 QueryBuilder |
||
9 | { |
||
10 | /** |
||
11 | * WP_Query instance |
||
12 | * |
||
13 | * @var WP_Query |
||
14 | */ |
||
15 | protected $query; |
||
16 | |||
17 | /** |
||
18 | * Post Model instance |
||
19 | * |
||
20 | * @var Model |
||
21 | */ |
||
22 | protected $model; |
||
23 | |||
24 | /** |
||
25 | * Builder constructor. |
||
26 | * |
||
27 | * @param WP_QueryInterface $query |
||
28 | */ |
||
29 | public function __construct(WP_QueryInterface $query) |
||
33 | |||
34 | /** |
||
35 | * Limit the number of returned results |
||
36 | * |
||
37 | * @param integer $limit The maximum number of results to return |
||
38 | * use -1 for no limit |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | public function limit($limit) |
||
48 | |||
49 | /** |
||
50 | * Return an unlimited number of results. |
||
51 | * |
||
52 | * @return $this |
||
53 | */ |
||
54 | public function all() |
||
58 | |||
59 | /** |
||
60 | * Set the order for the query |
||
61 | * |
||
62 | * @param string $order |
||
63 | * |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function order($order) |
||
72 | |||
73 | /** |
||
74 | * Query by post status |
||
75 | * |
||
76 | * @param string|array $status the post status or stati to match |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function whereStatus($status) |
||
86 | |||
87 | /** |
||
88 | * Query by slug |
||
89 | * |
||
90 | * @param string $slug the post slug to query by |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function whereSlug($slug) |
||
100 | |||
101 | /** |
||
102 | * Get the results as a collection |
||
103 | * |
||
104 | * @return Collection |
||
105 | */ |
||
106 | public function results() |
||
114 | |||
115 | /** |
||
116 | * Get the results as a collection of post model instances |
||
117 | * |
||
118 | * @return Collection |
||
119 | */ |
||
120 | View Code Duplication | protected function collectModels() |
|
131 | |||
132 | /** |
||
133 | * Set a query variable on the query |
||
134 | * |
||
135 | * @param string $var Query variable key |
||
136 | * @param mixed $value Query value for key |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function set($var, $value) |
||
146 | |||
147 | /** |
||
148 | * Set the model for this query. |
||
149 | * |
||
150 | * @param Model $model |
||
151 | * |
||
152 | * @return $this |
||
153 | */ |
||
154 | public function setModel(Model $model) |
||
160 | |||
161 | /** |
||
162 | * Get the model |
||
163 | * |
||
164 | * @return Model |
||
165 | */ |
||
166 | public function getModel() |
||
170 | |||
171 | } |
||
172 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..