Complex classes like Parser 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 Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Parser |
||
46 | { |
||
47 | /** @var SourceHandler */ |
||
48 | protected $sourceHandler; |
||
49 | |||
50 | /** @var int */ |
||
51 | protected $lineNumber; |
||
52 | |||
53 | /** @var string */ |
||
54 | protected $property; |
||
55 | |||
56 | /** |
||
57 | * Reads and parses a string |
||
58 | * |
||
59 | * @param string $string po content |
||
60 | * |
||
61 | * @throws \Exception. |
||
62 | * @return Parser |
||
63 | */ |
||
64 | public static function parseString($string) |
||
71 | |||
72 | /** |
||
73 | * Reads and parses a file |
||
74 | * |
||
75 | * @param string $filePath |
||
76 | * |
||
77 | * @throws \Exception. |
||
78 | * @return Catalog |
||
79 | */ |
||
80 | public static function parseFile($filePath) |
||
86 | |||
87 | public function __construct(SourceHandler $sourceHandler) |
||
91 | |||
92 | /** |
||
93 | * Reads and parses strings of a .po file. |
||
94 | * |
||
95 | * @param SourceHandler . Optional |
||
96 | * |
||
97 | * @throws \Exception, \InvalidArgumentException, ParseException |
||
98 | * @return Catalog |
||
99 | */ |
||
100 | public function parse() |
||
101 | { |
||
102 | $catalog = new Catalog(); |
||
103 | $this->lineNumber = 0; |
||
104 | $entry = array(); |
||
105 | $this->mode = null; // current mode |
||
|
|||
106 | $this->property = null; // current property |
||
107 | |||
108 | // Flags |
||
109 | $headersFound = false; |
||
110 | |||
111 | while (!$this->sourceHandler->ended()) { |
||
112 | |||
113 | $line = trim($this->sourceHandler->getNextLine()); |
||
114 | |||
115 | if ($this->shouldIgnoreLine($line, $entry)) { |
||
116 | $this->lineNumber++; |
||
117 | continue; |
||
118 | } |
||
119 | |||
120 | if ($this->shouldCloseEntry($line, $entry)) { |
||
121 | if (!$headersFound && $this->isHeader($entry)) { |
||
122 | $headersFound = true; |
||
123 | $catalog->addHeaders( |
||
124 | $this->parseHeaders($entry['msgstr']) |
||
125 | ); |
||
126 | } else { |
||
127 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
128 | } |
||
129 | |||
130 | $entry = array(); |
||
131 | $this->property = null; |
||
132 | |||
133 | if (empty($line)) { |
||
134 | $this->lineNumber++; |
||
135 | continue; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $entry = $this->parseLine($line, $entry); |
||
140 | |||
141 | $this->lineNumber++; |
||
142 | continue; |
||
143 | } |
||
144 | $this->sourceHandler->close(); |
||
145 | |||
146 | // add final entry |
||
147 | if (count($entry)) { |
||
148 | $catalog->addEntry(EntryFactory::createFromArray($entry)); |
||
149 | } |
||
150 | |||
151 | return $catalog; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param string $line |
||
156 | * @param array $entry |
||
157 | * |
||
158 | * @return array |
||
159 | * @throws ParseException |
||
160 | */ |
||
161 | protected function parseLine($line, $entry) |
||
162 | { |
||
163 | $firstChar = strlen($line) > 0 ? $line[0] : ''; |
||
164 | |||
165 | switch ($firstChar) { |
||
166 | case '#': |
||
167 | $entry = $this->parseComment($line, $entry); |
||
168 | break; |
||
169 | |||
170 | case 'm': |
||
171 | $entry = $this->parseProperty($line, $entry); |
||
172 | break; |
||
173 | |||
174 | case '"': |
||
175 | $entry = $this->parseMultiline($line, $entry); |
||
176 | break; |
||
177 | } |
||
178 | |||
179 | return $entry; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param string $line |
||
184 | * @param array $entry |
||
185 | * |
||
186 | * @return array |
||
187 | * @throws ParseException |
||
188 | */ |
||
189 | protected function parseProperty($line, array $entry) |
||
190 | { |
||
191 | list($key, $value) = $this->getProperty($line); |
||
192 | |||
193 | if (!isset($entry[$key])) { |
||
194 | $entry[$key] = ''; |
||
195 | } |
||
196 | |||
197 | switch (true) { |
||
198 | case $key === 'msgctxt': |
||
199 | case $key === 'msgid': |
||
200 | case $key === 'msgid_plural': |
||
201 | case $key === 'msgstr': |
||
202 | $entry[$key] .= $this->unquote($value); |
||
203 | $this->property = $key; |
||
204 | break; |
||
205 | |||
206 | case strpos($key, 'msgstr[') !== false: |
||
207 | $entry[$key] .= $this->unquote($value); |
||
208 | $this->property = $key; |
||
209 | break; |
||
210 | |||
211 | default: |
||
212 | throw new ParseException(sprintf('Could not parse %s at line %d', $key, $this->lineNumber)); |
||
213 | } |
||
214 | |||
215 | return $entry; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $line |
||
220 | * @param array $entry |
||
221 | * |
||
222 | * @return array |
||
223 | * @throws ParseException |
||
224 | */ |
||
225 | protected function parseMultiline($line, $entry) |
||
226 | { |
||
227 | switch (true) { |
||
228 | case $this->property === 'msgctxt': |
||
229 | case $this->property === 'msgid': |
||
230 | case $this->property === 'msgid_plural': |
||
231 | case $this->property === 'msgstr': |
||
232 | case strpos($this->property, 'msgstr[') !== false: |
||
233 | $entry[$this->property] .= $this->unquote($line); |
||
234 | break; |
||
235 | |||
236 | default: |
||
237 | throw new ParseException( |
||
238 | sprintf('Error parsing property %s as multiline.', $this->property) |
||
239 | ); |
||
240 | } |
||
241 | |||
242 | return $entry; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param string $line |
||
247 | * @param array $entry |
||
248 | * |
||
249 | * @return array |
||
250 | * @throws ParseException |
||
251 | */ |
||
252 | protected function parseComment($line, $entry) |
||
253 | { |
||
254 | $comment = trim(substr($line, 0, 2)); |
||
255 | |||
256 | switch ($comment) { |
||
257 | case '#,': |
||
258 | $line = trim(substr($line, 2)); |
||
259 | $entry['flags'] = preg_split('/,\s*/', $line); |
||
260 | break; |
||
261 | |||
262 | case '#.': |
||
263 | $entry['ccomment'] = !isset($entry['ccomment']) ? array() : $entry['ccomment']; |
||
264 | $entry['ccomment'][] = trim(substr($line, 2)); |
||
265 | break; |
||
266 | |||
267 | |||
268 | case '#|': // Previous string |
||
269 | case '#~': // Old entry |
||
270 | case '#~|': // Previous string old |
||
271 | $mode = array( |
||
272 | '#|' => 'previous', |
||
273 | '#~' => 'obsolete', |
||
274 | '#~|' => 'previous-obsolete' |
||
275 | ); |
||
276 | |||
277 | $line = trim(substr($line, 2)); |
||
278 | $property = $mode[$comment]; |
||
279 | if (!isset($entry[$property])) { |
||
280 | $subEntry = array(); |
||
281 | } else { |
||
282 | $subEntry = $entry[$property]; |
||
283 | } |
||
284 | |||
285 | $subEntry = $this->parseLine($line, $subEntry); |
||
286 | //$subEntry = $this->parseProperty($line, $subEntry); |
||
287 | $entry[$property] = $subEntry; |
||
288 | break; |
||
289 | |||
290 | |||
291 | case '#': |
||
292 | default: |
||
293 | $entry['tcomment'] = !isset($entry['tcomment']) ? array() : $entry['tcomment']; |
||
294 | $entry['tcomment'][] = trim(substr($line, 1)); |
||
295 | break; |
||
296 | } |
||
297 | |||
298 | return $entry; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param string $msgstr |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | protected function parseHeaders($msgstr) |
||
307 | { |
||
308 | $headers = array_filter(explode('\\n', $msgstr)); |
||
309 | |||
310 | return $headers; |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param string $line |
||
315 | * @param array $entry |
||
316 | * |
||
317 | * @return bool |
||
318 | */ |
||
319 | protected function shouldIgnoreLine($line, array $entry) |
||
320 | { |
||
321 | return empty($line) && count($entry) === 0; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param string $line |
||
326 | * @param array $entry |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | protected function shouldCloseEntry($line, array $entry) |
||
331 | { |
||
332 | $tokens = $this->getProperty($line); |
||
333 | $property = $tokens[0]; |
||
334 | |||
335 | return ($line === '' || ($property === 'msgid' && isset($entry['msgid']))); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * @param string $value |
||
340 | * @return string |
||
341 | */ |
||
342 | protected function unquote($value) |
||
346 | |||
347 | /** |
||
348 | * Checks if entry is a header by |
||
349 | * |
||
350 | * @param array $entry |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | protected function isHeader(array $entry) |
||
403 | |||
404 | /** |
||
405 | * @param string $line |
||
406 | * |
||
407 | * @return array |
||
408 | */ |
||
409 | protected function getProperty($line) |
||
415 | } |
||
416 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: