Complex classes like Localize 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 Localize, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Localize extends \Tight\Modules\AbstractModule |
||
| 35 | { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string Resource file type |
||
| 39 | */ |
||
| 40 | private $resourceFileType; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Current locale |
||
| 44 | */ |
||
| 45 | private $locale; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array Associative array for the current locale |
||
| 49 | */ |
||
| 50 | private $values; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * |
||
| 55 | * @param array|\Tight\Modules\Localize\LocalizeConfig $config |
||
| 56 | * @throws \InvalidArgumentException If $config is not an array or an object |
||
| 57 | * of \Tight\Modules\Localize\LocalizeConfig class |
||
| 58 | */ |
||
| 59 | public function __construct($config = []) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets the resource file type |
||
| 74 | * @param string $resourceFileType Resource file type |
||
| 75 | * @return \Tight\Modules\Localize\Localize Fluent setter |
||
| 76 | */ |
||
| 77 | public function setResourceFileType($resourceFileType) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Gets the current resource file type |
||
| 85 | * @return string Resource file type |
||
| 86 | * @see \Tight\Modules\Localize\LocalizeConfig::FILETYPE_INI |
||
| 87 | * @see \Tight\Modules\Localize\LocalizeConfig::FILETYPE_JSON |
||
| 88 | * @see \Tight\Modules\Localize\LocalizeConfig::FILETYPE_XML |
||
| 89 | */ |
||
| 90 | public function getResourceFileType() { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Gets all the defined values for the current locale |
||
| 96 | * @return array Associative array of values |
||
| 97 | */ |
||
| 98 | public function getValues() { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Reloads the class with the new locale |
||
| 104 | */ |
||
| 105 | public function reloadConfig() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Checks the dependences for the class |
||
| 123 | * @throws \Tight\Exception\ModuleException If resource directory cant be |
||
| 124 | * found |
||
| 125 | */ |
||
| 126 | private function checkDependences() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets a new locale |
||
| 134 | * @param string $locale New defined locale |
||
| 135 | * @throws \Tight\Exception\ModuleException If resource default resource file |
||
| 136 | * cant be found |
||
| 137 | */ |
||
| 138 | public function setLocale($locale) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Gets the available locales |
||
| 145 | * @return array Available locales |
||
| 146 | */ |
||
| 147 | public function getLocales() { |
||
| 161 | |||
| 162 | private function getLocalesFromJson() { |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the json values from the json file |
||
| 192 | * @return array String values |
||
| 193 | * @throws \Tight\Exception\ModuleException If json file cant be found |
||
| 194 | */ |
||
| 195 | private function getValuesFromJson() { |
||
| 196 | $output = []; |
||
| 197 | $folder = \Tight\Utils::addTrailingSlash($this->getConfig()->resourceFolder); |
||
| 198 | $fileName = $this->getConfig()->resourceFileName . $this->getConfig()->langSeparator . $this->locale . "." . $this->getConfig()->resourceFileType; |
||
| 199 | $file = $folder . $fileName; |
||
| 200 | if (is_file($file)) { |
||
| 201 | $output = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
||
| 202 | } else { |
||
| 203 | $fileName = $this->getConfig()->resourceFileName . "." . $this->getConfig()->resourceFileType; |
||
| 204 | $file = $folder . $fileName; |
||
| 205 | if (is_file($file)) { |
||
| 206 | $output = json_decode(file_get_contents($file), JSON_FORCE_OBJECT); |
||
| 207 | } else { |
||
| 208 | throw new \Tight\Exception\ModuleException("Resource file <strong>" . $file . "</strong> not found"); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | return $output; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the locales from the xml resource file |
||
| 216 | * @return array Array of locales |
||
| 217 | */ |
||
| 218 | private function getLocalesFromXml() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Gets the xml resource read from the xml resource file |
||
| 230 | * @return \SimpleXMLElement XML values for the current locale |
||
| 231 | * @throws \Tight\Exception\ModuleException If xml values resource file cant be found |
||
| 232 | */ |
||
| 233 | private function getXmlResources() { |
||
| 234 | $output = null; |
||
| 235 | if ($this->resourceFileType === LocalizeConfig::FILETYPE_XML) { |
||
| 236 | $file = \Tight\Utils::addTrailingSlash($this->getConfig()->resourceFolder) . $this->getConfig()->resourceFileName . "." . $this->resourceFileType; |
||
| 237 | if (is_file($file)) { |
||
| 238 | $xmlContent = file_get_contents($file); |
||
| 239 | $output = new \SimpleXMLElement($xmlContent); |
||
| 240 | } else { |
||
| 241 | throw new \Tight\Exception\ModuleException("Resource file <strong>" . $file . "</strong> does not exists"); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | return $output; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Gets the values from xml resource file |
||
| 249 | * @return array Array of values |
||
| 250 | * @throws \Tight\Exception\ModuleException If the current locale cant be |
||
| 251 | * found in the xml resource file |
||
| 252 | */ |
||
| 253 | private function getValuesFromXml() { |
||
| 254 | $resources = $this->getXmlResources(); |
||
| 255 | $locale = null; |
||
| 256 | $size = count($resources->values); |
||
| 257 | $index = 0; |
||
| 258 | while ($index < $size && $locale == null) { |
||
| 259 | if ($resources->values[$index]['lang'] == $this->locale) { |
||
| 260 | $locale = $resources->values[$index]; |
||
| 261 | } |
||
| 262 | $index++; |
||
| 263 | } |
||
| 264 | if (null == $locale) { |
||
| 265 | $fileName = $this->getConfig()->resourceFileName . "." . $this->getConfig()->resourceFileType; |
||
| 266 | throw new \Tight\Exception\ModuleException("Locale <strong>" . $this->locale . "</strong> not found at resource file <strong>" . $fileName . "</strong>"); |
||
| 267 | } |
||
| 268 | return $this->parseXmlStringValues($locale); |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Parses a \SimpleXmlElement object to get its values |
||
| 273 | * @param \SimpleXMLElement $xml XML object |
||
| 274 | * @return array Values |
||
| 275 | */ |
||
| 276 | private function parseXmlStringValues(\SimpleXMLElement $xml) { |
||
| 277 | $output = []; |
||
| 278 | if ($xml->count() > 0) { |
||
| 279 | $children = $xml->children(); |
||
| 280 | $size = count($children); |
||
| 281 | for ($index = 0; $index < $size; $index++) { |
||
| 282 | // If xml tag is <array>, parse without merge |
||
| 283 | if ($children[$index]->getName() === "array") { |
||
| 284 | $key = $children[$index]['name']; |
||
| 285 | if (null !== $key) { |
||
| 286 | $output[$key->__toString()] = $this->parseXmlStringValues($children[$index]); |
||
| 287 | } |
||
| 288 | } else { |
||
| 289 | $output = array_merge($output, $this->parseXmlStringValues($children[$index])); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } else { |
||
| 293 | $key = $xml['name']; |
||
| 294 | if (null !== $key) { |
||
| 295 | $output[$key->__toString()] = $xml->__toString(); |
||
| 296 | } |
||
| 297 | } |
||
| 298 | return $output; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Gets a value from a defined key |
||
| 303 | * @param string $key Key |
||
| 304 | * @return string Value defined for the key $key or an empty string if the |
||
| 305 | * key is not defined |
||
| 306 | */ |
||
| 307 | public function get($key) { |
||
| 314 | |||
| 315 | public function onConfigChange() { |
||
| 318 | |||
| 319 | public function onLoad() { |
||
| 322 | |||
| 323 | public function onRemove() { |
||
| 326 | |||
| 327 | } |
||
| 328 |