Complex classes like Options 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 Options, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class Options |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * The String that marks a word not to be hyphenated. |
||
| 57 | * |
||
| 58 | * This string has to be prepend to the word in question |
||
| 59 | * |
||
| 60 | * @var string _noHyphenateString |
||
| 61 | */ |
||
| 62 | protected $_noHyphenateString = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * This property defines the default hyphenation-character. |
||
| 66 | * |
||
| 67 | * By default this is the soft-hyphen character U+00AD |
||
| 68 | * |
||
| 69 | * @var string $_hyphen |
||
| 70 | */ |
||
| 71 | protected $_hyphen = "\xAD"; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * How many chars to stay to the left of the first hyphenation of a word. |
||
| 75 | * |
||
| 76 | * By default this is 2 characters |
||
| 77 | * |
||
| 78 | * @var int $_leftmin |
||
| 79 | */ |
||
| 80 | protected $_leftMin = 2; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * How many chars to stay to the right of the last hyphenation of a word. |
||
| 84 | * |
||
| 85 | * By default this is 2 characters |
||
| 86 | * |
||
| 87 | * @var int $_rightmin |
||
| 88 | */ |
||
| 89 | protected $_rightMin = 2; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Minimum Word length for Hyphenation. |
||
| 93 | * |
||
| 94 | * This defaults to 6 Characters. |
||
| 95 | * |
||
| 96 | * @var int $_wordMin |
||
| 97 | */ |
||
| 98 | protected $_wordMin = 6; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The currently set quality for hyphenation. |
||
| 102 | * |
||
| 103 | * The higher the number, the better the hyphenation is |
||
| 104 | * |
||
| 105 | * @var int $_quality |
||
| 106 | */ |
||
| 107 | protected $_quality = 9; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * The String that shall be searched for as a customHyphen. |
||
| 111 | * |
||
| 112 | * @var string $_customHyphen |
||
| 113 | */ |
||
| 114 | protected $_customHyphen = '--'; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The filters to be used to postprocess the hyphenations. |
||
| 118 | * |
||
| 119 | * @var array $_filters |
||
| 120 | */ |
||
| 121 | protected $_filters = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * The tokenizers to use. |
||
| 125 | * |
||
| 126 | * @var array $_tokenizers |
||
| 127 | */ |
||
| 128 | protected $_tokenizers = array(); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * THe locale to be used. |
||
| 132 | * |
||
| 133 | * @var string $_locale |
||
| 134 | */ |
||
| 135 | protected $_defaultLocale = 'en_EN'; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set the String that marks a word as not to be hyphenated |
||
| 139 | * |
||
| 140 | * @param string $noHyphenateString The string that marks a word not to be |
||
| 141 | * hyphenated |
||
| 142 | * |
||
| 143 | * @return \Org\Heigl\Hyphenator\Options |
||
| 144 | */ |
||
| 145 | public function setNoHyphenateString($noHyphenateString) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the String that marks a word as not to be hyphenated |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getNoHyphenateString() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the hyphen-string |
||
| 164 | * |
||
| 165 | * @param string $hyphen The hyphen to use |
||
| 166 | * |
||
| 167 | * @return \Org\Heigl\Hyphenator\Options |
||
| 168 | */ |
||
| 169 | public function setHyphen($hyphen) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the hyphen-string |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function getHyphen() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set the Minimum left characters |
||
| 188 | * |
||
| 189 | * @param int $leftMin Left minimum Chars |
||
| 190 | * |
||
| 191 | * @return \Org\Heigl\Hyphenator\Options |
||
| 192 | */ |
||
| 193 | public function setLeftMin($leftMin) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the minimum left characters |
||
| 202 | * |
||
| 203 | * @return int |
||
| 204 | */ |
||
| 205 | public function getLeftMin() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set the Minimum right characters |
||
| 212 | * |
||
| 213 | * @param int $rightMin Right minimum Characters |
||
| 214 | * |
||
| 215 | * @return \Org\Heigl\Hyphenator\Options |
||
| 216 | */ |
||
| 217 | public function setRightMin($rightMin) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the minimum right characters |
||
| 226 | * |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function getRightMin() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Set the minimum size of a word to be hyphenated |
||
| 236 | * |
||
| 237 | * Words with less characters (not byte!) are not to be hyphenated |
||
| 238 | * |
||
| 239 | * @param int $minLength Minimum Word-Length |
||
| 240 | * |
||
| 241 | * @return \Org\Heigl\Hyphenator\Options |
||
| 242 | */ |
||
| 243 | public function setMinWordLength($minLength) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * This is a wrapper for setMinWordLength |
||
| 252 | * |
||
| 253 | * @param int $wordLength The minimum word Length |
||
| 254 | * |
||
| 255 | * @return \Org\Heigl\Hyphenator\Options |
||
| 256 | */ |
||
| 257 | public function setWordMin($wordLength) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the hyphenation quality |
||
| 264 | * |
||
| 265 | * @param int $quality The new Hyphenation Quality |
||
| 266 | * |
||
| 267 | * @return \Org\Heigl\Hyphenator\Options |
||
| 268 | */ |
||
| 269 | public function setQuality($quality) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the hyphenation-quality |
||
| 278 | * |
||
| 279 | * @return int |
||
| 280 | */ |
||
| 281 | public function getQuality() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the minimum Length of a word to be hyphenated |
||
| 288 | * |
||
| 289 | * @return int |
||
| 290 | */ |
||
| 291 | public function getMinWordLength() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the string that is treated as a custom Hyphenation |
||
| 298 | * |
||
| 299 | * These will be replaced by the set hyphenation character |
||
| 300 | * |
||
| 301 | * @param array $customHyphen The custom hyphenation-character |
||
| 302 | * |
||
| 303 | * @return \Org\Heigl\Hyphenator\Options |
||
| 304 | */ |
||
| 305 | public function setCustomHyphen($customHyphen) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the custom Hyphen |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function getCustomHyphen() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set the filters |
||
| 324 | * |
||
| 325 | * @param string|array $filters The filters to use as comma separated list |
||
| 326 | * or array |
||
| 327 | * |
||
| 328 | * @return \Org\Heigl\Hyphenator\Options |
||
| 329 | */ |
||
| 330 | public function setFilters($filters) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Add a filter to the options-array |
||
| 345 | * |
||
| 346 | * @param string|Filter $filter The filter to be added |
||
| 347 | * |
||
| 348 | * @throws \UnexpectedValueException |
||
| 349 | * @return \Org\Heigl\Hyphenator\Options |
||
| 350 | */ |
||
| 351 | public function addFilter($filter) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get all the filters |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | public function getFilters() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Set the tokenizers to use |
||
| 378 | * |
||
| 379 | * @param string|array $tokenizers The Tokenizers to use |
||
| 380 | * |
||
| 381 | * @return \Org\Heigl\Hyphenator\Options |
||
| 382 | */ |
||
| 383 | public function setTokenizers($tokenizers) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Add a tokenizer to the tomeizer-list |
||
| 398 | * |
||
| 399 | * @param string|Tokenizer $tokenizer The tokenizer to add |
||
| 400 | * |
||
| 401 | * @return \Org\Heigl\Hyphenator\Options |
||
| 402 | */ |
||
| 403 | public function addTokenizer($tokenizer) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get all the tokenizers |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | public function getTokenizers() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Create an Option-Object by parsing a given file. |
||
| 431 | * |
||
| 432 | * @param string $file The config-file to be parsed |
||
| 433 | * |
||
| 434 | * @throws \Org\Heigl\Hyphenator\Exception\PathNotFoundException |
||
| 435 | * @throws \Org\Heigl\Hyphenator\Exception\InvalidArgumentException |
||
| 436 | * @return \Org\Heigl\Hyphenator\Options |
||
| 437 | */ |
||
| 438 | public static function factory($file) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set the default locale for this instance |
||
| 464 | * |
||
| 465 | * @param string $locale The locale to be set |
||
| 466 | * |
||
| 467 | * @return \Org\Heigl\Hyphenator\Options |
||
| 468 | */ |
||
| 469 | public function setDefaultLocale($locale) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Get the default locale for this instance |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getDefaultLocale() |
||
| 485 | } |
||
| 486 |