@@ -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) |
@@ -68,7 +68,7 @@ |
||
| 68 | 68 | } elseif ($fields instanceof Iterator) { |
| 69 | 69 | $fields = iterator_to_array($fields); |
| 70 | 70 | } else { |
| 71 | - throw new InvalidArgumentException(__CLASS__ . " requires an array, got: " . gettype($fields)); |
|
| 71 | + throw new InvalidArgumentException(__CLASS__." requires an array, got: ".gettype($fields)); |
|
| 72 | 72 | } |
| 73 | 73 | } |
| 74 | 74 | $this->fields = collect(array_values($fields)); |
@@ -152,7 +152,9 @@ |
||
| 152 | 152 | public function next() |
| 153 | 153 | { |
| 154 | 154 | $this->position++; |
| 155 | - if ($this->valid()) return $this->current(); |
|
| 155 | + if ($this->valid()) { |
|
| 156 | + return $this->current(); |
|
| 157 | + } |
|
| 156 | 158 | } |
| 157 | 159 | |
| 158 | 160 | /** |
@@ -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))) { |
@@ -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 | /** |
@@ -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; |
@@ -283,7 +283,7 @@ discard block |
||
| 283 | 283 | */ |
| 284 | 284 | protected function replaceQuotedSpecialChars($data, $delim, $quo, $eol) |
| 285 | 285 | { |
| 286 | - return preg_replace_callback('/(['. preg_quote($quo, '/') . '])(.*)\1/imsU', function($matches) use ($delim, $eol) { |
|
| 286 | + return preg_replace_callback('/(['.preg_quote($quo, '/').'])(.*)\1/imsU', function($matches) use ($delim, $eol) { |
|
| 287 | 287 | $ret = str_replace($eol, self::PLACEHOLDER_NEWLINE, $matches[0]); |
| 288 | 288 | $ret = str_replace($delim, self::PLACEHOLDER_DELIM, $ret); |
| 289 | 289 | return $ret; |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | protected function unEscape($str, $esc, $quo) |
| 337 | 337 | { |
| 338 | - return str_replace($esc . $quo, $quo, $str); |
|
| 338 | + return str_replace($esc.$quo, $quo, $str); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** |
@@ -476,7 +476,7 @@ discard block |
||
| 476 | 476 | */ |
| 477 | 477 | public function toArray() |
| 478 | 478 | { |
| 479 | - return array_map(function($row){ |
|
| 479 | + return array_map(function($row) { |
|
| 480 | 480 | return $row->toArray(); |
| 481 | 481 | }, iterator_to_array($this)); |
| 482 | 482 | } |
@@ -329,7 +329,7 @@ |
||
| 329 | 329 | * @param string $str The string to unescape |
| 330 | 330 | * @param string $esc The escape character used |
| 331 | 331 | * @param string $quo The quote character used |
| 332 | - * @return mixed The string with characters unescaped |
|
| 332 | + * @return string The string with characters unescaped |
|
| 333 | 333 | * @todo This actually shouldn't even be necessary. Characters should be read |
| 334 | 334 | * in one at a time and a quote that follows another should just be ignored |
| 335 | 335 | * deeming this unnecessary. |
@@ -14,11 +14,9 @@ |
||
| 14 | 14 | namespace CSVelte; |
| 15 | 15 | |
| 16 | 16 | use CSVelte\Contract\Streamable; |
| 17 | - |
|
| 18 | 17 | use CSVelte\Table\Row; |
| 19 | 18 | use CSVelte\Table\HeaderRow; |
| 20 | 19 | use CSVelte\Reader\FilteredIterator as FilteredReader; |
| 21 | - |
|
| 22 | 20 | use CSVelte\Exception\EndOfFileException; |
| 23 | 21 | |
| 24 | 22 | use function |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | { |
| 124 | 124 | self::assertFileExists($filename); |
| 125 | 125 | if (!is_readable($filename)) { |
| 126 | - throw new IOException('Permission denied for: ' . $filename, IOException::ERR_FILE_PERMISSION_DENIED); |
|
| 126 | + throw new IOException('Permission denied for: '.$filename, IOException::ERR_FILE_PERMISSION_DENIED); |
|
| 127 | 127 | } |
| 128 | 128 | } |
| 129 | 129 | |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | protected static function assertFileExists($filename) |
| 138 | 138 | { |
| 139 | 139 | if (!file_exists($filename)) { |
| 140 | - throw new IOException('File does not exist: ' . $filename, IOException::ERR_FILE_NOT_FOUND); |
|
| 140 | + throw new IOException('File does not exist: '.$filename, IOException::ERR_FILE_NOT_FOUND); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -62,10 +62,10 @@ discard block |
||
| 62 | 62 | $paths = $this->getPaths(); |
| 63 | 63 | if ($rp = realpath($path)) { |
| 64 | 64 | if (in_array($rp, $paths)) return true; |
| 65 | - $this->paths []= $rp; |
|
| 65 | + $this->paths [] = $rp; |
|
| 66 | 66 | return true; |
| 67 | 67 | } |
| 68 | - $this->paths []= $path; |
|
| 68 | + $this->paths [] = $path; |
|
| 69 | 69 | return false; |
| 70 | 70 | } |
| 71 | 71 | |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | */ |
| 114 | 114 | public function load($className) |
| 115 | 115 | { |
| 116 | - if(class_exists($className)) { |
|
| 116 | + if (class_exists($className)) { |
|
| 117 | 117 | return; |
| 118 | 118 | } |
| 119 | 119 | $fqcp = str_replace(self::NAMESPACE_SEPARATOR, DIRECTORY_SEPARATOR, $className); |
| 120 | 120 | $paths = $this->getPaths(); |
| 121 | 121 | foreach ($paths as $path) { |
| 122 | - $classPath = $path . DIRECTORY_SEPARATOR . $fqcp . '.php'; |
|
| 123 | - if(file_exists($classPath) && is_readable($classPath)) { |
|
| 122 | + $classPath = $path.DIRECTORY_SEPARATOR.$fqcp.'.php'; |
|
| 123 | + if (file_exists($classPath) && is_readable($classPath)) { |
|
| 124 | 124 | require_once($classPath); |
| 125 | 125 | return; |
| 126 | 126 | } |
@@ -61,7 +61,9 @@ |
||
| 61 | 61 | { |
| 62 | 62 | $paths = $this->getPaths(); |
| 63 | 63 | if ($rp = realpath($path)) { |
| 64 | - if (in_array($rp, $paths)) return true; |
|
| 64 | + if (in_array($rp, $paths)) { |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 65 | 67 | $this->paths []= $rp; |
| 66 | 68 | return true; |
| 67 | 69 | } |
@@ -19,7 +19,6 @@ |
||
| 19 | 19 | if (if $file->hasHeader()) { |
| 20 | 20 | $header = $file->getHeader() |
| 21 | 21 | } |
| 22 | - |
|
| 23 | 22 | * you can instead simply call $header->getHeader() and handle this exception if |
| 24 | 23 | * said file has no header |
| 25 | 24 | * |
@@ -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 | } |