Complex classes like AliasRecord 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 AliasRecord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class AliasRecord extends Config implements AliasRecordInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $name; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @inheritdoc |
||
| 23 | */ |
||
| 24 | public function __construct(array $data = null, $name = '', $env = '') |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @inheritdoc |
||
| 35 | */ |
||
| 36 | public function getConfig(ConfigInterface $config, $key, $default = null) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritdoc |
||
| 46 | */ |
||
| 47 | public function name() |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritdoc |
||
| 54 | */ |
||
| 55 | public function setName($name) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | public function hasRoot() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | * |
||
| 71 | * @throws \Exception when the alias does not specify a root. |
||
| 72 | */ |
||
| 73 | public function root() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @inheritdoc |
||
| 87 | */ |
||
| 88 | public function uri() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @inheritdoc |
||
| 95 | */ |
||
| 96 | public function setUri($uri) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritdoc |
||
| 103 | */ |
||
| 104 | public function remoteHostWithUser() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @inheritdoc |
||
| 115 | */ |
||
| 116 | public function remoteUser() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritdoc |
||
| 123 | */ |
||
| 124 | public function hasRemoteUser() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @inheritdoc |
||
| 131 | */ |
||
| 132 | public function remoteHost() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritdoc |
||
| 139 | */ |
||
| 140 | public function isRemote() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @inheritdoc |
||
| 147 | */ |
||
| 148 | public function isLocal() |
||
| 149 | { |
||
| 150 | return !$this->isRemote() && !$this->isContainer(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @inheritdoc |
||
| 155 | */ |
||
| 156 | public function isContainer() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | */ |
||
| 164 | public function isNone() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritdoc |
||
| 171 | */ |
||
| 172 | public function localRoot() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * os returns the OS that this alias record points to. For local alias |
||
| 183 | * records, PHP_OS will be returned. For remote alias records, the |
||
| 184 | * value from the `os` element will be returned. If there is no `os` |
||
| 185 | * element, then the default assumption is that the remote system is Linux. |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | * Linux |
||
| 189 | * WIN* (e.g. WINNT) |
||
| 190 | * CYGWIN |
||
| 191 | * MINGW* (e.g. MINGW32) |
||
| 192 | */ |
||
| 193 | public function os() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @inheritdoc |
||
| 203 | */ |
||
| 204 | public function exportConfig() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Reconfigure data exported from the form it is expected to be in |
||
| 211 | * inside an alias record to the form it is expected to be in when |
||
| 212 | * inside a configuration file. |
||
| 213 | */ |
||
| 214 | protected function remap($data) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Fetch the parameter-specific options from the 'alias-parameters' section of the alias. |
||
| 231 | * @param string $parameterName |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | protected function getParameterSpecificOptions($aliasData, $parameterName) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Convert the data in this record to the layout that was used |
||
| 244 | * in the legacy code, for backwards compatiblity. |
||
| 245 | */ |
||
| 246 | public function legacyRecord() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Conversion table from old to new option names. These all implicitly |
||
| 262 | * go in `options`, although they can come from different locations. |
||
| 263 | */ |
||
| 264 | protected function remapOptionTable() |
||
| 275 | } |
||
| 276 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.