diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-10-21 13:29:43 +0200 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-10-21 13:29:43 +0200 |
commit | cbfc72a07f8c20843153efce08cb955945f3c023 (patch) | |
tree | a5e8afb978495710d45a171b3173bac658cc9f5d | |
parent | b8e0853c2e021f47a08bb38060e8e93a27cce3ba (diff) | |
download | csv-cbfc72a07f8c20843153efce08cb955945f3c023.zip csv-cbfc72a07f8c20843153efce08cb955945f3c023.tar.gz csv-cbfc72a07f8c20843153efce08cb955945f3c023.tar.bz2 |
Improve code
- using call_user_func to avoid PHP bug https://bugs.php.net/bug.php?id=68475
- improve regexp documentation
- add more excluded files to gitattributes
-rw-r--r-- | .gitattributes | 11 | ||||
-rw-r--r-- | src/AbstractCsv.php | 24 | ||||
-rw-r--r-- | src/Config/Output.php | 6 | ||||
-rw-r--r-- | src/Modifier/MapIterator.php | 3 | ||||
-rw-r--r-- | src/Modifier/QueryFilter.php | 4 | ||||
-rw-r--r-- | src/Modifier/RowFilter.php | 7 | ||||
-rw-r--r-- | src/Modifier/StreamFilter.php | 15 | ||||
-rw-r--r-- | src/Plugin/ForbiddenNullValuesValidator.php | 2 | ||||
-rw-r--r-- | src/Plugin/SkipNullValuesFormatter.php | 2 | ||||
-rw-r--r-- | src/Reader.php | 9 | ||||
-rw-r--r-- | src/Writer.php | 6 |
11 files changed, 52 insertions, 37 deletions
diff --git a/.gitattributes b/.gitattributes index 0ae82b4..e431d7a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,7 +1,12 @@ -/test export-ignore -/examples export-ignore /.gitattributes export-ignore /.gitignore export-ignore +/.php_cs export-ignore /.travis.yml export-ignore +/examples export-ignore +/phpunit.xml export-ignore +/README.md export-ignore /scrutinizer.yml export-ignore -/phpunit.xml export-ignore
\ No newline at end of file +/test export-ignore +/CHANGELOG.md export-ignore +/CONTRIBUTING.md export-ignore +/CONDUCT.md export-ignore diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index db3ab6c..9259fd3 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -94,9 +94,9 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate */ protected function __construct($path, $open_mode = 'r+') { - $this->flags = SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE; + $this->flags = SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE; $this->open_mode = strtolower($open_mode); - $this->path = $this->normalizePath($path); + $this->path = $this->normalizePath($path); $this->initStreamFilter($this->path); } @@ -133,7 +133,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate public function getIterator() { $iterator = $this->path; - if (! $iterator instanceof SplFileObject) { + if (!$iterator instanceof SplFileObject) { $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode); } $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape); @@ -192,7 +192,9 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate { if ($path instanceof SplTempFileObject) { throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path'); - } elseif ($path instanceof SplFileInfo) { + } + + if ($path instanceof SplFileInfo) { $path = $path->getPath().'/'.$path->getBasename(); } @@ -256,14 +258,14 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate protected function newInstance($class_name, $open_mode) { $csv = new $class_name($this->path, $open_mode); - $csv->delimiter = $this->delimiter; - $csv->enclosure = $this->enclosure; - $csv->escape = $this->escape; + $csv->delimiter = $this->delimiter; + $csv->enclosure = $this->enclosure; + $csv->escape = $this->escape; $csv->encodingFrom = $this->encodingFrom; - $csv->flags = $this->flags; - $csv->input_bom = $this->input_bom; - $csv->output_bom = $this->output_bom; - $csv->newline = $this->newline; + $csv->flags = $this->flags; + $csv->input_bom = $this->input_bom; + $csv->output_bom = $this->output_bom; + $csv->newline = $this->newline; return $csv; } diff --git a/src/Config/Output.php b/src/Config/Output.php index 1bddfe3..991b9de 100644 --- a/src/Config/Output.php +++ b/src/Config/Output.php @@ -156,7 +156,7 @@ trait Output */ public function output($filename = null) { - if (! is_null($filename)) { + if (!is_null($filename)) { $filename = trim($filename); $filename = filter_var($filename, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); header('Content-Type: application/octet-stream'); @@ -183,7 +183,7 @@ trait Output $csv = $this->getIterator(); $csv->rewind(); $csv->setFlags(SplFileObject::READ_CSV); - if (! empty($bom)) { + if (!empty($bom)) { $csv->fseek(mb_strlen($input_bom)); } echo $bom; @@ -271,7 +271,7 @@ trait Output $item = $doc->createElement($row_name); array_walk($row, function ($value) use (&$item, $doc, $cell_name) { $content = $doc->createTextNode($value); - $cell = $doc->createElement($cell_name); + $cell = $doc->createElement($cell_name); $cell->appendChild($content); $item->appendChild($cell); }); diff --git a/src/Modifier/MapIterator.php b/src/Modifier/MapIterator.php index 436cb99..cedc95b 100644 --- a/src/Modifier/MapIterator.php +++ b/src/Modifier/MapIterator.php @@ -50,8 +50,7 @@ class MapIterator extends IteratorIterator public function current() { $iterator = $this->getInnerIterator(); - $callable = $this->callable; - return $callable($iterator->current(), $iterator->key(), $iterator); + return call_user_func($this->callable, $iterator->current(), $iterator->key(), $iterator); } } diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php index e11f589..eaceec8 100644 --- a/src/Modifier/QueryFilter.php +++ b/src/Modifier/QueryFilter.php @@ -268,7 +268,7 @@ trait QueryFilter * * @return Iterator */ - protected function getStripBomIterator($iterator) + protected function getStripBomIterator(Iterator $iterator) { $bom = $this->getInputBom(); @@ -344,7 +344,7 @@ trait QueryFilter $obj->uasort(function ($rowA, $rowB) { $sortRes = 0; foreach ($this->iterator_sort_by as $callable) { - if (0 !== ($sortRes = $callable($rowA, $rowB))) { + if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) { break; } } diff --git a/src/Modifier/RowFilter.php b/src/Modifier/RowFilter.php index 0205ee4..4728660 100644 --- a/src/Modifier/RowFilter.php +++ b/src/Modifier/RowFilter.php @@ -161,7 +161,7 @@ trait RowFilter protected function formatRow(array $row) { foreach ($this->formatters as $formatter) { - $row = $formatter($row); + $row = call_user_func($formatter, $row); } return $row; @@ -172,13 +172,12 @@ trait RowFilter * * @param array $row * - * @throws \League\Csv\Exception\InvalidRowException If the validation failed - * + * @throws InvalidRowException If the validation failed */ protected function validateRow(array $row) { foreach ($this->validators as $name => $validator) { - if (true !== $validator($row)) { + if (true !== call_user_func($validator, $row)) { throw new InvalidRowException($name, $row, 'row validation failed'); } } diff --git a/src/Modifier/StreamFilter.php b/src/Modifier/StreamFilter.php index 34b9b14..d3d717f 100644 --- a/src/Modifier/StreamFilter.php +++ b/src/Modifier/StreamFilter.php @@ -52,7 +52,12 @@ trait StreamFilter * * @var string */ - protected $stream_regex = ',^php://filter/(?P<mode>:?read=|write=)?(?P<filters>.*?)/resource=(?P<resource>.*)$,i'; + protected $stream_regex = ',^ + php://filter/ + (?P<mode>:?read=|write=)? # The resource open mode + (?P<filters>.*?) # The resource registered filters + /resource=(?P<resource>.*) # The resource path + $,ix'; /** * Internal path setter @@ -77,8 +82,8 @@ trait StreamFilter return; } - $this->stream_uri = $matches['resource']; - $this->stream_filters = explode('|', $matches['filters']); + $this->stream_uri = $matches['resource']; + $this->stream_filters = explode('|', $matches['filters']); $this->stream_filter_mode = $this->fetchStreamModeAsInt($matches['mode']); } @@ -111,7 +116,7 @@ trait StreamFilter */ protected function assertStreamable() { - if (! is_string($this->stream_uri)) { + if (!is_string($this->stream_uri)) { throw new LogicException('The stream filter API can not be used'); } } @@ -141,7 +146,7 @@ trait StreamFilter public function setStreamFilterMode($mode) { $this->assertStreamable(); - if (! in_array($mode, [STREAM_FILTER_ALL, STREAM_FILTER_READ, STREAM_FILTER_WRITE])) { + if (!in_array($mode, [STREAM_FILTER_ALL, STREAM_FILTER_READ, STREAM_FILTER_WRITE])) { throw new OutOfBoundsException('the $mode should be a valid `STREAM_FILTER_*` constant'); } diff --git a/src/Plugin/ForbiddenNullValuesValidator.php b/src/Plugin/ForbiddenNullValuesValidator.php index c4c36ae..2ce443c 100644 --- a/src/Plugin/ForbiddenNullValuesValidator.php +++ b/src/Plugin/ForbiddenNullValuesValidator.php @@ -34,6 +34,6 @@ class ForbiddenNullValuesValidator return is_null($value); }); - return ! $res; + return !$res; } } diff --git a/src/Plugin/SkipNullValuesFormatter.php b/src/Plugin/SkipNullValuesFormatter.php index 37118e0..9746139 100644 --- a/src/Plugin/SkipNullValuesFormatter.php +++ b/src/Plugin/SkipNullValuesFormatter.php @@ -31,7 +31,7 @@ class SkipNullValuesFormatter public function __invoke(array $row) { return array_filter($row, function ($value) { - return ! is_null($value); + return !is_null($value); }); } } diff --git a/src/Reader.php b/src/Reader.php index e4b5deb..f7e1d49 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -51,7 +51,7 @@ class Reader extends AbstractCsv $iterator = $this->applyIteratorFilter($iterator); $iterator = $this->applyIteratorSortBy($iterator); $iterator = $this->applyIteratorInterval($iterator); - if (! is_null($callable)) { + if (!is_null($callable)) { return new MapIterator($iterator, $callable); } @@ -73,7 +73,12 @@ class Reader extends AbstractCsv $index = 0; $iterator = $this->query(); $iterator->rewind(); - while ($iterator->valid() && true === $callable($iterator->current(), $iterator->key(), $iterator)) { + while ($iterator->valid() && true === call_user_func( + $callable, + $iterator->current(), + $iterator->key(), + $iterator + )) { ++$index; $iterator->next(); } diff --git a/src/Writer.php b/src/Writer.php index 571d021..707c5bf 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -69,7 +69,7 @@ class Writer extends AbstractCsv protected static function initFputcsv() { if (is_null(static::$fputcsv)) { - static::$fputcsv = new ReflectionMethod('\SplFileObject', 'fputcsv'); + static::$fputcsv = new ReflectionMethod('\SplFileObject', 'fputcsv'); static::$fputcsv_param_count = static::$fputcsv->getNumberOfParameters(); } } @@ -87,7 +87,7 @@ class Writer extends AbstractCsv */ public function insertAll($rows) { - if (! is_array($rows) && ! $rows instanceof Traversable) { + if (!is_array($rows) && !$rows instanceof Traversable) { throw new InvalidArgumentException( 'the provided data must be an array OR a \Traversable object' ); @@ -109,7 +109,7 @@ class Writer extends AbstractCsv */ public function insertOne($row) { - if (! is_array($row)) { + if (!is_array($row)) { $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape); } $row = $this->formatRow($row); |