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 | 37 | 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 | 16 | public static function createKey(?string $msgid, ?string $msgctxt = null, ?string $msgid_plural = null): string |
|
73 | { |
||
74 | 16 | $key = ''; |
|
75 | 16 | if (!empty($msgctxt)) { |
|
76 | 4 | $key .= $msgctxt . '|'; |
|
77 | } |
||
78 | 16 | $key .= (string) $msgid; |
|
79 | 16 | if (!empty($msgid_plural)) { |
|
80 | 9 | $key .= '|' . $msgid_plural; |
|
81 | } |
||
82 | 16 | return $key; |
|
83 | } |
||
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 | 15 | public function createKeyFromEntry(PoEntry $entry): string |
|
93 | { |
||
94 | 15 | return $this->createKey( |
|
95 | 15 | $entry->getAsString(PoTokens::MESSAGE), |
|
96 | 15 | $entry->getAsString(PoTokens::CONTEXT), |
|
97 | 15 | $entry->getAsString(PoTokens::PLURAL) |
|
98 | ); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Replace any existing header with the provided PoHeader |
||
103 | * |
||
104 | * @param PoHeader $header header object |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | 7 | 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 | 10 | 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 | 8 | 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 | 6 | public function mergeEntry(PoEntry $newEntry): bool |
|
195 | { |
||
196 | 6 | $key = $this->createKeyFromEntry($newEntry); |
|
197 | |||
198 | // keyed entries only |
||
199 | 6 | if (empty($key)) { |
|
200 | 1 | return false; |
|
201 | } |
||
202 | |||
203 | 6 | if (isset($this->entries[$key])) { |
|
204 | 2 | $existingEntry = $this->entries[$key]; |
|
205 | 2 | $mergeTokens = array(PoTokens::REFERENCE, PoTokens::EXTRACTED_COMMENTS); |
|
206 | 2 | foreach ($mergeTokens as $type) { |
|
207 | 2 | $toMerge = $newEntry->get($type); |
|
208 | 2 | if (!empty($toMerge)) { |
|
209 | 2 | $toMerge = is_array($toMerge) ? $toMerge : array($toMerge); |
|
210 | 2 | foreach ($toMerge as $value) { |
|
211 | 2 | $existingEntry->add($type, $value); |
|
212 | } |
||
213 | } |
||
214 | } |
||
215 | } else { |
||
216 | 6 | $this->entries[$key] = $newEntry; |
|
217 | } |
||
218 | 6 | return true; |
|
219 | } |
||
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 |
|
231 | { |
||
232 | 2 | $key = $this->createKey($msgid, $msgctxt, $msgid_plural); |
|
233 | 2 | $entry = null; |
|
234 | |||
235 | 2 | if (!empty($key) && isset($this->entries[$key])) { |
|
236 | 2 | $entry = $this->entries[$key]; |
|
237 | } |
||
238 | |||
239 | 2 | return $entry; |
|
240 | } |
||
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 |
|
258 | { |
||
259 | 1 | $key = $this->createKeyFromEntry($entry); |
|
260 | |||
261 | // try by the key first. |
||
262 | 1 | if (!empty($key) && isset($this->entries[$key])) { |
|
263 | 1 | if ($entry === $this->entries[$key]) { |
|
264 | 1 | unset($this->entries[$key]); |
|
265 | 1 | return true; |
|
266 | } |
||
267 | } |
||
268 | |||
269 | // the entry can't be matched by key, so we have to loop :( |
||
270 | 1 | foreach ($this->entries as $key => $value) { |
|
271 | 1 | if ($entry === $value) { |
|
272 | 1 | unset($this->entries[$key]); |
|
273 | 1 | return true; |
|
274 | } |
||
275 | } |
||
276 | |||
277 | // no match found in main entries, try the unkeyedEntries |
||
278 | 1 | foreach ($this->unkeyedEntries as $key => $value) { |
|
279 | 1 | if ($entry === $value) { |
|
280 | 1 | unset($this->unkeyedEntries[$key]); |
|
281 | 1 | return true; |
|
282 | } |
||
283 | } |
||
284 | |||
285 | 1 | return false; |
|
286 | } |
||
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 |
|
298 | { |
||
299 | 2 | $source = $this->dumpString(); |
|
300 | 2 | $testName = file_exists($file) ? $file : dirname($file); |
|
301 | 2 | $status = is_writable($testName); |
|
302 | 2 | if ($status === true) { |
|
303 | 1 | $status = file_put_contents($file, $source); |
|
304 | } |
||
305 | 2 | if (false === $status) { |
|
306 | 1 | throw new FileNotWritableException($file); |
|
307 | } |
||
308 | 1 | } |
|
309 | |||
310 | /** |
||
311 | * Dump the current contents in PO format to a string |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 5 | public function dumpString(): string |
|
316 | { |
||
317 | 5 | if ($this->header === null) { |
|
318 | 2 | $this->header = new PoHeader; |
|
319 | 2 | $this->header->buildDefaultHeader(); |
|
320 | } |
||
321 | 5 | $output = ''; |
|
322 | |||
323 | 5 | $output .= $this->header->dumpEntry(); |
|
324 | 5 | foreach ($this->entries as $entry) { |
|
325 | 3 | $output .= $entry->dumpEntry(); |
|
326 | } |
||
327 | 5 | foreach ($this->unkeyedEntries as $entry) { |
|
328 | 2 | $output .= $entry->dumpEntry(); |
|
329 | } |
||
330 | 5 | $output .= "\n"; |
|
331 | |||
332 | 5 | return $output; |
|
333 | } |
||
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, ?resource $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 | 6 | public function parsePoSource(string $source): void |
|
477 | } |
||
478 |