Complex classes like DatasetTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DatasetTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait DatasetTrait { |
||
13 | /** |
||
14 | * @var int |
||
15 | */ |
||
16 | private $offset = 0; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private $limit = 0; |
||
22 | |||
23 | /** |
||
24 | * @var string[] |
||
25 | */ |
||
26 | private $order; |
||
27 | |||
28 | /** |
||
29 | * Get the dataset array. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | abstract public function getData(); |
||
34 | |||
35 | /** |
||
36 | * Get the current page. |
||
37 | * |
||
38 | * @return int Returns the page number. |
||
39 | */ |
||
40 | 1 | public function getPage(): int { |
|
48 | |||
49 | /** |
||
50 | * Set the current page. |
||
51 | * |
||
52 | * @param int $page A valid page number. |
||
53 | * @return $this |
||
54 | */ |
||
55 | 3 | public function setPage(int $page) { |
|
63 | |||
64 | /** |
||
65 | * Retrieve an external iterator |
||
66 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
67 | * @return Traversable Returns a generator of all rows. |
||
|
|||
68 | */ |
||
69 | 4 | public function getIterator() { |
|
72 | |||
73 | 10 | public function fetchAll(int $mode = 0, ...$args): array { |
|
107 | |||
108 | /** |
||
109 | * Fetch the data and perform a quasi-{@link array_column()} operation on it. |
||
110 | * |
||
111 | * @param string|int|null $columnKey The key or ordinal of the value or **null** to return the entire row. |
||
112 | * @param string|int|null $indexKey The key or ordinal of the index or **null** to not index the data. |
||
113 | * @param bool $grouped If true the result will be grouped by {@link $indexKey} and each value will be an array of rows. |
||
114 | * @return array Returns the array of results. |
||
115 | */ |
||
116 | 9 | public function fetchArrayColumn($columnKey = null, $indexKey = null, bool $grouped = false): array { |
|
167 | |||
168 | /** |
||
169 | * Get the first row of data. |
||
170 | * |
||
171 | * @return mixed|null Returns the first row or **null** if there is no data. |
||
172 | */ |
||
173 | 1 | public function firstRow() { |
|
178 | |||
179 | /** |
||
180 | * Get the number of records queried. |
||
181 | * |
||
182 | * @return int Returns the count. |
||
183 | */ |
||
184 | 11 | public function count(): int { |
|
187 | |||
188 | /** |
||
189 | * Specify data which should be serialized to JSON |
||
190 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
191 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
192 | * which is a value of any type other than a resource. |
||
193 | * @since 5.4.0 |
||
194 | */ |
||
195 | public function jsonSerialize() { |
||
198 | |||
199 | /** |
||
200 | * Get the offset. |
||
201 | * |
||
202 | * @return int Returns the offset. |
||
203 | */ |
||
204 | 2 | public function getOffset() { |
|
207 | |||
208 | /** |
||
209 | * Set the offset. |
||
210 | * |
||
211 | * @param int $offset |
||
212 | * @return $this |
||
213 | */ |
||
214 | 1 | public function setOffset($offset) { |
|
222 | |||
223 | /** |
||
224 | * Get the limit. |
||
225 | * |
||
226 | * @return int Returns the limit. |
||
227 | */ |
||
228 | 4 | public function getLimit() { |
|
231 | |||
232 | /** |
||
233 | * Set the limit. |
||
234 | * |
||
235 | * @param int $limit |
||
236 | * @return $this |
||
237 | */ |
||
238 | 2 | public function setLimit($limit) { |
|
246 | |||
247 | /** |
||
248 | * Get the sort order. |
||
249 | * |
||
250 | * @return string[] Returns an array of column names, optionally prefixed with "-" to denote descending order. |
||
251 | */ |
||
252 | 4 | public function getOrder() { |
|
255 | |||
256 | /** |
||
257 | * Set the sort order. |
||
258 | * |
||
259 | * @param string ...$columns The column names to sort by, optionally prefixed with "-" to denote descending order. |
||
260 | * @return $this |
||
261 | */ |
||
262 | public function setOrder(...$columns) { |
||
266 | } |
||
267 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.