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 |
||
44 | class Reader implements \Iterator |
||
45 | { |
||
46 | const PLACEHOLDER_DELIM = '[=[__DLIM__]=]'; |
||
47 | const PLACEHOLDER_NEWLINE = '[=[__NWLN__]=]'; |
||
48 | |||
49 | /** |
||
50 | * This class supports any sources of input that implements this interface. |
||
51 | * This way I can read from local files, streams, FTP, any class that implements |
||
52 | * the "Readable" interface |
||
53 | * @var \CSVelte\Contract\Streamable |
||
54 | */ |
||
55 | protected $source; |
||
56 | |||
57 | /** |
||
58 | * @var \CSVelte\Flavor The "flavor" or format of the CSV being read |
||
59 | */ |
||
60 | protected $flavor; |
||
61 | |||
62 | /** |
||
63 | * @var \CSVelte\Table\Row|boolean Row currently loaded into memory |
||
64 | */ |
||
65 | protected $current; |
||
66 | |||
67 | /** |
||
68 | * @var integer The current line being read (from input source) |
||
69 | */ |
||
70 | protected $line = 0; |
||
71 | |||
72 | /** |
||
73 | * @var \CSVelte\Table\HeaderRow The header row (if any) |
||
74 | */ |
||
75 | protected $header; |
||
76 | |||
77 | /** |
||
78 | * @var array An array of callback functions |
||
79 | */ |
||
80 | protected $filters = array(); |
||
81 | |||
82 | /** |
||
83 | * @var bool True if current line ended while inside a quoted string |
||
84 | */ |
||
85 | protected $open = false; |
||
86 | |||
87 | /** |
||
88 | * @var bool True if last character read was the escape character |
||
89 | */ |
||
90 | protected $escape = false; |
||
91 | |||
92 | /** |
||
93 | * Reader Constructor. |
||
94 | * Initializes a reader object using an input source and optionally a flavor |
||
95 | * |
||
96 | * @param mixed $input The source of our CSV data |
||
97 | * @param \CSVelte\Flavor|array|null $flavor The "flavor" or format specification object |
||
98 | */ |
||
99 | 22 | public function __construct($input, $flavor = null) |
|
105 | |||
106 | /** |
||
107 | * Set the flavor. |
||
108 | * |
||
109 | * Set the ``CSVelte\Flavor`` object, used to determine CSV format. |
||
110 | * |
||
111 | * @param \CSVelte\Flavor|array|null $flavor Either an array or a flavor object |
||
112 | */ |
||
113 | 20 | protected function setFlavor($flavor = null) |
|
114 | { |
||
115 | 20 | if (is_array($flavor)) $flavor = new Flavor($flavor); |
|
116 | // @todo put this inside a try/catch |
||
117 | 20 | if (is_null($flavor)) { |
|
118 | 11 | $flavor = taste($this->source); |
|
119 | 11 | } |
|
120 | 20 | if (is_null($flavor->header)) { |
|
121 | // Flavor is immutable, give me a new one with header set to lickHeader return val |
||
122 | 4 | $flavor = $flavor->copy(['header' => taste_has_header($this->source)]); |
|
123 | 4 | } |
|
124 | 20 | $this->flavor = $flavor; |
|
125 | 20 | return $this; |
|
126 | 4 | } |
|
127 | |||
128 | /** |
||
129 | * Set the reader source. |
||
130 | * |
||
131 | * The reader can accept anything that implements Readable and is actually |
||
132 | * readable (can be read). This will make sure that whatever is passed to |
||
133 | * the reader meets these expectations and set $this->source. It can also |
||
134 | * accept any string (or any object with a __toString() method), or an |
||
135 | * SplFileObject, so long as it represents a file rather than a directory. |
||
136 | * |
||
137 | * @param mixed $input See description |
||
138 | * @return $this |
||
139 | */ |
||
140 | 20 | protected function setSource($input) |
|
148 | |||
149 | /** |
||
150 | * Load a line into memory |
||
151 | * |
||
152 | * @return void ($this?) |
||
153 | * @access protected |
||
154 | */ |
||
155 | 20 | protected function load() |
|
173 | |||
174 | /** |
||
175 | * Read single line from CSV data source (stream, file, etc.), taking into |
||
176 | * account CSV's de-facto quoting rules with respect to designated line |
||
177 | * terminator character when they fall within quoted strings. |
||
178 | * |
||
179 | * @return string A CSV row (could possibly span multiple lines depending on |
||
180 | * quoting and escaping) |
||
181 | * @throws \CSVelte\Exception\EndOfFileException when eof has been reached |
||
182 | * and the read buffer has all been returned |
||
183 | */ |
||
184 | 20 | protected function readLine() |
|
202 | |||
203 | /** |
||
204 | * Determine whether last line ended while a quoted string was still "open" |
||
205 | * |
||
206 | * This method is used in a loop to determine if each line being read ends |
||
207 | * while a quoted string is still "open". |
||
208 | * |
||
209 | * @param string $line Line of csv to analyze |
||
210 | * @param string $quoteChar The quote/enclosure character to use |
||
211 | * @param string $escapeChar The escape char/sequence to use |
||
212 | * @return bool True if currently within a quoted string |
||
213 | */ |
||
214 | 20 | protected function inQuotedString($line, $quoteChar, $escapeChar) |
|
215 | { |
||
216 | 20 | if (!empty($line)) { |
|
217 | do { |
||
218 | 20 | if (!isset($i)) $i = 0; |
|
219 | 20 | $c = $line[$i++]; |
|
220 | 20 | if ($this->escape) { |
|
221 | 1 | $this->escape = false; |
|
222 | 1 | continue; |
|
223 | } |
||
224 | 20 | $this->escape = ($c == $escapeChar); |
|
225 | 20 | if ($c == $quoteChar) $this->open = !$this->open; |
|
226 | 20 | } while ($i < strlen($line)); |
|
227 | 20 | } |
|
228 | 20 | return $this->open; |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * Flavor Getter. |
||
233 | * |
||
234 | * Retreive the "flavor" object being used by the reader |
||
235 | * |
||
236 | * @return \CSVelte\Flavor |
||
237 | * @access public |
||
238 | */ |
||
239 | 20 | public function getFlavor() |
|
243 | |||
244 | /** |
||
245 | * Check if flavor object defines header. |
||
246 | * |
||
247 | * Determine whether or not the input source's CSV data contains a header |
||
248 | * row or not. Unless you explicitly specify so within your Flavor object, |
||
249 | * this method is a logical best guess. The CSV format does not |
||
250 | * provide metadata of any kind and therefor does not provide this info. |
||
251 | * |
||
252 | * @return boolean True if the input source has a header row (or, to be more ) |
||
253 | * accurate, if the flavor SAYS it has a header row) |
||
254 | * @todo Rather than always reading in Taster::SAMPLE_SIZE, read in ten lines at a time until |
||
255 | * whatever method it is has enough data to make a reliable decision/guess |
||
256 | */ |
||
257 | 20 | public function hasHeader() |
|
261 | |||
262 | /** |
||
263 | * Temporarily replace special characters within a quoted string |
||
264 | * |
||
265 | * Replace all instances of newlines and whatever character you specify (as |
||
266 | * the delimiter) that are contained within quoted text. The replacements are |
||
267 | * simply a special placeholder string. This is done so that I can use the |
||
268 | * very unsmart "explode" function and not have to worry about it exploding |
||
269 | * on delimiters or newlines within quotes. Once I have exploded, I typically |
||
270 | * sub back in the real characters before doing anything else. |
||
271 | * |
||
272 | * @param string $data The string to do the replacements on |
||
273 | * @param string $delim The delimiter character to replace |
||
274 | * @param string $quo The quote character |
||
275 | * @param string $eol Line terminator character/sequence |
||
276 | * @return string The data with replacements performed |
||
277 | * @access protected |
||
278 | * @internal |
||
279 | * @todo I could probably pass in (maybe optionally) the newline character I |
||
280 | * want to replace as well. I'll do that if I need to. |
||
281 | * @todo Create a regex class so you can do $regex->escape() rather than |
||
282 | * preg_quote |
||
283 | */ |
||
284 | 20 | protected function replaceQuotedSpecialChars($data, $delim, $quo, $eol) |
|
292 | |||
293 | /** |
||
294 | * Undo temporary special char replacements |
||
295 | * |
||
296 | * Replace the special character placeholders with the characters they |
||
297 | * originally substituted. |
||
298 | * |
||
299 | * @param string $data The data to undo replacements in |
||
300 | * @param string $delim The delimiter character |
||
301 | * @param string $eol The character or string of characters used to terminate lines |
||
302 | * @return string The data with placeholders replaced with original characters |
||
303 | * @internal |
||
304 | */ |
||
305 | 20 | protected function undoReplaceQuotedSpecialChars($data, $delim, $eol) |
|
314 | |||
315 | /** |
||
316 | * Remove quotes wrapping text. |
||
317 | * |
||
318 | * @param string $data The data to unquote |
||
319 | * @return string The data with quotes stripped from the outside of it |
||
320 | * @internal |
||
321 | */ |
||
322 | 20 | protected function unQuote($data) |
|
329 | |||
330 | /** |
||
331 | * @internal |
||
332 | * @todo This actually shouldn't even be necessary. Characters should be read |
||
333 | * in one at a time and a quote that follows another should just be ignored |
||
334 | * deeming this unnecessary. |
||
335 | */ |
||
336 | 20 | protected function unEscape($str, $esc, $quo) |
|
340 | |||
341 | /** |
||
342 | * Parse a line of CSV data into an array of columns |
||
343 | * |
||
344 | * @param string A line of CSV data to parse |
||
345 | * @return array An array of columns |
||
346 | * @access protected |
||
347 | * @internal |
||
348 | */ |
||
349 | 20 | protected function parse($line) |
|
360 | |||
361 | /** |
||
362 | * Retrieve current row. |
||
363 | * |
||
364 | * @return CSVelte\Table\Row The current row |
||
365 | */ |
||
366 | 20 | public function current() |
|
370 | |||
371 | /** |
||
372 | * Advance to the next row |
||
373 | * |
||
374 | * @return CSVelte\Table\Row|null The current row (if there is one) |
||
375 | */ |
||
376 | 16 | public function next() |
|
383 | |||
384 | /** |
||
385 | * Determine if current position has valid row. |
||
386 | * |
||
387 | * @return boolean True if current row is valid |
||
388 | */ |
||
389 | 8 | public function valid() |
|
393 | |||
394 | /** |
||
395 | * Retrieve current row key (line number). |
||
396 | * |
||
397 | * @return int The current line number |
||
398 | */ |
||
399 | 5 | public function key() |
|
403 | |||
404 | /** |
||
405 | * Rewind to the beginning of the dataset. |
||
406 | * |
||
407 | * @return CSVelte\Table\Row|null The current row |
||
408 | */ |
||
409 | 20 | public function rewind() |
|
420 | |||
421 | /** |
||
422 | * Retrieve header row. |
||
423 | * |
||
424 | * @return CSVelte\Table\HeaderRow|null The header row if there is one |
||
425 | */ |
||
426 | 2 | public function header() |
|
430 | |||
431 | /** |
||
432 | * Add anonumous function as filter. |
||
433 | * |
||
434 | * Add an anonymous function that accepts the current row as its only argument. |
||
435 | * Return true from the function to keep that row, false otherwise. |
||
436 | * |
||
437 | * @param Callable $filter An anonymous function to filter out row by certain criteria |
||
438 | * @return $this |
||
439 | */ |
||
440 | 3 | public function addFilter(Callable $filter) |
|
445 | |||
446 | /** |
||
447 | * Add multiple filters at once. |
||
448 | * |
||
449 | * Add an array of anonymous functions to filter out certain rows. |
||
450 | * |
||
451 | * @param array $filters An array of anonymous functions |
||
452 | * @return $this |
||
453 | */ |
||
454 | 1 | public function addFilters(array $filters) |
|
461 | |||
462 | /** |
||
463 | * Returns an iterator with rows from user-supplied filter functions removed |
||
464 | * |
||
465 | * @return CSVelte\Reader\FilteredReader An iterator with filtered rows |
||
466 | */ |
||
467 | 3 | public function filter() |
|
471 | |||
472 | /** |
||
473 | * Retrieve the contents of the dataset as an array of arrays. |
||
474 | * |
||
475 | * @return array An array of arrays of CSV content |
||
476 | */ |
||
477 | public function toArray() |
||
483 | |||
484 | } |
||
485 |