summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-08-26 12:06:14 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-08-26 12:06:14 +0200
commitb6e75f7dc42d4dae5879f2cbeca8141d89050b41 (patch)
tree2e3f0cc3a8fef9c99d4e4f96d36ed9db6b522abb
parentd0c07427c9e7526da1e5aa436c6dc8143af3bde2 (diff)
downloadcsv-b6e75f7dc42d4dae5879f2cbeca8141d89050b41.zip
csv-b6e75f7dc42d4dae5879f2cbeca8141d89050b41.tar.gz
csv-b6e75f7dc42d4dae5879f2cbeca8141d89050b41.tar.bz2
improve code quality using calisthenics recommandations
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/Config/StreamFilter.php76
-rw-r--r--src/Iterator/Query.php18
-rw-r--r--src/Reader.php13
-rw-r--r--src/Writer.php11
5 files changed, 68 insertions, 56 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ee5d557..8b994fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,19 +1,19 @@
#Changelog
All Notable changes to `League\Csv` will be documented in this file
-## NEXT - XXXX-XX-XX
+## NEXT - YYYY-MM-DD
### Added
- Stream Filter API in `League\Csv\AbstractCsv`
- named constructors `createFromPath` and `createFromFileObject` in `League\Csv\AbstractCsv` to ease CSV object instantiation
-- `detectDelimiterList` in `League\Csv\AbstractCsv` to replace and remove the use of `Exception` in `detectDelimiter`
+- `detectDelimiterList` in `League\Csv\AbstractCsv` to replace and remove the use of `RuntimeException` in `detectDelimiter`
- `setEncodingFrom` and `setDecodingFrom` in `League\Csv\AbstractCsv` to replace `setEncoding` and `getEncoding` for naming consistency
### Deprecated
- Nothing
### Fixed
-- `League\Csv\Reader::each` more stric `$callable` MUST returns true
+- `League\Csv\Reader::each` more strict `$callable` MUST returns true
### Remove
- `detectDelimiter`
diff --git a/src/Config/StreamFilter.php b/src/Config/StreamFilter.php
index 0514ad4..b4e95cd 100644
--- a/src/Config/StreamFilter.php
+++ b/src/Config/StreamFilter.php
@@ -46,7 +46,7 @@ trait StreamFilter
* @var string the real path to the file
*
*/
- protected $stream_real_path;
+ protected $stream_uri;
/**
* Internal path setter
@@ -61,33 +61,45 @@ trait StreamFilter
*/
protected function initStreamFilter($path)
{
- $this->stream_filters = [];
- $this->stream_real_path = null;
if (! is_string($path)) {
+ $this->stream_uri = null;
+ $this->stream_filters = [];
+
return;
}
- $this->stream_real_path = $path;
+ $this->extractStreamSettings($path);
+ }
- //if we are submitting a filter meta wrapper
- //we extract and inject the mode, the filter and the path
- if (preg_match(
+ /**
+ * Extract Available stream settings from $path
+ *
+ * @param string $path the file path
+ *
+ * @return void
+ */
+ protected function extractStreamSettings($path)
+ {
+ if (! preg_match(
',^php://filter/(?P<mode>:?read=|write=)?(?P<filters>.*?)/resource=(?P<resource>.*)$,i',
$path,
$matches
)) {
- $matches['mode'] = strtolower($matches['mode']);
- $mode = STREAM_FILTER_ALL;
- if ('write=' == $matches['mode']) {
- $mode = STREAM_FILTER_WRITE;
- } elseif ('read=' == $matches['mode']) {
- $mode = STREAM_FILTER_READ;
- }
- $this->stream_filter_mode = $mode;
- $this->stream_real_path = $matches['resource'];
- $this->stream_filters = explode('|', $matches['filters']);
- }
+ $this->stream_uri = $path;
+ $this->stream_filters = [];
+ return;
+ }
+ $matches['mode'] = strtolower($matches['mode']);
+ $mode = STREAM_FILTER_ALL;
+ if ('write=' == $matches['mode']) {
+ $mode = STREAM_FILTER_WRITE;
+ } elseif ('read=' == $matches['mode']) {
+ $mode = STREAM_FILTER_READ;
+ }
+ $this->stream_filter_mode = $mode;
+ $this->stream_uri = $matches['resource'];
+ $this->stream_filters = explode('|', $matches['filters']);
}
/**
@@ -97,9 +109,9 @@ trait StreamFilter
*
* @throws \LogicException If the API can not be use
*/
- protected function checkStreamApiAvailability()
+ protected function assertStreamable()
{
- if (! is_string($this->stream_real_path)) {
+ if (! is_string($this->stream_uri)) {
throw new LogicException('The stream filter API can not be used');
}
}
@@ -111,7 +123,7 @@ trait StreamFilter
*/
public function isActiveStreamFilter()
{
- return is_string($this->stream_real_path);
+ return is_string($this->stream_uri);
}
/**
@@ -128,7 +140,7 @@ trait StreamFilter
*/
public function setStreamFilterMode($mode)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
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');
}
@@ -146,7 +158,7 @@ trait StreamFilter
*/
public function getStreamFilterMode()
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
return $this->stream_filter_mode;
}
@@ -160,7 +172,7 @@ trait StreamFilter
*/
protected function sanitizeStreamFilter($filter_name)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
$filter_name = (string) $filter_name;
return trim($filter_name);
@@ -175,7 +187,7 @@ trait StreamFilter
*/
public function appendStreamFilter($filter_name)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
$this->stream_filters[] = $this->sanitizeStreamFilter($filter_name);
return $this;
@@ -190,7 +202,7 @@ trait StreamFilter
*/
public function prependStreamFilter($filter_name)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
array_unshift($this->stream_filters, $this->sanitizeStreamFilter($filter_name));
return $this;
@@ -205,7 +217,7 @@ trait StreamFilter
*/
public function hasStreamFilter($filter_name)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
return false !== array_search($filter_name, $this->stream_filters, true);
}
@@ -219,7 +231,7 @@ trait StreamFilter
*/
public function removeStreamFilter($filter_name)
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
$res = array_search($filter_name, $this->stream_filters, true);
if (false !== $res) {
unset($this->stream_filters[$res]);
@@ -235,7 +247,7 @@ trait StreamFilter
*/
public function clearStreamFilter()
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
$this->stream_filters = [];
return $this;
@@ -248,9 +260,9 @@ trait StreamFilter
*/
protected function getStreamFilterPath()
{
- $this->checkStreamApiAvailability();
+ $this->assertStreamable();
if (! $this->stream_filters) {
- return $this->stream_real_path;
+ return $this->stream_uri;
}
$prefix = '';
@@ -260,6 +272,6 @@ trait StreamFilter
$prefix = 'write=';
}
- return 'php://filter/'.$prefix.implode('|', $this->stream_filters).'/resource='.$this->stream_real_path;
+ return 'php://filter/'.$prefix.implode('|', $this->stream_filters).'/resource='.$this->stream_uri;
}
}
diff --git a/src/Iterator/Query.php b/src/Iterator/Query.php
index e2f1a0a..22544b0 100644
--- a/src/Iterator/Query.php
+++ b/src/Iterator/Query.php
@@ -177,19 +177,19 @@ trait Query
if (! $this->iterator_sort_by) {
return $iterator;
}
+ $nb_callbacks = count($this->iterator_sort_by);
+ $this->iterator_sort_by = array_values($this->iterator_sort_by);
$res = iterator_to_array($iterator, false);
-
- uasort($res, function ($rowA, $rowB) {
- foreach ($this->iterator_sort_by as $callable) {
- $res = $callable($rowA, $rowB);
- if (0 !== $res) {
- return $res;
- }
+ uasort($res, function ($rowA, $rowB) use ($nb_callbacks) {
+ $res = 0;
+ $index = 0;
+ while ($index < $nb_callbacks && 0 === $res) {
+ $res = $this->iterator_sort_by[$index]($rowA, $rowB);
+ ++$index;
}
- return 0;
+ return $res;
});
-
$this->clearSortBy();
return new ArrayIterator($res);
diff --git a/src/Reader.php b/src/Reader.php
index 0eeabde..5f4ec20 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -96,13 +96,12 @@ class Reader extends AbstractCsv
*/
public function each(callable $callable)
{
+ $index = 0;
$iterator = $this->query();
- $index = 0;
- foreach ($iterator as $rowIndex => $row) {
- if (true !== $callable($row, $rowIndex, $iterator)) {
- break;
- }
- $index++;
+ $iterator->rewind();
+ while ($iterator->valid() && true === $callable($iterator->current(), $iterator->key(), $iterator)) {
+ ++$index;
+ $iterator->next();
}
return $index;
@@ -123,7 +122,7 @@ class Reader extends AbstractCsv
$this->setLimit(1);
$iterator = $this->query();
$iterator->rewind();
- $res = $iterator->getInnerIterator()->current();
+ $res = $iterator->current();
if (! is_array($res)) {
return [];
}
diff --git a/src/Writer.php b/src/Writer.php
index 49973c5..1612f21 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -223,13 +223,13 @@ class Writer extends AbstractCsv
$row = str_getcsv((string) $row, $this->delimiter, $this->enclosure, $this->escape);
}
- foreach ($row as $value) {
- if (!$this->isConvertibleContent($value)) {
+ array_walk($row, function ($value) {
+ if (! $this->isConvertibleContent($value)) {
throw new InvalidArgumentException(
'the values are not convertible into strings'
);
}
- }
+ });
return $row;
}
@@ -237,14 +237,15 @@ class Writer extends AbstractCsv
/**
* Check if a given value can be added into a CSV cell
*
+ * The value MUST respect the null handling mode
+ * The valie MUST be convertible into a string
+ *
* @param mixed $value the value to be added
*
* @return boolean
*/
protected function isConvertibleContent($value)
{
- //check if the row value respects the null handling mode
- //check if the row value can be convertible into string
return (is_null($value) && self::NULL_AS_EXCEPTION != $this->null_handling_mode)
|| self::isValidString($value);
}