Complex classes like My_Class 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 My_Class, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | abstract class My_Class { |
||
| 4 | |||
| 5 | public function __construct() {} |
||
| 8 | |||
| 9 | public function getSomeValue() {} |
||
| 10 | public function parseMyDSN() {} |
||
| 11 | public function get_some_value() {} |
||
| 12 | public function GetSomeValue() {} |
||
| 13 | public function getSomeValue_Again() {} |
||
| 14 | public function My_Package_getSomeValue() {} |
||
| 15 | public function _getSomeValue() {} |
||
| 16 | public function _parseMyDSN() {} |
||
| 17 | public function _get_some_value() {} |
||
| 18 | public function _GetSomeValue() {} |
||
| 19 | public function _getSomeValue_Again() {} |
||
| 20 | public function _My_Package_getSomeValue() {} |
||
| 21 | |||
| 22 | protected function getSomeValue() {} |
||
| 23 | protected function parseMyDSN() {} |
||
| 24 | protected function get_some_value() {} |
||
| 25 | protected function GetSomeValue() {} |
||
| 26 | protected function getSomeValue_Again() {} |
||
| 27 | protected function My_Package_getSomeValue() {} |
||
| 28 | protected function _getSomeValue() {} |
||
| 29 | protected function _parseMyDSN() {} |
||
| 30 | protected function _get_some_value() {} |
||
| 31 | protected function _GetSomeValue() {} |
||
| 32 | protected function _getSomeValue_Again() {} |
||
| 33 | protected function _My_Package_getSomeValue() {} |
||
| 34 | |||
| 35 | private function getSomeValue() {} |
||
| 36 | private function parseMyDSN() {} |
||
| 37 | private function get_some_value() {} |
||
| 38 | private function GetSomeValue() {} |
||
| 39 | private function getSomeValue_Again() {} |
||
| 40 | private function My_Package_getSomeValue() {} |
||
| 41 | private function _getSomeValue() {} |
||
| 42 | private function _parseMyDSN() {} |
||
| 43 | private function _get_some_value() {} |
||
| 44 | private function _GetSomeValue() {} |
||
| 45 | private function _getSomeValue_Again() {} |
||
| 46 | private function _My_Package_getSomeValue() {} |
||
| 47 | |||
| 48 | function getSomeValue() {} |
||
|
|
|||
| 49 | function parseMyDSN() {} |
||
| 50 | function get_some_value() {} |
||
| 51 | function GetSomeValue() {} |
||
| 52 | function getSomeValue_Again() {} |
||
| 53 | function My_Package_getSomeValue() {} |
||
| 54 | function _getSomeValue() {} |
||
| 55 | function _parseMyDSN() {} |
||
| 56 | function _get_some_value() {} |
||
| 57 | function _GetSomeValue() {} |
||
| 58 | function _getSomeValue_Again() {} |
||
| 59 | function _My_Package_getSomeValue() {} |
||
| 60 | |||
| 61 | }//end class |
||
| 62 | |||
| 289 |
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.