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