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:
Complex classes like FilterClause 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 FilterClause, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class FilterClause |
||
5 | { |
||
6 | public $var1; |
||
7 | public $var2; |
||
8 | public $op; |
||
9 | |||
10 | /** |
||
11 | * Create a filter clause from the string |
||
12 | * |
||
13 | * @param string $string The filter clause string |
||
14 | */ |
||
15 | public function __construct($string = false) |
||
22 | |||
23 | /** |
||
24 | * Find the string inside the other string |
||
25 | * |
||
26 | * @param string $haystack The string to search inside |
||
27 | * @param string $needle The string to search for |
||
28 | * |
||
29 | * @return boolean True if the needle exists in the haystack, false otherwise |
||
30 | */ |
||
31 | protected static function str_startswith($haystack, $needle) |
||
35 | |||
36 | /** |
||
37 | * Is the specified filter a function? |
||
38 | * |
||
39 | * @param string $string The filter clause |
||
40 | * |
||
41 | * @return boolean True if the filter is an operation, false otherwise |
||
42 | */ |
||
43 | protected function filterIsFunction($string) |
||
48 | |||
49 | /** |
||
50 | * Convert the OData simple op to standard operations |
||
51 | * |
||
52 | * @param string $op The OData op |
||
53 | * |
||
54 | * @return string The standard programatic notation |
||
55 | */ |
||
56 | protected function odataOpToStd($op) |
||
76 | |||
77 | /** |
||
78 | * Convert the string into an OData Filter |
||
79 | * |
||
80 | * @param string $string The string to turn into a filter |
||
81 | */ |
||
82 | protected function process_filter_string($string) |
||
98 | |||
99 | public function to_sql_string() |
||
112 | |||
113 | public function to_ldap_string() |
||
131 | |||
132 | private function getMongoIndexOfOperator() |
||
155 | |||
156 | /** |
||
157 | * Convert the standard operations to Mongo operations |
||
158 | * |
||
159 | * @param string $op The standard op |
||
160 | * |
||
161 | * @return string The mongo operator |
||
162 | */ |
||
163 | private function opToMongo($op) |
||
181 | |||
182 | /** |
||
183 | * Convert the right hand side of the filter clause into an Mongo array |
||
184 | * |
||
185 | * @param string $op The standard operator |
||
186 | * @param string $var The second variable in the operator |
||
187 | * |
||
188 | * @return array An array of mongo operations |
||
189 | */ |
||
190 | private function getMongoClauseArray($op, $var2) |
||
194 | |||
195 | public function toMongoFilter() |
||
254 | |||
255 | public function php_compare($value) |
||
277 | } |
||
278 |
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.