Complex classes like PoEntry 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 PoEntry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class PoEntry |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array $entry |
||
| 21 | */ |
||
| 22 | protected $entry = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * establish an empty entry |
||
| 26 | */ |
||
| 27 | 32 | public function __construct() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * add a value to an array 'type' in the entry |
||
| 43 | * |
||
| 44 | * @param string $type PoToken constant |
||
| 45 | * @param string $value value to store |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | 25 | public function add(string $type, string $value): void |
|
| 56 | |||
| 57 | /** |
||
| 58 | * add a quoted value to the array 'type' in the entry |
||
| 59 | * |
||
| 60 | * @param string $type PoToken constant |
||
| 61 | * @param string $value value to store |
||
| 62 | * @return void |
||
| 63 | */ |
||
| 64 | 7 | public function addQuoted(string $type, string $value): void |
|
| 77 | |||
| 78 | /** |
||
| 79 | * add a quoted value to the nested array 'type' in the entry |
||
| 80 | * |
||
| 81 | * This is mainly useful for translated plurals. Since any plural msgstr can have |
||
| 82 | * continuation lines, the message is stored as an array of arrays. |
||
| 83 | * |
||
| 84 | * @param string $type PoToken constant |
||
| 85 | * @param integer $position array position to store |
||
| 86 | * @param string $value value to store |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | 7 | public function addQuotedAtPosition(string $type, int $position, string $value): void |
|
| 106 | |||
| 107 | /** |
||
| 108 | * get the value for a specified type |
||
| 109 | * |
||
| 110 | * @param string $type PoToken constant |
||
| 111 | * |
||
| 112 | * @return string|string[]|null |
||
| 113 | */ |
||
| 114 | 10 | public function get(string $type) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * get the value of a specified type as a string |
||
| 121 | * |
||
| 122 | * @param string $type PoToken constant |
||
| 123 | * @return string|null |
||
| 124 | */ |
||
| 125 | 18 | public function getAsString(string $type): ?string |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Get the value of a specified type as an array of strings. This is |
||
| 136 | * mainly for plural TRANSLATED messages. |
||
| 137 | * |
||
| 138 | * @param string $type PoToken constant |
||
| 139 | * |
||
| 140 | * @return string[]|null |
||
| 141 | */ |
||
| 142 | 2 | public function getAsStringArray(string $type): ?array |
|
| 155 | |||
| 156 | /** |
||
| 157 | * set the value of a specified type |
||
| 158 | * |
||
| 159 | * @param string $type PoToken constant |
||
| 160 | * @param string|string[]|null $value value to set |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | 28 | public function set(string $type, $value): void |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Dump this entry as a po/pot file fragment |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 7 | public function dumpEntry(): string |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Dump the comments for this entry as a po/pot file fragment |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 7 | protected function dumpEntryComments(): string |
|
| 234 | |||
| 235 | /** |
||
| 236 | * format a string for output by escaping control and double quote |
||
| 237 | * characters, then surrounding with double quotes |
||
| 238 | * |
||
| 239 | * @param string|string[]|null $value string to prepare |
||
| 240 | * @param boolean $bare true for bare output, default false adds leading |
||
| 241 | * space and trailing newline |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | 7 | protected function formatQuotedString($value, bool $bare = false): string |
|
| 261 | |||
| 262 | /** |
||
| 263 | * check for presence of a flag |
||
| 264 | * |
||
| 265 | * @param string $name flag to check |
||
| 266 | * |
||
| 267 | * @return boolean true if flag is set, otherwise false |
||
| 268 | */ |
||
| 269 | 4 | public function hasFlag(string $name): bool |
|
| 285 | |||
| 286 | /** |
||
| 287 | * add a flag to the entry |
||
| 288 | * |
||
| 289 | * @param string $name flag to check |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 4 | public function addFlag(string $name): void |
|
| 307 | } |
||
| 308 |