@@ -16,14 +16,11 @@ |
||
16 | 16 | use CSVelte\Traits\IsReadable; |
17 | 17 | use CSVelte\Traits\IsWritable; |
18 | 18 | use CSVelte\Traits\IsSeekable; |
19 | - |
|
20 | 19 | use CSVelte\Contract\Readable; |
21 | 20 | use CSVelte\Contract\Writable; |
22 | 21 | use CSVelte\Contract\Seekable; |
23 | 22 | |
24 | 23 | use \InvalidArgumentException; |
25 | -use CSVelte\Exception\NotYetImplementedException; |
|
26 | -use CSVelte\Exception\EndOfFileException; |
|
27 | 24 | use CSVelte\Exception\IOException; |
28 | 25 | |
29 | 26 | /** |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | return self::streamize((string) $resource); |
145 | 145 | } |
146 | 146 | |
147 | - throw new InvalidArgumentException('Invalid resource type: ' . $type); |
|
147 | + throw new InvalidArgumentException('Invalid resource type: '.$type); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | /** |
@@ -231,17 +231,17 @@ discard block |
||
231 | 231 | $stream = @fopen($uri, $mode); |
232 | 232 | } else { |
233 | 233 | if (!is_array($context)) { |
234 | - throw new InvalidArgumentException("Invalid argument for context. Expected array, got: " . gettype($context)); |
|
234 | + throw new InvalidArgumentException("Invalid argument for context. Expected array, got: ".gettype($context)); |
|
235 | 235 | } |
236 | 236 | $context = stream_context_create($context); |
237 | 237 | $stream = @fopen($uri, $mode, false, $context); |
238 | 238 | } |
239 | 239 | if (false === $stream) { |
240 | - throw new IOException("Invalid stream URI: " . $uri, IOException::ERR_INVALID_STREAM_URI); |
|
240 | + throw new IOException("Invalid stream URI: ".$uri, IOException::ERR_INVALID_STREAM_URI); |
|
241 | 241 | } |
242 | 242 | } |
243 | 243 | if (!is_resource($stream) || get_resource_type($stream) != 'stream') { |
244 | - throw new IOException("Expected stream resource, got: " . gettype($stream), IOException::ERR_INVALID_STREAM_RESOURCE); |
|
244 | + throw new IOException("Expected stream resource, got: ".gettype($stream), IOException::ERR_INVALID_STREAM_RESOURCE); |
|
245 | 245 | } |
246 | 246 | return $stream; |
247 | 247 | } |
@@ -225,7 +225,9 @@ discard block |
||
225 | 225 | */ |
226 | 226 | protected static function open($stream, $mode = null, $context = null) |
227 | 227 | { |
228 | - if (is_null($mode)) $mode = 'r+b'; |
|
228 | + if (is_null($mode)) { |
|
229 | + $mode = 'r+b'; |
|
230 | + } |
|
229 | 231 | if (is_string($uri = $stream)) { |
230 | 232 | if (is_null($context)) { |
231 | 233 | $stream = @fopen($uri, $mode); |
@@ -289,8 +291,12 @@ discard block |
||
289 | 291 | */ |
290 | 292 | public function getMetaData($key = null) |
291 | 293 | { |
292 | - if (!$this->stream) return null; |
|
293 | - if (is_null($key)) return $this->meta; |
|
294 | + if (!$this->stream) { |
|
295 | + return null; |
|
296 | + } |
|
297 | + if (is_null($key)) { |
|
298 | + return $this->meta; |
|
299 | + } |
|
294 | 300 | return (array_key_exists($key, $this->meta)) ? $this->meta[$key] : null; |
295 | 301 | } |
296 | 302 | |
@@ -388,7 +394,9 @@ discard block |
||
388 | 394 | */ |
389 | 395 | public function getSize() |
390 | 396 | { |
391 | - if (!$this->stream) return null; |
|
397 | + if (!$this->stream) { |
|
398 | + return null; |
|
399 | + } |
|
392 | 400 | if (is_null($this->size)) { |
393 | 401 | $stats = fstat($this->stream); |
394 | 402 | if (array_key_exists('size', $stats)) { |
@@ -422,7 +430,9 @@ discard block |
||
422 | 430 | public function read($length) |
423 | 431 | { |
424 | 432 | $this->assertIsReadable(); |
425 | - if ($this->eof()) return false; |
|
433 | + if ($this->eof()) { |
|
434 | + return false; |
|
435 | + } |
|
426 | 436 | return fread($this->stream, $length); |
427 | 437 | } |
428 | 438 |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * likely buffer the output so that this may be called after writeRows() |
91 | 91 | * |
92 | 92 | * @param \Iterator|array A list of header values |
93 | - * @return boolean |
|
93 | + * @return boolean|null |
|
94 | 94 | * @throws \CSVelte\Exception\WriterException |
95 | 95 | */ |
96 | 96 | public function setHeaderRow($headers) |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | * This means taking an array of data, and converting it to a Row object |
162 | 162 | * |
163 | 163 | * @param \Iterator|array of data items |
164 | - * @return CSVelte\Table\AbstractRow |
|
164 | + * @return Row |
|
165 | 165 | * @access protected |
166 | 166 | */ |
167 | 167 | protected function prepareRow(Iterator $row) |
@@ -189,6 +189,9 @@ discard block |
||
189 | 189 | return $this->quoteString($data); |
190 | 190 | } |
191 | 191 | |
192 | + /** |
|
193 | + * @param string $str |
|
194 | + */ |
|
192 | 195 | protected function quoteString($str) |
193 | 196 | { |
194 | 197 | $flvr = $this->getFlavor(); |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | { |
142 | 142 | if (is_array($rows)) $rows = new ArrayIterator($rows); |
143 | 143 | if (!($rows instanceof Iterator)) { |
144 | - throw new InvalidArgumentException('First argument for ' . __METHOD__ . ' must be iterable'); |
|
144 | + throw new InvalidArgumentException('First argument for '.__METHOD__.' must be iterable'); |
|
145 | 145 | } |
146 | 146 | $written = 0; |
147 | 147 | if ($rows instanceof Reader) { |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | { |
166 | 166 | $items = array(); |
167 | 167 | foreach ($row as $data) { |
168 | - $items []= $this->prepareData($data); |
|
168 | + $items [] = $this->prepareData($data); |
|
169 | 169 | } |
170 | 170 | $row = new Row($items); |
171 | 171 | return $row; |
@@ -193,11 +193,11 @@ discard block |
||
193 | 193 | // to use it for very long, in fact, once I finish writing the Data class |
194 | 194 | // it is gonezo! |
195 | 195 | $hasSpecialChars = function($s) use ($flvr) { |
196 | - $specialChars = preg_quote($flvr->lineTerminator . $flvr->quoteChar . $flvr->delimiter); |
|
196 | + $specialChars = preg_quote($flvr->lineTerminator.$flvr->quoteChar.$flvr->delimiter); |
|
197 | 197 | $pattern = "/[{$specialChars}]/m"; |
198 | 198 | return preg_match($pattern, $s); |
199 | 199 | }; |
200 | - switch($flvr->quoteStyle) { |
|
200 | + switch ($flvr->quoteStyle) { |
|
201 | 201 | case Flavor::QUOTE_ALL: |
202 | 202 | $doQuote = true; |
203 | 203 | break; |
@@ -227,6 +227,6 @@ discard block |
||
227 | 227 | $escapeQuote = ""; |
228 | 228 | if ($isQuoted) $escapeQuote = ($flvr->doubleQuote) ? $flvr->quoteChar : $flvr->escapeChar; |
229 | 229 | // @todo Not sure what else, if anything, I'm supposed to be escaping here.. |
230 | - return str_replace($flvr->quoteChar, $escapeQuote . $flvr->quoteChar, $str); |
|
230 | + return str_replace($flvr->quoteChar, $escapeQuote.$flvr->quoteChar, $str); |
|
231 | 231 | } |
232 | 232 | } |
@@ -64,7 +64,9 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function __construct(Writable $output, $flavor = null) |
66 | 66 | { |
67 | - if (!($flavor instanceof Flavor)) $flavor = new Flavor($flavor); |
|
67 | + if (!($flavor instanceof Flavor)) { |
|
68 | + $flavor = new Flavor($flavor); |
|
69 | + } |
|
68 | 70 | $this->flavor = $flavor; |
69 | 71 | $this->output = $output; |
70 | 72 | } |
@@ -96,7 +98,9 @@ discard block |
||
96 | 98 | if ($this->written) { |
97 | 99 | throw new WriterException("Cannot set header row once data has already been written. "); |
98 | 100 | } |
99 | - if (is_array($headers)) $headers = new ArrayIterator($headers); |
|
101 | + if (is_array($headers)) { |
|
102 | + $headers = new ArrayIterator($headers); |
|
103 | + } |
|
100 | 104 | $this->headers = $headers; |
101 | 105 | } |
102 | 106 | |
@@ -114,7 +118,9 @@ discard block |
||
114 | 118 | $headerRow = new HeaderRow((array) $this->headers); |
115 | 119 | $this->writeHeaderRow($headerRow); |
116 | 120 | } |
117 | - if (is_array($row)) $row = new ArrayIterator($row); |
|
121 | + if (is_array($row)) { |
|
122 | + $row = new ArrayIterator($row); |
|
123 | + } |
|
118 | 124 | $row = $this->prepareRow($row); |
119 | 125 | if ($count = $this->output->writeLine($row->join($delim), $eol)) { |
120 | 126 | $this->written++; |
@@ -139,7 +145,9 @@ discard block |
||
139 | 145 | */ |
140 | 146 | public function writeRows($rows) |
141 | 147 | { |
142 | - if (is_array($rows)) $rows = new ArrayIterator($rows); |
|
148 | + if (is_array($rows)) { |
|
149 | + $rows = new ArrayIterator($rows); |
|
150 | + } |
|
143 | 151 | if (!($rows instanceof Iterator)) { |
144 | 152 | throw new InvalidArgumentException('First argument for ' . __METHOD__ . ' must be iterable'); |
145 | 153 | } |
@@ -148,7 +156,9 @@ discard block |
||
148 | 156 | $this->writeHeaderRow($rows->header()); |
149 | 157 | } |
150 | 158 | foreach ($rows as $row) { |
151 | - if ($this->writeRow($row)) $written++; |
|
159 | + if ($this->writeRow($row)) { |
|
160 | + $written++; |
|
161 | + } |
|
152 | 162 | } |
153 | 163 | return $written; |
154 | 164 | } |
@@ -225,7 +235,9 @@ discard block |
||
225 | 235 | { |
226 | 236 | $flvr = $this->getFlavor(); |
227 | 237 | $escapeQuote = ""; |
228 | - if ($isQuoted) $escapeQuote = ($flvr->doubleQuote) ? $flvr->quoteChar : $flvr->escapeChar; |
|
238 | + if ($isQuoted) { |
|
239 | + $escapeQuote = ($flvr->doubleQuote) ? $flvr->quoteChar : $flvr->escapeChar; |
|
240 | + } |
|
229 | 241 | // @todo Not sure what else, if anything, I'm supposed to be escaping here.. |
230 | 242 | return str_replace($flvr->quoteChar, $escapeQuote . $flvr->quoteChar, $str); |
231 | 243 | } |
@@ -165,6 +165,7 @@ discard block |
||
165 | 165 | * replaceQuotedSpecialChars method which (at least to me) makes more sense. |
166 | 166 | * |
167 | 167 | * @param string The string to replace quoted strings within |
168 | + * @param string $data |
|
168 | 169 | * @return string The input string with quoted strings removed |
169 | 170 | * @access protected |
170 | 171 | * @todo Replace code that uses this method with the replaceQuotedSpecialChars |
@@ -414,7 +415,7 @@ discard block |
||
414 | 415 | * Determine whether a particular string of data has quotes around it. |
415 | 416 | * |
416 | 417 | * @param string The data to check |
417 | - * @return boolean Whether the data is quoted or not |
|
418 | + * @return integer Whether the data is quoted or not |
|
418 | 419 | * @access protected |
419 | 420 | */ |
420 | 421 | protected function isQuoted($data) |
@@ -464,6 +465,8 @@ discard block |
||
464 | 465 | * |
465 | 466 | * @param string The string to do the replacements on |
466 | 467 | * @param string The delimiter character to replace |
468 | + * @param string $data |
|
469 | + * @param string $delim |
|
467 | 470 | * @return string The data with replacements performed |
468 | 471 | * @access protected |
469 | 472 | * @todo I could probably pass in (maybe optionally) the newline character I |
@@ -490,6 +493,7 @@ discard block |
||
490 | 493 | * to be practical. |
491 | 494 | * |
492 | 495 | * @param string The string of data to check the type of |
496 | + * @param string $data |
|
493 | 497 | * @return string One of the TYPE_ string constants above |
494 | 498 | * @access protected |
495 | 499 | * @uses \Carbon\Carbon date/time ilbrary/class |
@@ -546,6 +550,7 @@ discard block |
||
546 | 550 | * @param string $delim The CSV data's delimiting char (can be a variety of chars but) |
547 | 551 | * typically $eol is either a comma or a tab, sometimes a pipe) |
548 | 552 | * @param string The CSV data's end-of-line char(s) (\n \r or \r\n) |
553 | + * @param string $eol |
|
549 | 554 | * @return boolean True if the data (most likely) contains a header row |
550 | 555 | * @access public |
551 | 556 | * @todo This method needs a total refactor. It's not necessary to loop twice |
@@ -261,20 +261,20 @@ |
||
261 | 261 | throw new TasterException("quoteChar and delimiter cannot be determined", TasterException::ERR_QUOTE_AND_DELIM); |
262 | 262 | } |
263 | 263 | |
264 | - /** |
|
265 | - * Take a list of likely delimiter characters and find the one that occurs |
|
266 | - * the most consistent amount of times within the provided data. |
|
267 | - * |
|
268 | - * @param string The character(s) used for newlines |
|
269 | - * @return string One of four Flavor::QUOTING_* constants |
|
270 | - * @see \CSVelte\Flavor for possible quote style constants |
|
271 | - * @access protected |
|
272 | - * @todo Refactor this method--It needs more thorough testing against a wider |
|
273 | - * variety of CSV data to be sure it works reliably. And I'm sure there |
|
274 | - * are many performance and logic improvements that could be made. This |
|
275 | - * is essentially a first draft. |
|
276 | - * @todo Use replaceQuotedSpecialChars rather than removeQuotedStrings |
|
277 | - */ |
|
264 | + /** |
|
265 | + * Take a list of likely delimiter characters and find the one that occurs |
|
266 | + * the most consistent amount of times within the provided data. |
|
267 | + * |
|
268 | + * @param string The character(s) used for newlines |
|
269 | + * @return string One of four Flavor::QUOTING_* constants |
|
270 | + * @see \CSVelte\Flavor for possible quote style constants |
|
271 | + * @access protected |
|
272 | + * @todo Refactor this method--It needs more thorough testing against a wider |
|
273 | + * variety of CSV data to be sure it works reliably. And I'm sure there |
|
274 | + * are many performance and logic improvements that could be made. This |
|
275 | + * is essentially a first draft. |
|
276 | + * @todo Use replaceQuotedSpecialChars rather than removeQuotedStrings |
|
277 | + */ |
|
278 | 278 | protected function lickDelimiter($eol = "\n") |
279 | 279 | { |
280 | 280 | $delimiters = array(",", "\t", "|", ":", ";", "/", '\\'); |
@@ -193,16 +193,16 @@ discard block |
||
193 | 193 | { |
194 | 194 | $str = $this->removeQuotedStrings($this->sample); |
195 | 195 | $eols = [ |
196 | - self::EOL_WINDOWS => "\r\n", // 0x0D - 0x0A - Windows, DOS OS/2 |
|
197 | - self::EOL_UNIX => "\n", // 0x0A - - Unix, OSX |
|
198 | - self::EOL_TRS80 => "\r", // 0x0D - - Apple ][, TRS80 |
|
196 | + self::EOL_WINDOWS => "\r\n", // 0x0D - 0x0A - Windows, DOS OS/2 |
|
197 | + self::EOL_UNIX => "\n", // 0x0A - - Unix, OSX |
|
198 | + self::EOL_TRS80 => "\r", // 0x0D - - Apple ][, TRS80 |
|
199 | 199 | ]; |
200 | 200 | |
201 | 201 | $curCount = 0; |
202 | 202 | // @todo This should return a default maybe? |
203 | 203 | $curEol = PHP_EOL; |
204 | - foreach($eols as $k => $eol) { |
|
205 | - if( ($count = substr_count($str, $eol)) > $curCount) { |
|
204 | + foreach ($eols as $k => $eol) { |
|
205 | + if (($count = substr_count($str, $eol)) > $curCount) { |
|
206 | 206 | $curCount = $count; |
207 | 207 | $curEol = $eol; |
208 | 208 | } |
@@ -235,12 +235,12 @@ discard block |
||
235 | 235 | $patterns = []; |
236 | 236 | // delim can be anything but line breaks, quotes, alphanumeric, underscore, backslash, or any type of spaces |
237 | 237 | $antidelims = implode(array("\r", "\n", "\w", preg_quote('"', '/'), preg_quote("'", '/')/*, preg_quote('\\', '/')*/, preg_quote(chr(self::SPACE), '/'))); |
238 | - $delim = '(?P<delim>[^' . $antidelims . '])'; |
|
238 | + $delim = '(?P<delim>[^'.$antidelims.'])'; |
|
239 | 239 | $quote = '(?P<quoteChar>"|\'|`)'; // @todo I think MS Excel uses some strange encoding for fancy open/close quotes |
240 | - $patterns[] = '/' . $delim . ' ?' . $quote . '.*?\2\1/ms'; // ,"something", - anything but whitespace or quotes followed by a possible space followed by a quote followed by anything followed by same quote, followed by same anything but whitespace |
|
241 | - $patterns[] = '/(?:^|\n)' . $quote . '.*?\1' . $delim . ' ?/ms'; // 'something', - beginning of line or line break, followed by quote followed by anything followed by quote followed by anything but whitespace or quotes |
|
242 | - $patterns[] = '/' . $delim . ' ?' . $quote . '.*?\2(?:^|\n)/ms'; // ,'something' - anything but whitespace or quote followed by possible space followed by quote followed by anything followed by quote, followed by end of line |
|
243 | - $patterns[] = '/(?:^|\n)' . $quote . '.*?\2(?:$|\n)/ms'; // 'something' - beginning of line followed by quote followed by anything followed by quote followed by same quote followed by end of line |
|
240 | + $patterns[] = '/'.$delim.' ?'.$quote.'.*?\2\1/ms'; // ,"something", - anything but whitespace or quotes followed by a possible space followed by a quote followed by anything followed by same quote, followed by same anything but whitespace |
|
241 | + $patterns[] = '/(?:^|\n)'.$quote.'.*?\1'.$delim.' ?/ms'; // 'something', - beginning of line or line break, followed by quote followed by anything followed by quote followed by anything but whitespace or quotes |
|
242 | + $patterns[] = '/'.$delim.' ?'.$quote.'.*?\2(?:^|\n)/ms'; // ,'something' - anything but whitespace or quote followed by possible space followed by quote followed by anything followed by quote, followed by end of line |
|
243 | + $patterns[] = '/(?:^|\n)'.$quote.'.*?\2(?:$|\n)/ms'; // 'something' - beginning of line followed by quote followed by anything followed by quote followed by same quote followed by end of line |
|
244 | 244 | foreach ($patterns as $pattern) { |
245 | 245 | // @todo I had to add the error suppression char here because it was |
246 | 246 | // causing undefined offset errors with certain data sets. strange... |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | if (array_key_exists(self::DATA_NONNUMERIC, array_flip($types))) { |
386 | 386 | // allow for a SMALL amount of error here |
387 | 387 | $counts = array(self::DATA_SPECIAL => 0, self::DATA_NONNUMERIC => 0); |
388 | - array_walk($freq['quoted'], function ($val) use (&$counts) { |
|
388 | + array_walk($freq['quoted'], function($val) use (&$counts) { |
|
389 | 389 | $counts[$val]++; |
390 | 390 | }); |
391 | 391 | arsort($counts); |
@@ -511,7 +511,7 @@ discard block |
||
511 | 511 | $day = '[0-3]?[0-9]'; |
512 | 512 | $sep = '[\/\.\-]?'; |
513 | 513 | $time = '([0-2]?[0-9](:[0-5][0-9]){1,2}(am|pm)?|[01]?[0-9](am|pm))'; |
514 | - $date = '(' . $month . $sep . $day . $sep . $year . '|' . $day . $sep . $month . $sep . $year . '|' . $year . $sep . $month . $sep . $day . ')'; |
|
514 | + $date = '('.$month.$sep.$day.$sep.$year.'|'.$day.$sep.$month.$sep.$year.'|'.$year.$sep.$month.$sep.$day.')'; |
|
515 | 515 | $dt = Carbon::parse($data); |
516 | 516 | if ($dt->today()) { |
517 | 517 | // then this is most likely a time string... |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | } |
522 | 522 | if (preg_match("/^{$date}$/i", $data)) { |
523 | 523 | return self::TYPE_DATE; |
524 | - } elseif(preg_match("/^{$date} {$time}$/i")) { |
|
524 | + } elseif (preg_match("/^{$date} {$time}$/i")) { |
|
525 | 525 | return self::TYPE_DATETIME; |
526 | 526 | } |
527 | 527 | } catch (\Exception $e) { |
@@ -145,7 +145,9 @@ discard block |
||
145 | 145 | try { |
146 | 146 | list($quoteChar, $delimiter) = $this->lickQuoteAndDelim(); |
147 | 147 | } catch (TasterException $e) { |
148 | - if ($e->getCode() !== TasterException::ERR_QUOTE_AND_DELIM) throw $e; |
|
148 | + if ($e->getCode() !== TasterException::ERR_QUOTE_AND_DELIM) { |
|
149 | + throw $e; |
|
150 | + } |
|
149 | 151 | $quoteChar = '"'; |
150 | 152 | $delimiter = $this->lickDelimiter($lineTerminator); |
151 | 153 | } |
@@ -244,7 +246,9 @@ discard block |
||
244 | 246 | foreach ($patterns as $pattern) { |
245 | 247 | // @todo I had to add the error suppression char here because it was |
246 | 248 | // causing undefined offset errors with certain data sets. strange... |
247 | - if (@preg_match_all($pattern, $this->sample, $matches) && $matches) break; |
|
249 | + if (@preg_match_all($pattern, $this->sample, $matches) && $matches) { |
|
250 | + break; |
|
251 | + } |
|
248 | 252 | } |
249 | 253 | if ($matches) { |
250 | 254 | $quotes = array_count_values($matches['quoteChar']); |
@@ -283,7 +287,9 @@ discard block |
||
283 | 287 | $charFrequency = array(); |
284 | 288 | while ($start < count($lines)) { |
285 | 289 | foreach ($lines as $key => $line) { |
286 | - if (!trim($line)) continue; |
|
290 | + if (!trim($line)) { |
|
291 | + continue; |
|
292 | + } |
|
287 | 293 | foreach ($delimiters as $char) { |
288 | 294 | $freq = substr_count($line, $char); |
289 | 295 | $charFrequency[$char][$key] = $freq; |
@@ -376,11 +382,18 @@ discard block |
||
376 | 382 | } |
377 | 383 | $types = array_unique($freq['quoted']); |
378 | 384 | // if quoting_styles still has QUOTE_ALL or QUOTE_NONE, then that's the one to return |
379 | - if (array_key_exists(Flavor::QUOTE_ALL, $quoting_styles)) return Flavor::QUOTE_ALL; |
|
380 | - if (array_key_exists(Flavor::QUOTE_NONE, $quoting_styles)) return Flavor::QUOTE_NONE; |
|
385 | + if (array_key_exists(Flavor::QUOTE_ALL, $quoting_styles)) { |
|
386 | + return Flavor::QUOTE_ALL; |
|
387 | + } |
|
388 | + if (array_key_exists(Flavor::QUOTE_NONE, $quoting_styles)) { |
|
389 | + return Flavor::QUOTE_NONE; |
|
390 | + } |
|
381 | 391 | if (count($types) == 1) { |
382 | - if (current($types) == self::DATA_SPECIAL) return Flavor::QUOTE_MINIMAL; |
|
383 | - elseif (current($types) == self::DATA_NONNUMERIC) return Flavor::QUOTE_NONNUMERIC; |
|
392 | + if (current($types) == self::DATA_SPECIAL) { |
|
393 | + return Flavor::QUOTE_MINIMAL; |
|
394 | + } elseif (current($types) == self::DATA_NONNUMERIC) { |
|
395 | + return Flavor::QUOTE_NONNUMERIC; |
|
396 | + } |
|
384 | 397 | } else { |
385 | 398 | if (array_key_exists(self::DATA_NONNUMERIC, array_flip($types))) { |
386 | 399 | // allow for a SMALL amount of error here |
@@ -392,7 +405,9 @@ discard block |
||
392 | 405 | $most = current($counts); |
393 | 406 | $least = end($counts); |
394 | 407 | $err_margin = $least / $most; |
395 | - if ($err_margin < 1) return Flavor::QUOTE_NONNUMERIC; |
|
408 | + if ($err_margin < 1) { |
|
409 | + return Flavor::QUOTE_NONNUMERIC; |
|
410 | + } |
|
396 | 411 | } |
397 | 412 | } |
398 | 413 | return Flavor::QUOTE_MINIMAL; |
@@ -583,15 +598,23 @@ discard block |
||
583 | 598 | foreach ($types as $line_no => $cols) { |
584 | 599 | foreach ($cols as $col_no => $col_info) { |
585 | 600 | extract($col_info); |
586 | - if (!array_key_exists($col_no, $potential_header)) continue; |
|
601 | + if (!array_key_exists($col_no, $potential_header)) { |
|
602 | + continue; |
|
603 | + } |
|
587 | 604 | extract($potential_header[$col_no], EXTR_PREFIX_ALL, "header"); |
588 | 605 | if ($header_type == self::TYPE_STRING) { |
589 | 606 | // use length |
590 | - if ($length != $header_length) $hasHeader++; |
|
591 | - else $hasHeader--; |
|
607 | + if ($length != $header_length) { |
|
608 | + $hasHeader++; |
|
609 | + } else { |
|
610 | + $hasHeader--; |
|
611 | + } |
|
592 | 612 | } else { |
593 | - if ($type != $header_type) $hasHeader++; |
|
594 | - else $hasHeader--; |
|
613 | + if ($type != $header_type) { |
|
614 | + $hasHeader++; |
|
615 | + } else { |
|
616 | + $hasHeader--; |
|
617 | + } |
|
595 | 618 | } |
596 | 619 | } |
597 | 620 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | * accept any string (or any object with a __toString() method), or an |
128 | 128 | * SplFileObject, so long as it represents a file rather than a directory. |
129 | 129 | * |
130 | - * @param \CSVelte\Contract\Readable|object|string|SplFileObject $input See description |
|
130 | + * @param Readable $input See description |
|
131 | 131 | * @return $this |
132 | 132 | */ |
133 | 133 | protected function setSource($input) |
@@ -328,6 +328,9 @@ discard block |
||
328 | 328 | * @todo This actually shouldn't even be necessary. Characters should be read |
329 | 329 | * in one at a time and a quote that follows another should just be ignored |
330 | 330 | * deeming this unnecessary. |
331 | + * @param string $str |
|
332 | + * @param string $esc |
|
333 | + * @param string $quo |
|
331 | 334 | */ |
332 | 335 | protected function unEscape($str, $esc, $quo) |
333 | 336 | { |
@@ -338,6 +341,7 @@ discard block |
||
338 | 341 | * Parse a line of CSV data into an array of columns |
339 | 342 | * |
340 | 343 | * @param string A line of CSV data to parse |
344 | + * @param string $line |
|
341 | 345 | * @return array An array of columns |
342 | 346 | * @access protected |
343 | 347 | * @internal |
@@ -417,7 +421,7 @@ discard block |
||
417 | 421 | /** |
418 | 422 | * Retrieve header row. |
419 | 423 | * |
420 | - * @return CSVelte\Table\HeaderRow|null The header row if there is one |
|
424 | + * @return HeaderRow The header row if there is one |
|
421 | 425 | */ |
422 | 426 | public function header() |
423 | 427 | { |
@@ -458,7 +462,7 @@ discard block |
||
458 | 462 | /** |
459 | 463 | * Returns an iterator with rows from user-supplied filter functions removed |
460 | 464 | * |
461 | - * @return CSVelte\Reader\FilteredReader An iterator with filtered rows |
|
465 | + * @return FilteredReader An iterator with filtered rows |
|
462 | 466 | */ |
463 | 467 | public function filter() |
464 | 468 | { |
@@ -91,8 +91,8 @@ |
||
91 | 91 | public function __construct($input, $flavor = null) |
92 | 92 | { |
93 | 93 | $this->setSource($input) |
94 | - ->setFlavor($flavor) |
|
95 | - ->rewind(); |
|
94 | + ->setFlavor($flavor) |
|
95 | + ->rewind(); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | /** |
@@ -185,7 +185,7 @@ discard block |
||
185 | 185 | do { |
186 | 186 | if (!isset($lines)) $lines = array(); |
187 | 187 | if (false === ($line = $this->source->readLine($eol))) { |
188 | - throw new EndOfFileException("End of file reached: " . $this->source->getName()); |
|
188 | + throw new EndOfFileException("End of file reached: ".$this->source->getName()); |
|
189 | 189 | } |
190 | 190 | array_push($lines, rtrim($line, $eol)); |
191 | 191 | } while ($this->inQuotedString(end($lines), $f->quoteChar, $f->escapeChar)); |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | */ |
280 | 280 | protected function replaceQuotedSpecialChars($data, $delim, $quo, $eol) |
281 | 281 | { |
282 | - return preg_replace_callback('/(['. preg_quote($quo, '/') . '])(.*)\1/imsU', function($matches) use ($delim, $eol) { |
|
282 | + return preg_replace_callback('/(['.preg_quote($quo, '/').'])(.*)\1/imsU', function($matches) use ($delim, $eol) { |
|
283 | 283 | $ret = str_replace($eol, self::PLACEHOLDER_NEWLINE, $matches[0]); |
284 | 284 | $ret = str_replace($delim, self::PLACEHOLDER_DELIM, $ret); |
285 | 285 | return $ret; |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | */ |
332 | 332 | protected function unEscape($str, $esc, $quo) |
333 | 333 | { |
334 | - return str_replace($esc . $quo, $quo, $str); |
|
334 | + return str_replace($esc.$quo, $quo, $str); |
|
335 | 335 | } |
336 | 336 | |
337 | 337 | /** |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | */ |
473 | 473 | public function toArray() |
474 | 474 | { |
475 | - return array_map(function($row){ |
|
475 | + return array_map(function($row) { |
|
476 | 476 | return $row->toArray(); |
477 | 477 | }, iterator_to_array($this)); |
478 | 478 | } |
@@ -104,7 +104,9 @@ discard block |
||
104 | 104 | */ |
105 | 105 | protected function setFlavor($flavor = null) |
106 | 106 | { |
107 | - if (is_array($flavor)) $flavor = new Flavor($flavor); |
|
107 | + if (is_array($flavor)) { |
|
108 | + $flavor = new Flavor($flavor); |
|
109 | + } |
|
108 | 110 | $taster = new Taster($this->source); |
109 | 111 | // @todo put this inside a try/catch |
110 | 112 | if (is_null($flavor)) { |
@@ -159,7 +161,9 @@ discard block |
||
159 | 161 | $this->header = new HeaderRow($parsed); |
160 | 162 | } else { |
161 | 163 | $this->current = new Row($parsed); |
162 | - if ($this->header) $this->current->setHeaderRow($this->header); |
|
164 | + if ($this->header) { |
|
165 | + $this->current->setHeaderRow($this->header); |
|
166 | + } |
|
163 | 167 | } |
164 | 168 | } catch (EndOfFileException $e) { |
165 | 169 | $this->current = false; |
@@ -183,7 +187,9 @@ discard block |
||
183 | 187 | $eol = $f->lineTerminator; |
184 | 188 | try { |
185 | 189 | do { |
186 | - if (!isset($lines)) $lines = array(); |
|
190 | + if (!isset($lines)) { |
|
191 | + $lines = array(); |
|
192 | + } |
|
187 | 193 | if (false === ($line = $this->source->readLine($eol))) { |
188 | 194 | throw new EndOfFileException("End of file reached: " . $this->source->getName()); |
189 | 195 | } |
@@ -191,7 +197,9 @@ discard block |
||
191 | 197 | } while ($this->inQuotedString(end($lines), $f->quoteChar, $f->escapeChar)); |
192 | 198 | } catch (EndOfFileException $e) { |
193 | 199 | // only throw the exception if we don't already have lines in the buffer |
194 | - if (!count($lines)) throw $e; |
|
200 | + if (!count($lines)) { |
|
201 | + throw $e; |
|
202 | + } |
|
195 | 203 | } |
196 | 204 | return rtrim(implode($eol, $lines), $eol); |
197 | 205 | } |
@@ -211,14 +219,18 @@ discard block |
||
211 | 219 | { |
212 | 220 | if (!empty($line)) { |
213 | 221 | do { |
214 | - if (!isset($i)) $i = 0; |
|
222 | + if (!isset($i)) { |
|
223 | + $i = 0; |
|
224 | + } |
|
215 | 225 | $c = $line[$i++]; |
216 | 226 | if ($this->escape) { |
217 | 227 | $this->escape = false; |
218 | 228 | continue; |
219 | 229 | } |
220 | 230 | $this->escape = ($c == $escapeChar); |
221 | - if ($c == $quoteChar) $this->open = !$this->open; |
|
231 | + if ($c == $quoteChar) { |
|
232 | + $this->open = !$this->open; |
|
233 | + } |
|
222 | 234 | } while ($i < strlen($line)); |
223 | 235 | } |
224 | 236 | return $this->open; |
@@ -73,7 +73,7 @@ |
||
73 | 73 | |
74 | 74 | public function toArray() |
75 | 75 | { |
76 | - return array_map(function($row){ |
|
76 | + return array_map(function($row) { |
|
77 | 77 | return $row->toArray(); |
78 | 78 | }, iterator_to_array($this)); |
79 | 79 | } |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | protected function assertValidAttribute($attr) |
193 | 193 | { |
194 | 194 | if (!property_exists(self::class, $attr)) |
195 | - throw new InvalidArgumentException("Unknown attribute: " . $attr); |
|
195 | + throw new InvalidArgumentException("Unknown attribute: ".$attr); |
|
196 | 196 | } |
197 | 197 | |
198 | 198 | /** |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | */ |
242 | 242 | public function __set($attr, $val) |
243 | 243 | { |
244 | - throw new ImmutableException("Cannot change attributes on an immutable object: " . self::class . "::\$" . $attr); |
|
244 | + throw new ImmutableException("Cannot change attributes on an immutable object: ".self::class."::\$".$attr); |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | public function toArray() |
@@ -191,8 +191,9 @@ |
||
191 | 191 | */ |
192 | 192 | protected function assertValidAttribute($attr) |
193 | 193 | { |
194 | - if (!property_exists(self::class, $attr)) |
|
195 | - throw new InvalidArgumentException("Unknown attribute: " . $attr); |
|
194 | + if (!property_exists(self::class, $attr)) { |
|
195 | + throw new InvalidArgumentException("Unknown attribute: " . $attr); |
|
196 | + } |
|
196 | 197 | } |
197 | 198 | |
198 | 199 | /** |
@@ -56,7 +56,7 @@ |
||
56 | 56 | public function __construct($fields) |
57 | 57 | { |
58 | 58 | $this->setFields($fields) |
59 | - ->rewind(); |
|
59 | + ->rewind(); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | protected function setFields($fields) |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | } elseif ($fields instanceof Iterator) { |
68 | 68 | $fields = iterator_to_array($fields); |
69 | 69 | } else { |
70 | - throw new InvalidArgumentException(__CLASS__ . " requires an array, got: " . gettype($fields)); |
|
70 | + throw new InvalidArgumentException(__CLASS__." requires an array, got: ".gettype($fields)); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | $this->fields = array_values($fields); |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | protected function assertOffsetExists($offset) |
250 | 250 | { |
251 | 251 | if (!$this->offsetExists($offset)) { |
252 | - throw new OutOfBoundsException("Undefined offset: " . $offset); |
|
252 | + throw new OutOfBoundsException("Undefined offset: ".$offset); |
|
253 | 253 | } |
254 | 254 | } |
255 | 255 |
@@ -151,7 +151,9 @@ discard block |
||
151 | 151 | public function next() |
152 | 152 | { |
153 | 153 | $this->position++; |
154 | - if ($this->valid()) return $this->current(); |
|
154 | + if ($this->valid()) { |
|
155 | + return $this->current(); |
|
156 | + } |
|
155 | 157 | } |
156 | 158 | |
157 | 159 | /** |
@@ -163,7 +165,9 @@ discard block |
||
163 | 165 | public function rewind() |
164 | 166 | { |
165 | 167 | $this->position = 0; |
166 | - if ($this->valid()) return $this->current(); |
|
168 | + if ($this->valid()) { |
|
169 | + return $this->current(); |
|
170 | + } |
|
167 | 171 | } |
168 | 172 | |
169 | 173 | /** |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | */ |
41 | 41 | public function writeLine($line, $eol = PHP_EOL) |
42 | 42 | { |
43 | - return $this->write($line . $eol); |
|
43 | + return $this->write($line.$eol); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | protected function assertIsWritable() |
53 | 53 | { |
54 | 54 | if (!$this->isWritable()) { |
55 | - throw new IOException("Stream not writable: " . $this->getName(), IOException::ERR_NOT_WRITABLE); |
|
55 | + throw new IOException("Stream not writable: ".$this->getName(), IOException::ERR_NOT_WRITABLE); |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 |
@@ -75,7 +75,7 @@ |
||
75 | 75 | protected function assertIsReadable() |
76 | 76 | { |
77 | 77 | if (!$this->isReadable()) { |
78 | - throw new IOException("Stream not readable: " . $this->getName(), IOException::ERR_NOT_READABLE); |
|
78 | + throw new IOException("Stream not readable: ".$this->getName(), IOException::ERR_NOT_READABLE); |
|
79 | 79 | } |
80 | 80 | } |
81 | 81 |
@@ -45,7 +45,9 @@ |
||
45 | 45 | { |
46 | 46 | $size = 0; |
47 | 47 | $buffer = false; |
48 | - if (!is_array($eol)) $eol = array($eol); |
|
48 | + if (!is_array($eol)) { |
|
49 | + $eol = array($eol); |
|
50 | + } |
|
49 | 51 | while (!$this->eof()) { |
50 | 52 | // Using a loose equality here to match on '' and false. |
51 | 53 | if (null == ($byte = $this->read(1))) { |