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 | function __construct($string = false) |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Find the string inside the other string |
||
| 20 | * |
||
| 21 | * @param string $haystack The string to search inside |
||
| 22 | * @param string $needle The string to search for |
||
| 23 | * |
||
| 24 | * @return boolean True if the needle exists in the haystack, false otherwise |
||
| 25 | */ |
||
| 26 | static function str_startswith($haystack, $needle) |
||
| 30 | |||
| 31 | protected function filterIsFunction($string) |
||
| 36 | |||
| 37 | protected function odataOpToStd($op) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Convert the string into an OData Filter |
||
| 60 | * |
||
| 61 | * @param string $string The string to turn into a filter |
||
| 62 | */ |
||
| 63 | protected function process_filter_string($string) |
||
| 79 | |||
| 80 | public function to_sql_string() |
||
| 93 | |||
| 94 | public function to_ldap_string() |
||
| 112 | |||
| 113 | private function getMongoIndexOfOperator() |
||
| 129 | |||
| 130 | private function opToMongo($op) |
||
| 148 | |||
| 149 | private function getMongoClauseArray($op, $var2) |
||
| 153 | |||
| 154 | public function toMongoFilter() |
||
| 173 | |||
| 174 | function php_compare($value) |
||
| 192 | } |
||
| 193 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.