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 QueryBuilder extends BaseBuilder |
||
11 | { |
||
12 | /** |
||
13 | * The term model |
||
14 | * @var Model |
||
15 | */ |
||
16 | protected $model; |
||
17 | |||
18 | /** |
||
19 | * Collection of arguments |
||
20 | * @var Collection |
||
21 | */ |
||
22 | protected $args; |
||
23 | |||
24 | /** |
||
25 | * Taxonomy Identifier |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $taxonomy; |
||
29 | |||
30 | /** |
||
31 | * TermQueryBuilder Constructor. |
||
32 | * |
||
33 | * @param array $args |
||
34 | */ |
||
35 | public function __construct(array $args = []) |
||
39 | |||
40 | /** |
||
41 | * Create a new instance. |
||
42 | * |
||
43 | * @return static |
||
44 | */ |
||
45 | public static function make() |
||
49 | |||
50 | /** |
||
51 | * Restrict the query to terms of the provided Taxonomy. |
||
52 | * |
||
53 | * @param string $taxonomy |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function forTaxonomy($taxonomy) |
||
63 | |||
64 | /** |
||
65 | * Get all terms. |
||
66 | * |
||
67 | * @return $this |
||
68 | */ |
||
69 | public function all() |
||
74 | |||
75 | /** |
||
76 | * Include terms that have no related objects in the results. |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function includeEmpty() |
||
86 | |||
87 | /** |
||
88 | * Limit the maximum number of results returned. |
||
89 | * |
||
90 | * @param int $max_results Maximum number to return. 0 or 'all' for unlimited. |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function limit($max_results) |
||
100 | |||
101 | /** |
||
102 | * Get the query results. |
||
103 | * |
||
104 | * @throws WP_ErrorException |
||
105 | * |
||
106 | * @return Collection |
||
107 | */ |
||
108 | public function results() |
||
120 | |||
121 | /** |
||
122 | * Get the results as a collection of models. |
||
123 | * |
||
124 | * @return Collection |
||
125 | */ |
||
126 | View Code Duplication | protected function collectModels() |
|
138 | |||
139 | /** |
||
140 | * Perform the term query and return the results. |
||
141 | * |
||
142 | * @throws WP_ErrorException |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | protected function fetchTerms() |
||
154 | } |
||
155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.