Complex classes like SubdomainsInstallShell 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 SubdomainsInstallShell, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class SubdomainsInstallShell extends Shell { |
||
| 22 | |||
| 23 | public function main() { |
||
| 24 | |||
| 25 | $this->clear(); |
||
| 26 | |||
| 27 | $this->helper('Multidimensional/Subdomains.Header')->output(); |
||
| 28 | |||
| 29 | $subdomains = $this->_getSubdomains(); |
||
| 30 | |||
| 31 | if ($this->_countSubdomains($subdomains)) { |
||
| 32 | $continue = $this->_inputYesNo('Update configuration?'); |
||
| 33 | } else { |
||
| 34 | $continue = $this->_inputYesNo('Install subdomains plugin?'); |
||
| 35 | } |
||
| 36 | |||
| 37 | if ($continue) { |
||
| 38 | |||
| 39 | do { |
||
| 40 | |||
| 41 | $addMore = true; |
||
| 42 | |||
| 43 | if ($this->_countSubdomains($subdomains)) { |
||
| 44 | $this->_displayCurrentUniqueSubdomains($subdomains); |
||
| 45 | $addMore = $this->_inputYesNo('Add a subdomain?'); |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($addMore) { |
||
| 49 | |||
| 50 | if (!$this->_countSubdomains($subdomains)) { |
||
| 51 | $this->out(); |
||
| 52 | $this->out('Add your first subdomain.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->_inputSubdomain($subdomains); |
||
| 56 | |||
| 57 | } |
||
| 58 | |||
| 59 | $this->_deleteSubdomain($subdomains); |
||
| 60 | $this->_writeConfig($subdomains); |
||
| 61 | $this->_finalCheck($subdomains); |
||
| 62 | |||
| 63 | } while (!$this->_countSubdomains($subdomains) && $this->_inputYesNo('Start over?')); |
||
| 64 | |||
| 65 | $this->_displayFinal($subdomains); |
||
| 66 | |||
| 67 | } |
||
| 68 | |||
| 69 | } |
||
| 70 | |||
| 71 | private function _displayCurrentUniqueSubdomains(&$subdomains) { |
||
| 72 | |||
| 73 | $subdomains = $this->_uniqueSubdomains($subdomains); |
||
| 74 | $subdomains = $this->_modifyArray($subdomains); |
||
| 75 | $this->_displayCurrentSubdomains($subdomains); |
||
| 76 | |||
| 77 | } |
||
| 78 | |||
| 79 | private function _inputSubdomain(&$subdomains) { |
||
| 80 | |||
| 81 | do { |
||
| 82 | |||
| 83 | $this->out(); |
||
| 84 | $subdomain = strtolower($this->in('Subdomain:')); |
||
| 85 | $valid = $this->_validateSubdomain($subdomain); |
||
| 86 | $this->out(); |
||
| 87 | |||
| 88 | if ($valid) { |
||
| 89 | $subdomains[] = $subdomain; |
||
| 90 | } else { |
||
| 91 | $this->err('Invalid subdomain.'); |
||
| 92 | } |
||
| 93 | |||
| 94 | } while (!$valid || $this->_inputYesNo('Add a subdomain?')); |
||
| 95 | |||
| 96 | } |
||
| 97 | |||
| 98 | private function _uniqueSubdomains($subdomains) { |
||
| 103 | |||
| 104 | private function _writeConfig($subdomains) { |
||
| 105 | |||
| 106 | Configure::write('Multidimensional/Subdomains.subdomains', array_values($subdomains)); |
||
| 107 | Configure::dump('subdomains', 'default', ['Multidimensional/Subdomains']); |
||
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | private function _displayFinal($subdomains) { |
||
| 112 | |||
| 113 | $this->out(); |
||
| 114 | if ($this->_countSubdomains($subdomains)) { |
||
| 115 | $this->out('Configuration saved!', 2); |
||
| 116 | } else { |
||
| 117 | $this->err('Plugin not currently active.', 2); |
||
| 118 | } |
||
| 119 | |||
| 120 | } |
||
| 121 | |||
| 122 | private function _finalCheck($subdomains) { |
||
| 123 | |||
| 124 | if (!$this->_countSubdomains($subdomains)) { |
||
| 125 | $this->out(); |
||
| 126 | $this->err('No subdomains configured.', 2); |
||
| 127 | } |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | private function _getSubdomains() { |
||
| 132 | |||
| 133 | $check = Configure::check('Multidimensional/Subdomains.subdomains'); |
||
| 134 | |||
| 135 | if (!$check) { |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | $subdomains = Configure::consume('Multidimensional/Subdomains.subdomains'); |
||
| 140 | |||
| 141 | if ($this->_countSubdomains($subdomains)) { |
||
| 142 | return $subdomains; |
||
| 143 | } |
||
| 144 | |||
| 145 | return false; |
||
| 146 | |||
| 147 | } |
||
| 148 | |||
| 149 | private function _modifyArray(array $array) { |
||
| 150 | |||
| 151 | return array_combine(range(1, count($array)), array_values($array)); ; |
||
|
|
|||
| 152 | |||
| 153 | } |
||
| 154 | |||
| 155 | |||
| 156 | private function _displayCurrentSubdomains(array $subdomains) { |
||
| 157 | |||
| 158 | if ($this->_countSubdomains($subdomains)) { |
||
| 159 | |||
| 160 | $this->out(); |
||
| 161 | $this->out('Current subdomains:', 2); |
||
| 162 | |||
| 163 | foreach ($subdomains AS $key => $value) { |
||
| 164 | $this->out(' ' . ($key) . '. ' . $value); |
||
| 165 | } |
||
| 166 | |||
| 167 | $this->out(); |
||
| 168 | |||
| 169 | } |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $string |
||
| 175 | */ |
||
| 176 | private function _inputYesNo($string) { |
||
| 181 | |||
| 182 | private function _deleteSubdomain(&$subdomains) { |
||
| 183 | |||
| 184 | $this->_displayCurrentUniqueSubdomains($subdomains); |
||
| 185 | |||
| 186 | while ($this->_countSubdomains($subdomains) && $this->_inputYesNo('Delete a subdomain?')) { |
||
| 187 | |||
| 188 | $this->out(); |
||
| 189 | $key = (int) $this->in('Enter number to delete:', array_keys($subdomains)); |
||
| 190 | |||
| 200 | |||
| 201 | /** |
||
| 202 | * @param string|null $subdomain |
||
| 203 | */ |
||
| 204 | private function _validateSubdomain($subdomain) { |
||
| 213 | |||
| 214 | private function _countSubdomains($subdomains) { |
||
| 229 | |||
| 230 | } |
Let’s take a look at an example: