Complex classes like Reader 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 Reader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Reader implements \Iterator |
||
37 | { |
||
38 | const PLACEHOLDER_DELIM = '[=[__DLIM__]=]'; |
||
39 | const PLACEHOLDER_NEWLINE = '[=[__NWLN__]=]'; |
||
40 | |||
41 | /** |
||
42 | * This class supports any sources of input that implements this interface. |
||
43 | * This way I can read from local files, streams, FTP, any class that implements |
||
44 | * the "Readable" interface |
||
45 | * @var \CSVelte\Contract\Readable |
||
46 | */ |
||
47 | protected $source; |
||
48 | |||
49 | /** |
||
50 | * @var \CSVelte\Flavor The "flavor" or format of the CSV being read |
||
51 | */ |
||
52 | protected $flavor; |
||
53 | |||
54 | /** |
||
55 | * @var \CSVelte\Table\Row|boolean Row currently loaded into memory |
||
56 | */ |
||
57 | protected $current; |
||
58 | |||
59 | /** |
||
60 | * @var integer The current line being read (from input source) |
||
61 | */ |
||
62 | protected $line = 0; |
||
63 | |||
64 | /** |
||
65 | * @var \CSVelte\Table\HeaderRow The header row (if any) |
||
66 | */ |
||
67 | protected $header; |
||
68 | |||
69 | /** |
||
70 | * @var array An array of callback functions |
||
71 | */ |
||
72 | protected $filters = array(); |
||
73 | |||
74 | /** |
||
75 | * @var bool True if current line ended while inside a quoted string |
||
76 | */ |
||
77 | protected $open = false; |
||
78 | |||
79 | /** |
||
80 | * @var bool True if last character read was the escape character |
||
81 | */ |
||
82 | protected $escape = false; |
||
83 | |||
84 | /** |
||
85 | * Reader Constructor. |
||
86 | * Initializes a reader object using an input source and optionally a flavor |
||
87 | * |
||
88 | * @param \CSVelte\Contract\Readable $input The source of our CSV data |
||
89 | * @param \CSVelte\Flavor|array|null $flavor The "flavor" or format specification object |
||
90 | */ |
||
91 | 22 | public function __construct($input, $flavor = null) |
|
97 | |||
98 | /** |
||
99 | * Set the flavor. |
||
100 | * |
||
101 | * Set the ``CSVelte\Flavor`` object, used to determine CSV format. |
||
102 | * |
||
103 | * @param \CSVelte\Flavor|array|null $flavor Either an array or a flavor object |
||
104 | */ |
||
105 | 20 | protected function setFlavor($flavor = null) |
|
120 | |||
121 | /** |
||
122 | * Set the reader source. |
||
123 | * |
||
124 | * The reader can accept anything that implements Readable and is actually |
||
125 | * readable (can be read). This will make sure that whatever is passed to |
||
126 | * the reader meets these expectations and set $this->source. It can also |
||
127 | * accept any string (or any object with a __toString() method), or an |
||
128 | * SplFileObject, so long as it represents a file rather than a directory. |
||
129 | * |
||
130 | * @param \CSVelte\Contract\Readable|object|string|SplFileObject $input See description |
||
131 | * @return $this |
||
132 | */ |
||
133 | 20 | protected function setSource($input) |
|
144 | |||
145 | /** |
||
146 | * Load a line into memory |
||
147 | * |
||
148 | * @return void ($this?) |
||
149 | * @access protected |
||
150 | */ |
||
151 | 20 | protected function load() |
|
169 | |||
170 | /** |
||
171 | * Read single line from CSV data source (stream, file, etc.), taking into |
||
172 | * account CSV's de-facto quoting rules with respect to designated line |
||
173 | * terminator character when they fall within quoted strings. |
||
174 | * |
||
175 | * @return string A CSV row (could possibly span multiple lines depending on |
||
176 | * quoting and escaping) |
||
177 | * @throws \CSVelte\Exception\EndOfFileException when eof has been reached |
||
178 | * and the read buffer has all been returned |
||
179 | */ |
||
180 | 20 | protected function readLine() |
|
198 | |||
199 | /** |
||
200 | * Determine whether last line ended while a quoted string was still "open" |
||
201 | * |
||
202 | * This method is used in a loop to determine if each line being read ends |
||
203 | * while a quoted string is still "open". |
||
204 | * |
||
205 | * @param string $line Line of csv to analyze |
||
206 | * @param string $quoteChar The quote/enclosure character to use |
||
207 | * @param string $escapeChar The escape char/sequence to use |
||
208 | * @return bool True if currently within a quoted string |
||
209 | */ |
||
210 | 20 | protected function inQuotedString($line, $quoteChar, $escapeChar) |
|
226 | |||
227 | /** |
||
228 | * Flavor Getter. |
||
229 | * |
||
230 | * Retreive the "flavor" object being used by the reader |
||
231 | * |
||
232 | * @return \CSVelte\Flavor |
||
233 | * @access public |
||
234 | */ |
||
235 | 20 | public function getFlavor() |
|
239 | |||
240 | /** |
||
241 | * Check if flavor object defines header. |
||
242 | * |
||
243 | * Determine whether or not the input source's CSV data contains a header |
||
244 | * row or not. Unless you explicitly specify so within your Flavor object, |
||
245 | * this method is a logical best guess. The CSV format does not |
||
246 | * provide metadata of any kind and therefor does not provide this info. |
||
247 | * |
||
248 | * @return boolean True if the input source has a header row (or, to be more ) |
||
249 | * accurate, if the flavor SAYS it has a header row) |
||
250 | * @todo Rather than always reading in Taster::SAMPLE_SIZE, read in ten lines at a time until |
||
251 | * whatever method it is has enough data to make a reliable decision/guess |
||
252 | */ |
||
253 | 20 | public function hasHeader() |
|
257 | |||
258 | /** |
||
259 | * Temporarily replace special characters within a quoted string |
||
260 | * |
||
261 | * Replace all instances of newlines and whatever character you specify (as |
||
262 | * the delimiter) that are contained within quoted text. The replacements are |
||
263 | * simply a special placeholder string. This is done so that I can use the |
||
264 | * very unsmart "explode" function and not have to worry about it exploding |
||
265 | * on delimiters or newlines within quotes. Once I have exploded, I typically |
||
266 | * sub back in the real characters before doing anything else. |
||
267 | * |
||
268 | * @param string $data The string to do the replacements on |
||
269 | * @param string $delim The delimiter character to replace |
||
270 | * @param string $quo The quote character |
||
271 | * @param string $eol Line terminator character/sequence |
||
272 | * @return string The data with replacements performed |
||
273 | * @access protected |
||
274 | * @internal |
||
275 | * @todo I could probably pass in (maybe optionally) the newline character I |
||
276 | * want to replace as well. I'll do that if I need to. |
||
277 | * @todo Create a regex class so you can do $regex->escape() rather than |
||
278 | * preg_quote |
||
279 | */ |
||
280 | 20 | protected function replaceQuotedSpecialChars($data, $delim, $quo, $eol) |
|
288 | |||
289 | /** |
||
290 | * Undo temporary special char replacements |
||
291 | * |
||
292 | * Replace the special character placeholders with the characters they |
||
293 | * originally substituted. |
||
294 | * |
||
295 | * @param string $data The data to undo replacements in |
||
296 | * @param string $delim The delimiter character |
||
297 | * @param string $eol The character or string of characters used to terminate lines |
||
298 | * @return string The data with placeholders replaced with original characters |
||
299 | * @internal |
||
300 | */ |
||
301 | 20 | protected function undoReplaceQuotedSpecialChars($data, $delim, $eol) |
|
310 | |||
311 | /** |
||
312 | * Remove quotes wrapping text. |
||
313 | * |
||
314 | * @param string $data The data to unquote |
||
315 | * @return string The data with quotes stripped from the outside of it |
||
316 | * @internal |
||
317 | */ |
||
318 | 20 | protected function unQuote($data) |
|
325 | |||
326 | /** |
||
327 | * @internal |
||
328 | * @todo This actually shouldn't even be necessary. Characters should be read |
||
329 | * in one at a time and a quote that follows another should just be ignored |
||
330 | * deeming this unnecessary. |
||
331 | */ |
||
332 | 20 | protected function unEscape($str, $esc, $quo) |
|
336 | |||
337 | /** |
||
338 | * Parse a line of CSV data into an array of columns |
||
339 | * |
||
340 | * @param string A line of CSV data to parse |
||
341 | * @return array An array of columns |
||
342 | * @access protected |
||
343 | * @internal |
||
344 | */ |
||
345 | 20 | protected function parse($line) |
|
356 | |||
357 | /** |
||
358 | * Retrieve current row. |
||
359 | * |
||
360 | * @return CSVelte\Table\Row The current row |
||
361 | */ |
||
362 | 20 | public function current() |
|
366 | |||
367 | /** |
||
368 | * Advance to the next row |
||
369 | * |
||
370 | * @return CSVelte\Table\Row|null The current row (if there is one) |
||
371 | */ |
||
372 | 15 | public function next() |
|
379 | |||
380 | /** |
||
381 | * Determine if current position has valid row. |
||
382 | * |
||
383 | * @return boolean True if current row is valid |
||
384 | */ |
||
385 | 8 | public function valid() |
|
389 | |||
390 | /** |
||
391 | * Retrieve current row key (line number). |
||
392 | * |
||
393 | * @return int The current line number |
||
394 | */ |
||
395 | 5 | public function key() |
|
399 | |||
400 | /** |
||
401 | * Rewind to the beginning of the dataset. |
||
402 | * |
||
403 | * @return CSVelte\Table\Row|null The current row |
||
404 | */ |
||
405 | 20 | public function rewind() |
|
416 | |||
417 | /** |
||
418 | * Retrieve header row. |
||
419 | * |
||
420 | * @return CSVelte\Table\HeaderRow|null The header row if there is one |
||
421 | */ |
||
422 | 2 | public function header() |
|
426 | |||
427 | /** |
||
428 | * Add anonumous function as filter. |
||
429 | * |
||
430 | * Add an anonymous function that accepts the current row as its only argument. |
||
431 | * Return true from the function to keep that row, false otherwise. |
||
432 | * |
||
433 | * @param Callable $filter An anonymous function to filter out row by certain criteria |
||
434 | * @return $this |
||
435 | */ |
||
436 | 3 | public function addFilter(Callable $filter) |
|
441 | |||
442 | /** |
||
443 | * Add multiple filters at once. |
||
444 | * |
||
445 | * Add an array of anonymous functions to filter out certain rows. |
||
446 | * |
||
447 | * @param array $filters An array of anonymous functions |
||
448 | * @return $this |
||
449 | */ |
||
450 | 1 | public function addFilters(array $filters) |
|
457 | |||
458 | /** |
||
459 | * Returns an iterator with rows from user-supplied filter functions removed |
||
460 | * |
||
461 | * @return CSVelte\Reader\FilteredReader An iterator with filtered rows |
||
462 | */ |
||
463 | 3 | public function filter() |
|
467 | |||
468 | /** |
||
469 | * Retrieve the contents of the dataset as an array of arrays. |
||
470 | * |
||
471 | * @return array An array of arrays of CSV content |
||
472 | */ |
||
473 | public function toArray() |
||
479 | |||
480 | } |
||
481 |