Complex classes like PoFile 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 PoFile, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class PoFile |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var PoHeader $header |
||
| 24 | */ |
||
| 25 | protected $header = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var PoEntry[] $entries |
||
| 29 | */ |
||
| 30 | protected $entries = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var PoEntry[] $unkeyedEntries |
||
| 34 | */ |
||
| 35 | protected $unkeyedEntries = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * $var array() $unrecognizedInput |
||
| 39 | * |
||
| 40 | * If any lines that cannot be processed are found when reading a po file, the |
||
| 41 | * unrecognized input will be recorded here, and an exception will be thrown. |
||
| 42 | * No interface is supplied, but this debug data is an array in the form: |
||
| 43 | * line number => input line |
||
| 44 | */ |
||
| 45 | public $unrecognizedInput = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Build a PoFile, empty or with provided entries |
||
| 49 | * |
||
| 50 | * @param PoHeader|null $header header object |
||
| 51 | * @param PoEntry[] $entries associative array po entries |
||
| 52 | * @param PoEntry[] $unkeyedEntries indexed array of po entries. Unkeyed entries |
||
| 53 | * are usually comment only entries, such as for |
||
| 54 | * obsolete entries. |
||
| 55 | */ |
||
| 56 | 39 | public function __construct(?array $header = null, array $entries = array(), array $unkeyedEntries = array()) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Build the internal entries array key from id, context and plural id |
||
| 65 | * |
||
| 66 | * @param string|null $msgid the untranslated message of the entry |
||
| 67 | * @param string|null $msgctxt the context of the entry, if any |
||
| 68 | * @param string|null $msgid_plural the untranslated plural message of the entry, if any |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 18 | public static function createKey(?string $msgid, ?string $msgctxt = null, ?string $msgid_plural = null): string |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Build an internal entries array key from a PoEntry |
||
| 87 | * |
||
| 88 | * @param PoEntry $entry the PoEntry to build key from |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 17 | public function createKeyFromEntry(PoEntry $entry): string |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Replace any existing header with the provided PoHeader |
||
| 103 | * |
||
| 104 | * @param PoHeader $header header object |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | 8 | public function setHeaderEntry(PoHeader $header): void |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Get the current header entry |
||
| 115 | * |
||
| 116 | * @return PoHeader |
||
| 117 | */ |
||
| 118 | 2 | public function getHeaderEntry(): PoHeader |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Get an array of current entries |
||
| 125 | * |
||
| 126 | * @return PoEntry[] |
||
| 127 | */ |
||
| 128 | 11 | public function getEntries(): array |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Replace any existing unkeyedEntries with new array of PoEntry objects |
||
| 135 | * |
||
| 136 | * @param PoEntry[] $entries po entries |
||
| 137 | * |
||
| 138 | * @return void |
||
| 139 | */ |
||
| 140 | 1 | public function setUnkeyedEntries(array $entries): void |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Get current array of unkeyed PoEntry objects |
||
| 147 | * |
||
| 148 | * @return PoEntry[] |
||
| 149 | */ |
||
| 150 | 2 | public function getUnkeyedEntries(): array |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Add an entry to the PoFile using internal key |
||
| 157 | * |
||
| 158 | * @param PoEntry $entry the PoEntry to add |
||
| 159 | * @param boolean $replace true to replace any existing entry matching this key, |
||
| 160 | * false to not change the PoFile for a duplicated key |
||
| 161 | * |
||
| 162 | * @return boolean true if added, false if not |
||
| 163 | */ |
||
| 164 | 9 | public function addEntry(PoEntry $entry, bool $replace = true): bool |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Merge an entry with any existing entry with the same key. If the key does |
||
| 185 | * not exist, add the entry, otherwise merge comments, references, and flags. |
||
| 186 | * |
||
| 187 | * This is intended for use in building a POT, where the handling of translated |
||
| 188 | * strings is not a factor. |
||
| 189 | * |
||
| 190 | * @param PoEntry $newEntry the PoEntry to merge |
||
| 191 | * |
||
| 192 | * @return boolean true if merged or added, false if not |
||
| 193 | */ |
||
| 194 | 7 | public function mergeEntry(PoEntry $newEntry): bool |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get an entry based on key values - msgid, msgctxt and msgid_plural |
||
| 223 | * |
||
| 224 | * @param string $msgid the untranslated message of the entry |
||
| 225 | * @param string|null $msgctxt the context of the entry, if any |
||
| 226 | * @param string|null $msgid_plural the untranslated plural message of the entry, if any |
||
| 227 | * |
||
| 228 | * @return PoEntry|null matching entry, or null if not found |
||
| 229 | */ |
||
| 230 | 2 | public function findEntry(string $msgid, ?string $msgctxt = null, ?string $msgid_plural = null): ?PoEntry |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Remove an entry from the PoFile |
||
| 244 | * |
||
| 245 | * In simple cases, the entry can be found by key. There are several cases |
||
| 246 | * where it is not that easy to locate the PoEntry to be removed: |
||
| 247 | * - the PoEntry was altered, making the generated and stored key different |
||
| 248 | * - the entry is not keyed and is in unkeyedEntries |
||
| 249 | * |
||
| 250 | * In any of these cases, we must loop thru the entry arrays looking for an |
||
| 251 | * exact object match, so the cost of the remove goes up |
||
| 252 | * |
||
| 253 | * @param PoEntry $entry the PoEntry to merge |
||
| 254 | * |
||
| 255 | * @return boolean true if remove, false if not |
||
| 256 | */ |
||
| 257 | 1 | public function removeEntry(PoEntry $entry): bool |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Write any current contents to a po file |
||
| 290 | * |
||
| 291 | * @param string $file po file to write |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | * |
||
| 295 | * @throws FileNotWritableException |
||
| 296 | */ |
||
| 297 | 2 | public function writePoFile(string $file): void |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Dump the current contents in PO format to a string |
||
| 312 | * |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 5 | public function dumpString(): string |
|
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Replace any current contents with entries from a file |
||
| 338 | * |
||
| 339 | * @param string $file po file/stream to read |
||
| 340 | * @param resource|null $context context for stream if required |
||
| 341 | * |
||
| 342 | * @return void |
||
| 343 | * |
||
| 344 | * @throws FileNotReadableException |
||
| 345 | */ |
||
| 346 | 6 | public function readPoFile(string $file, $context = null): void |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Replace any current contents with header and entries from PO souce string |
||
| 359 | * |
||
| 360 | * @param string $source po formatted string to parse |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | * |
||
| 364 | * @throws UnrecognizedInputException |
||
| 365 | */ |
||
| 366 | 7 | public function parsePoSource(string $source): void |
|
| 477 | } |
||
| 478 |