Complex classes like Formatter 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 Formatter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Formatter |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var Tokenizer |
||
| 30 | */ |
||
| 31 | protected $tokenizer; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var NewLine |
||
| 35 | */ |
||
| 36 | protected $newLine; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Parentheses |
||
| 40 | */ |
||
| 41 | protected $parentheses; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $tab = ' '; |
||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $inlineCount = 0; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $clauseLimit = false; |
||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $formattedSql = ''; |
||
| 60 | /** |
||
| 61 | * @var Indent |
||
| 62 | */ |
||
| 63 | protected $indentation; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Comment |
||
| 67 | */ |
||
| 68 | protected $comment; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns a SQL string in a readable human-friendly format. |
||
| 72 | * |
||
| 73 | * @param string $sql |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | public function format($sql) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * |
||
| 145 | */ |
||
| 146 | public function reset() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param $token |
||
| 159 | * @param $i |
||
| 160 | * @param array $tokens |
||
| 161 | * @param array $originalTokens |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | protected function formatOpeningParenthesis($token, $i, array &$tokens, array &$originalTokens) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param $token |
||
| 198 | * |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | protected function stringIsEndOfLimitClause($token) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param bool $addedNewline |
||
| 211 | * @param string $tab |
||
| 212 | * @param $token |
||
| 213 | * @param $queryValue |
||
| 214 | * |
||
| 215 | * @return mixed |
||
| 216 | */ |
||
| 217 | protected function formatTokenTypeReservedTopLevel($addedNewline, $tab, $token, $queryValue) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param $token |
||
| 235 | * @param $i |
||
| 236 | * @param array $tokens |
||
| 237 | * @param array $originalTokens |
||
| 238 | */ |
||
| 239 | protected function formatBoundaryCharacterToken($token, $i, array &$tokens, array &$originalTokens) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param $token |
||
| 248 | * @param $queryValue |
||
| 249 | */ |
||
| 250 | protected function formatWhiteSpaceToken($token, $queryValue) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $token |
||
| 265 | * @param $i |
||
| 266 | * @param array $tokens |
||
| 267 | */ |
||
| 268 | protected function formatDashToken($token, $i, array &$tokens) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getFormattedSql() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $formattedSql |
||
| 289 | * |
||
| 290 | * @return $this |
||
| 291 | */ |
||
| 292 | public function setFormattedSql($formattedSql) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param $string |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function appendToFormattedSql($string) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return int |
||
| 313 | */ |
||
| 314 | public function getInlineCount() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param int $inlineCount |
||
| 321 | * |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setInlineCount($inlineCount) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | public function getClauseLimit() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param bool $clauseLimit |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function setClauseLimit($clauseLimit) |
||
| 350 | } |
||
| 351 |