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) |
||
57 | { |
||
58 | switch($op) |
||
59 | { |
||
60 | case 'ne': |
||
61 | return '!='; |
||
62 | case 'eq': |
||
63 | return '='; |
||
64 | case 'lt': |
||
65 | return '<'; |
||
66 | case 'le': |
||
67 | return '<='; |
||
68 | case 'gt': |
||
69 | return '>'; |
||
70 | case 'ge': |
||
71 | return '>='; |
||
72 | default: |
||
73 | return $op; |
||
74 | } |
||
75 | } |
||
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) |
||
83 | { |
||
84 | if($this->filterIsFunction($string)) |
||
85 | { |
||
86 | $this->op = strtok($string, '('); |
||
87 | $this->var1 = strtok(','); |
||
88 | $this->var2 = trim(strtok(')')); |
||
89 | return; |
||
90 | } |
||
91 | $field = strtok($string, ' '); |
||
92 | $op = strtok(' '); |
||
93 | $rest = strtok("\0"); |
||
94 | $this->var1 = $field; |
||
95 | $this->op = $this->odataOpToStd($op); |
||
96 | $this->var2 = $rest; |
||
97 | } |
||
98 | |||
99 | public function to_sql_string() |
||
112 | |||
113 | public function to_ldap_string() |
||
131 | |||
132 | private function getMongoIndexOfOperator() |
||
133 | { |
||
148 | |||
149 | /** |
||
150 | * Convert the standard operations to Mongo operations |
||
151 | * |
||
152 | * @param string $op The standard op |
||
153 | * |
||
154 | * @return string The mongo operator |
||
155 | */ |
||
156 | private function opToMongo($op) |
||
174 | |||
175 | /** |
||
176 | * Convert the right hand side of the filter clause into an Mongo array |
||
177 | * |
||
178 | * @param string $op The standard operator |
||
179 | * @param string $var The second variable in the operator |
||
180 | * |
||
181 | * @return array An array of mongo operations |
||
182 | */ |
||
183 | private function getMongoClauseArray($op, $var2) |
||
187 | |||
188 | public function toMongoFilter() |
||
218 | |||
219 | public function php_compare($value) |
||
237 | } |
||
238 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.