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 |
||
| 11 | class Formatter implements FormatTokens |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Map of tokens to formatting function names |
||
| 15 | */ |
||
| 16 | private const TOKEN_MAP = [ |
||
| 17 | self::TOKEN_DATE_CENTURY => 'formatCentury', |
||
| 18 | self::TOKEN_SERIAL_PRE => 'formatSerialPre', |
||
| 19 | self::TOKEN_SERIAL_POST => 'formatSerialPost', |
||
| 20 | self::TOKEN_DELIMITER => 'formatDelimiter', |
||
| 21 | self::TOKEN_CHECK_DIGIT => 'formatCheckDigit', |
||
| 22 | self::TOKEN_SEX => 'formatSex', |
||
| 23 | self::TOKEN_AGE => 'formatAge', |
||
| 24 | self::TOKEN_LEGAL_FORM => 'formatLegalForm', |
||
| 25 | self::TOKEN_BIRTH_COUNTY => 'formatBirthCounty', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \Closure Formatting function, takes an Id object and returns a string |
||
| 30 | */ |
||
| 31 | private $formatter; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create formatter from format string |
||
| 35 | */ |
||
| 36 | public function __construct(string $format = '') |
||
| 89 | 6 | ||
| 90 | /** |
||
| 91 | * Format id using registered formatting functions |
||
| 92 | */ |
||
| 93 | public function format(IdInterface $idObject): string |
||
| 97 | |||
| 98 | /** |
||
| 99 | 6 | * Function must take an Id object and return a string |
|
| 100 | * |
||
| 101 | 6 | * @param mixed $formatter |
|
| 102 | 6 | */ |
|
| 103 | 6 | private function registerFormattingFunction($formatter): void |
|
| 114 | |||
| 115 | 6 | private function formatCentury(IdInterface $idObject): string |
|
| 123 | |||
| 124 | private function formatSerialPre(IdInterface $idObject): string |
||
| 128 | |||
| 129 | private function formatSerialPost(IdInterface $idObject): string |
||
| 133 | |||
| 134 | private function formatDelimiter(IdInterface $idObject): string |
||
| 138 | 1 | ||
| 139 | private function formatCheckDigit(IdInterface $idObject): string |
||
| 143 | |||
| 144 | private function formatSex(IdInterface $idObject): string |
||
| 148 | |||
| 149 | 1 | private function formatAge(IdInterface $idObject): string |
|
| 153 | |||
| 154 | private function formatLegalForm(IdInterface $idObject): string |
||
| 158 | 1 | ||
| 159 | private function formatBirthCounty(IdInterface $idObject): string |
||
| 163 | } |
||
| 164 |