Complex classes like Extension 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 Extension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait Extension { |
||
| 8 | |||
| 9 | private static $section = '', $dir_name = '', $items = [], $active = '', $primary = ''; |
||
| 10 | |||
| 11 | # Get section |
||
| 12 | |||
| 13 | private static function getSection(string $section) { |
||
| 17 | |||
| 18 | # Get directory name |
||
| 19 | |||
| 20 | private static function getDirName(string $section) { |
||
| 24 | |||
| 25 | # Get items |
||
| 26 | |||
| 27 | private static function getItems(string $dir_name) { |
||
| 48 | |||
| 49 | # Get user defined extension name |
||
| 50 | |||
| 51 | private static function getUserDefined() { |
||
| 65 | |||
| 66 | # Check if name valid |
||
| 67 | |||
| 68 | public static function valid(string $name) { |
||
| 72 | |||
| 73 | # Validate name |
||
| 74 | |||
| 75 | public static function validate(string $name) { |
||
| 79 | |||
| 80 | # Check if extension exists |
||
| 81 | |||
| 82 | public static function exists(string $name) { |
||
| 86 | |||
| 87 | # Init extensions list |
||
| 88 | |||
| 89 | public static function init($section) { |
||
| 111 | |||
| 112 | # Return active section |
||
| 113 | |||
| 114 | public static function section() { |
||
| 118 | |||
| 119 | # Return directory name |
||
| 120 | |||
| 121 | public static function dirName() { |
||
| 125 | |||
| 126 | # Return items |
||
| 127 | |||
| 128 | public static function items(string $section = null) { |
||
| 136 | |||
| 137 | # Return active extension name |
||
| 138 | |||
| 139 | public static function active() { |
||
| 143 | |||
| 144 | # Return primary extension name |
||
| 145 | |||
| 146 | public static function primary() { |
||
| 150 | |||
| 151 | # Get active extension path |
||
| 152 | |||
| 153 | public static function path() { |
||
| 159 | |||
| 160 | # Get primary extension path |
||
| 161 | |||
| 162 | public static function pathPrimary() { |
||
| 168 | |||
| 169 | # Get active extension data |
||
| 170 | |||
| 171 | public static function data(string $name = null) { |
||
| 179 | } |
||
| 180 | } |
||
| 181 |