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 |
||
13 | class SearchQuery extends ViewableData |
||
14 | { |
||
15 | public static $missing = null; |
||
16 | public static $present = null; |
||
17 | |||
18 | public static $default_page_size = 10; |
||
19 | |||
20 | /** These are public, but only for index & variant access - API users should not manually access these */ |
||
21 | |||
22 | public $search = []; |
||
23 | |||
24 | public $classes = []; |
||
25 | |||
26 | public $require = []; |
||
27 | public $exclude = []; |
||
28 | |||
29 | protected $start = 0; |
||
30 | protected $limit = -1; |
||
31 | |||
32 | /** These are the API functions */ |
||
33 | |||
34 | public function __construct() |
||
43 | |||
44 | /** |
||
45 | * @param string $text Search terms. Exact format (grouping, boolean expressions, etc.) depends on |
||
46 | * the search implementation. |
||
47 | * @param array $fields Limits the search to specific fields (using composite field names) |
||
48 | * @param array $boost Map of composite field names to float values. The higher the value, |
||
49 | * the more important the field gets for relevancy. |
||
50 | */ |
||
51 | View Code Duplication | public function search($text, $fields = null, $boost = []) |
|
60 | |||
61 | /** |
||
62 | * Similar to {@link search()}, but uses stemming and other similarity algorithms |
||
63 | * to find the searched terms. For example, a term "fishing" would also likely find results |
||
64 | * containing "fish" or "fisher". Depends on search implementation. |
||
65 | * |
||
66 | * @param string $text See {@link search()} |
||
67 | * @param array $fields See {@link search()} |
||
68 | * @param array $boost See {@link search()} |
||
69 | */ |
||
70 | View Code Duplication | public function fuzzysearch($text, $fields = null, $boost = []) |
|
79 | |||
80 | public function inClass($class, $includeSubclasses = true) |
||
87 | |||
88 | /** |
||
89 | * Similar to {@link search()}, but typically used to further narrow down |
||
90 | * based on other facets which don't influence the field relevancy. |
||
91 | * |
||
92 | * @param string $field Composite name of the field |
||
93 | * @param mixed $values Scalar value, array of values, or an instance of SearchQuery_Range |
||
94 | */ |
||
95 | public function filter($field, $values) |
||
101 | |||
102 | /** |
||
103 | * Excludes results which match these criteria, inverse of {@link filter()}. |
||
104 | * |
||
105 | * @param string $field |
||
106 | * @param mixed $values |
||
107 | */ |
||
108 | public function exclude($field, $values) |
||
114 | |||
115 | public function start($start) |
||
119 | |||
120 | public function limit($limit) |
||
124 | |||
125 | public function page($page) |
||
130 | |||
131 | public function isfiltered() |
||
135 | |||
136 | public function __toString() |
||
140 | } |
||
141 |
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.