From d913c5f52dd12503995bf3888dbd82f17df74c0b Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 18 Nov 2020 14:35:08 +0100 Subject: [PATCH 01/25] Update phpunit and phpspec versions --- .gitignore | 3 +++ composer.json | 6 +++--- phpunit.xml.dist | 1 - tests/CsvReaderFactoryTest.php | 5 +++-- tests/CsvReaderTest.php | 5 +++-- tests/CsvWriterTest.php | 2 +- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9780341..7086d9e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ composer.phar composer.lock phpspec.yml phpunit.xml +composer.lock +.idea +.phpunit.result.cache diff --git a/composer.json b/composer.json index 48d2ed7..d5aea80 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "docs": "https://portphp.readthedocs.org" }, "require": { - "portphp/portphp": "^1.2.0" + "portphp/portphp": "^1.2.0|dev-master" }, "autoload": { "psr-4": { @@ -31,8 +31,8 @@ } }, "require-dev": { - "phpunit/phpunit": "^4.0", - "phpspec/phpspec": "^2.1" + "phpunit/phpunit": "^9.4", + "phpspec/phpspec": "^7.0" }, "autoload-dev": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3d8fe50..ca641e6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,6 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" verbose="true" > diff --git a/tests/CsvReaderFactoryTest.php b/tests/CsvReaderFactoryTest.php index 15edd04..9b7cf60 100644 --- a/tests/CsvReaderFactoryTest.php +++ b/tests/CsvReaderFactoryTest.php @@ -1,10 +1,11 @@ Date: Wed, 18 Nov 2020 14:48:53 +0100 Subject: [PATCH 02/25] Update tests --- .gitignore | 2 -- composer.json | 1 + tests/CsvReaderFactoryTest.php | 2 +- tests/CsvReaderTest.php | 4 ++- tests/CsvWriterTest.php | 3 +-- tests/StreamWriterTest.php | 45 ++++++++++++++++++++++++++++++++++ 6 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 tests/StreamWriterTest.php diff --git a/.gitignore b/.gitignore index 7086d9e..d533ef1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ vendor/ -composer.phar composer.lock -phpspec.yml phpunit.xml composer.lock .idea diff --git a/composer.json b/composer.json index d5aea80..99432c7 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "docs": "https://portphp.readthedocs.org" }, "require": { + "php": "^7.1", "portphp/portphp": "^1.2.0|dev-master" }, "autoload": { diff --git a/tests/CsvReaderFactoryTest.php b/tests/CsvReaderFactoryTest.php index 9b7cf60..2b71464 100644 --- a/tests/CsvReaderFactoryTest.php +++ b/tests/CsvReaderFactoryTest.php @@ -1,6 +1,6 @@ expectException(DuplicateHeadersException::class); $reader = $this->getReader('data_column_headers_duplicates.csv'); $reader->setHeaderRowNumber(0); } diff --git a/tests/CsvWriterTest.php b/tests/CsvWriterTest.php index 44166a9..bdabadb 100644 --- a/tests/CsvWriterTest.php +++ b/tests/CsvWriterTest.php @@ -1,9 +1,8 @@ stream)) { + fclose($this->stream); + $this->stream = null; + } + } + + protected function getStream() + { + if (!is_resource($this->stream)) { + $this->stream = fopen('php://temp', 'r+'); + } + + return $this->stream; + } + + /** + * @param string $expected + * @param AbstractStreamWriter $actual + * @param string $message + */ + public static function assertContentsEquals($expected, $actual, $message = '') + { + $stream = $actual->getStream(); + rewind($stream); + $actual = stream_get_contents($stream); + + self::assertEquals($expected, $actual, $message); + } +} From 7234f77dd90ba012e66b75183716346db7075d91 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 18 Nov 2020 14:49:51 +0100 Subject: [PATCH 03/25] Update required php version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 99432c7..2eb0ad1 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "docs": "https://portphp.readthedocs.org" }, "require": { - "php": "^7.1", + "php": ">=7.1", "portphp/portphp": "^1.2.0|dev-master" }, "autoload": { From a265196930b35c3db2fc112ebbc45b7dc5d3b543 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 18 Nov 2020 14:54:54 +0100 Subject: [PATCH 04/25] Format composer.json --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 2eb0ad1..8805f44 100644 --- a/composer.json +++ b/composer.json @@ -24,17 +24,17 @@ }, "require": { "php": ">=7.1", - "portphp/portphp": "^1.2.0|dev-master" + "portphp/portphp": "^2.2.0|dev-master" + }, + "require-dev": { + "phpunit/phpunit": "^9.4", + "phpspec/phpspec": "^7.0" }, "autoload": { "psr-4": { "Port\\Csv\\": "src/" } }, - "require-dev": { - "phpunit/phpunit": "^9.4", - "phpspec/phpspec": "^7.0" - }, "autoload-dev": { "psr-4": { "Port\\Csv\\Tests\\": "tests/" From 74816f14c6ba90c4d6d8050aa5bb793c7605ca66 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 18 Nov 2020 15:38:10 +0100 Subject: [PATCH 05/25] Dependency Updates --- .gitignore | 2 +- composer.json | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index d533ef1..20016d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ vendor/ composer.lock +phpspec.yml phpunit.xml -composer.lock .idea .phpunit.result.cache diff --git a/composer.json b/composer.json index 8805f44..dd9a876 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "require": { "php": ">=7.1", - "portphp/portphp": "^2.2.0|dev-master" + "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { "phpunit/phpunit": "^9.4", @@ -44,5 +44,11 @@ "branch-alias": { "dev-master": "2.0.x-dev" } - } + }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/klodoma/portphp-portphp" + } + ] } From f17fc35f57bf776cf647fc6b9ea18b7cc273271f Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 24 Nov 2020 09:53:17 +0100 Subject: [PATCH 06/25] Dependency and Configuration Updates --- .editorconfig | 3 +++ .gitignore | 1 + composer.json | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index cac5bb7..9403682 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,6 @@ indent_size = 4 [*.yml*] indent_style = space indent_size = 2 + +[*.md] +trim_trailing_whitespace = true diff --git a/.gitignore b/.gitignore index 20016d4..5b8c118 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor/ +build/ composer.lock phpspec.yml phpunit.xml diff --git a/composer.json b/composer.json index dd9a876..2a942b1 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { - "phpunit/phpunit": "^9.4", + "phpunit/phpunit": "^8.0", "phpspec/phpspec": "^7.0" }, "autoload": { From d2b498d7f81486aca34d74135b2078c8e9580098 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 10:14:28 +0100 Subject: [PATCH 07/25] Drop Support for Php < 7.3 --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index d111709..99914a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,14 +3,14 @@ dist: trusty language: php php: - - 5.6 - - 7.1 - - 7.2 - 7.3 matrix: + allow_failures: + - php: hhvm + - php: nightly include: - - php: 5.6 + - php: 7.3 env: - COMPOSER_FLAGS="--prefer-lowest --prefer-stable" - COVERAGE=true From 82b1b2e7e71ea6fec7478804d54d8d7ea8e70bd8 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 10:42:59 +0100 Subject: [PATCH 08/25] Update phpunit --- .travis.yml | 2 ++ composer.json | 2 +- phpspec.yml.dist | 5 +++++ phpunit.xml.dist | 32 +++++++++++++++----------------- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index 99914a9..d95749e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,8 @@ language: php php: - 7.3 + - 7.4 + - nightly matrix: allow_failures: diff --git a/composer.json b/composer.json index 2a942b1..1e14f18 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { - "phpunit/phpunit": "^8.0", + "phpunit/phpunit": "^9.0", "phpspec/phpspec": "^7.0" }, "autoload": { diff --git a/phpspec.yml.dist b/phpspec.yml.dist index 0da615e..20fce67 100644 --- a/phpspec.yml.dist +++ b/phpspec.yml.dist @@ -2,4 +2,9 @@ suites: library_suite: namespace: Port\Csv psr4_prefix: Port\Csv +extensions: + FriendsOfPhpSpec\PhpSpec\CodeCoverage\CodeCoverageExtension: ~ formatter.name: pretty +code_coverage: + format: clover + output: build/phpspec.coverage.xml diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ca641e6..515d3f6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,19 +1,17 @@ - - - - - ./tests/ - - + + + + src + + + + + + + + ./tests/ + + + From 0a8843a67c759c669baf5ca4b0866ddb979c077b Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 10:47:07 +0100 Subject: [PATCH 09/25] Update php version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1e14f18..a442342 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "docs": "https://portphp.readthedocs.org" }, "require": { - "php": ">=7.1", + "php": ">=7.3", "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { From f8004489ee9e60e5a9a01843c1b9943ef151f737 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:03:53 +0100 Subject: [PATCH 10/25] CI Fixes, Update PHPUnit Version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a442342..5dadcd6 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^9.4", "phpspec/phpspec": "^7.0" }, "autoload": { From 2eae1b4881c15ed6dd5588e372cd3efe6460eaf9 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:10:44 +0100 Subject: [PATCH 11/25] CSV Reader, handle UTF8 - BOM --- src/CsvReader.php | 26 ++++++++++++++-- tests/CsvReaderTest.php | 48 +++++++++++++++++++---------- tests/fixtures/data_with_bom.csv | 4 +++ tests/fixtures/data_without_bom.csv | 4 +++ 4 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 tests/fixtures/data_with_bom.csv create mode 100644 tests/fixtures/data_without_bom.csv diff --git a/src/CsvReader.php b/src/CsvReader.php index c50871f..9f5964f 100644 --- a/src/CsvReader.php +++ b/src/CsvReader.php @@ -108,12 +108,12 @@ public function current() { // If the CSV has no column headers just return the line if (empty($this->columnHeaders)) { - return $this->file->current(); + return $this->getCurrentLine(); } // Since the CSV has column headers use them to construct an associative array for the columns in this line do { - $line = $this->file->current(); + $line = $this->getCurrentLine(); // In non-strict mode pad/slice the line to match the column headers if (!$this->isStrict()) { @@ -323,7 +323,7 @@ public function setStrict($strict) protected function readHeaderRow($rowNumber) { $this->file->seek($rowNumber); - $headers = $this->file->current(); + $headers = $this->getCurrentLine(); // Test for duplicate column headers $diff = array_diff_assoc($headers, array_unique($headers)); @@ -404,4 +404,24 @@ protected function mergeDuplicates(array $line) return $values; } + + /** + * Returns the current line from the file pointer. + * If found the BOM is removed + * + * @return array|false|string + */ + protected function getCurrentLine() + { + $key = $this->file->key(); + $line = $this->file->current(); + + //remove the BOM from the first line + if ($key === 0 && array_key_exists(0, $line)) { + $bom = pack('H*', 'EFBBBF'); + $line[0] = preg_replace("/^$bom/", '', $line[0]); + } + + return $line; + } } diff --git a/tests/CsvReaderTest.php b/tests/CsvReaderTest.php index 3852fc9..e1e15ba 100644 --- a/tests/CsvReaderTest.php +++ b/tests/CsvReaderTest.php @@ -5,12 +5,13 @@ use PHPUnit\Framework\TestCase; use Port\Csv\CsvReader; use Port\Exception\DuplicateHeadersException; +use SplFileObject; class CsvReaderTest extends TestCase { public function testReadCsvFileWithColumnHeaders() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setHeaderRowNumber(0); @@ -39,7 +40,7 @@ public function testReadCsvFileWithColumnHeaders() public function testReadCsvFileWithoutColumnHeaders() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); $csvReader = new CsvReader($file); $this->assertEmpty($csvReader->getColumnHeaders()); @@ -47,7 +48,7 @@ public function testReadCsvFileWithoutColumnHeaders() public function testReadCsvFileWithManualColumnHeaders() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setColumnHeaders(array('id', 'number', 'description')); @@ -60,7 +61,7 @@ public function testReadCsvFileWithManualColumnHeaders() public function testReadCsvFileWithTrailingBlankLines() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_blank_lines.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_blank_lines.csv'); $csvReader = new CsvReader($file); $csvReader->setColumnHeaders(array('id', 'number', 'description')); @@ -73,14 +74,14 @@ public function testReadCsvFileWithTrailingBlankLines() public function testCountWithoutHeaders() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_no_column_headers.csv'); $csvReader = new CsvReader($file); $this->assertEquals(3, $csvReader->count()); } public function testCountWithHeaders() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setHeaderRowNumber(0); $this->assertEquals(3, $csvReader->count(), 'Row count should not include header'); @@ -88,7 +89,7 @@ public function testCountWithHeaders() public function testCountWithFewerElementsThanColumnHeadersNotStrict() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_fewer_elements_than_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_fewer_elements_than_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setStrict(false); $csvReader->setHeaderRowNumber(0); @@ -98,7 +99,7 @@ public function testCountWithFewerElementsThanColumnHeadersNotStrict() public function testCountWithMoreElementsThanColumnHeadersNotStrict() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_more_elements_than_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_more_elements_than_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setStrict(false); $csvReader->setHeaderRowNumber(0); @@ -110,7 +111,7 @@ public function testCountWithMoreElementsThanColumnHeadersNotStrict() public function testCountDoesNotMoveFilePointer() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv'); $csvReader = new CsvReader($file); $csvReader->setHeaderRowNumber(0); @@ -123,7 +124,7 @@ public function testCountDoesNotMoveFilePointer() public function testVaryingElementCountWithColumnHeadersNotStrict() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers_varying_element_count.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_column_headers_varying_element_count.csv'); $csvReader = new CsvReader($file); $csvReader->setStrict(false); $csvReader->setHeaderRowNumber(0); @@ -134,7 +135,7 @@ public function testVaryingElementCountWithColumnHeadersNotStrict() public function testVaryingElementCountWithoutColumnHeadersNotStrict() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers_varying_element_count.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_no_column_headers_varying_element_count.csv'); $csvReader = new CsvReader($file); $csvReader->setStrict(false); $csvReader->setColumnHeaders(array('id', 'number', 'description')); @@ -145,7 +146,7 @@ public function testVaryingElementCountWithoutColumnHeadersNotStrict() public function testInvalidCsv() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_column_headers_varying_element_count.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_column_headers_varying_element_count.csv'); $reader = new CsvReader($file); $reader->setHeaderRowNumber(0); @@ -164,7 +165,7 @@ public function testInvalidCsv() public function testLastRowInvalidCsv() { - $file = new \SplFileObject(__DIR__.'/fixtures/data_no_column_headers_varying_element_count.csv'); + $file = new SplFileObject(__DIR__.'/fixtures/data_no_column_headers_varying_element_count.csv'); $reader = new CsvReader($file); $reader->setColumnHeaders(array('id', 'number', 'description')); @@ -190,11 +191,9 @@ public function testLineBreaks() $this->assertCount(3, $reader); } - /** - * @expectedException \Port\Exception\DuplicateHeadersException description - */ public function testDuplicateHeadersThrowsException() { + $this->expectException(\Port\Exception\DuplicateHeadersException::class);// description $this->expectException(DuplicateHeadersException::class); $reader = $this->getReader('data_column_headers_duplicates.csv'); $reader->setHeaderRowNumber(0); @@ -226,6 +225,21 @@ public function testDuplicateHeadersIncrement() ); } + public function testCSVWithBOM() + { + $reader = $this->getReader('data_with_bom.csv'); + $reader->setHeaderRowNumber(0); + $this->assertSame(['id', 'number', 'description'], $reader->getColumnHeaders()); + } + + public function testCSVWithoutBOM() + { + $reader = $this->getReader('data_without_bom.csv'); + $reader->setHeaderRowNumber(0); + $this->assertSame(['id', 'number', 'description'], $reader->getColumnHeaders()); + } + + public function testDuplicateHeadersMerge() { $reader = $this->getReader('data_column_headers_duplicates.csv'); @@ -277,7 +291,7 @@ public function testMaximumNesting() protected function getReader($filename) { - $file = new \SplFileObject(__DIR__.'/fixtures/'.$filename); + $file = new SplFileObject(__DIR__.'/fixtures/'.$filename); return new CsvReader($file); } diff --git a/tests/fixtures/data_with_bom.csv b/tests/fixtures/data_with_bom.csv new file mode 100644 index 0000000..405095f --- /dev/null +++ b/tests/fixtures/data_with_bom.csv @@ -0,0 +1,4 @@ +id,number,description +50,123,"Description" +6,456,"Another description" +7,7890,"Some more info" diff --git a/tests/fixtures/data_without_bom.csv b/tests/fixtures/data_without_bom.csv new file mode 100644 index 0000000..5d10a38 --- /dev/null +++ b/tests/fixtures/data_without_bom.csv @@ -0,0 +1,4 @@ +id,number,description +50,123,"Description" +6,456,"Another description" +7,7890,"Some more info" From a6c2c5ae6389b509504980b43ad46ca4352127f4 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:18:52 +0100 Subject: [PATCH 12/25] Code Inspection fixes, use lower phpunit version, composer error --- composer.json | 2 +- phpunit.xml.dist | 44 +++++++++++++++++++++++++++++--------------- src/CsvReader.php | 18 ++++++++++-------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/composer.json b/composer.json index 5dadcd6..887c996 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { - "phpunit/phpunit": "^9.4", + "phpunit/phpunit": "^9.2", "phpspec/phpspec": "^7.0" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 515d3f6..4e12743 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,17 +1,31 @@ - - - - src - - - - - - - - ./tests/ - - - + + + + + ./tests/ + + + + + + src + + + + + + + + diff --git a/src/CsvReader.php b/src/CsvReader.php index 9f5964f..b4db292 100644 --- a/src/CsvReader.php +++ b/src/CsvReader.php @@ -4,13 +4,15 @@ use Port\Exception\DuplicateHeadersException; use Port\Reader\CountableReader; +use SeekableIterator; +use SplFileObject; /** * Reads a CSV file, using as little memory as possible * * @author David de Boer */ -class CsvReader implements CountableReader, \SeekableIterator +class CsvReader implements CountableReader, SeekableIterator { const DUPLICATE_HEADERS_INCREMENT = 1; const DUPLICATE_HEADERS_MERGE = 2; @@ -25,7 +27,7 @@ class CsvReader implements CountableReader, \SeekableIterator /** * CSV file * - * @var \SplFileObject + * @var SplFileObject */ protected $file; @@ -74,21 +76,21 @@ class CsvReader implements CountableReader, \SeekableIterator protected $duplicateHeadersFlag; /** - * @param \SplFileObject $file + * @param SplFileObject $file * @param string $delimiter * @param string $enclosure * @param string $escape */ - public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') + public function __construct(SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') { ini_set('auto_detect_line_endings', true); $this->file = $file; $this->file->setFlags( - \SplFileObject::READ_CSV | - \SplFileObject::SKIP_EMPTY | - \SplFileObject::READ_AHEAD | - \SplFileObject::DROP_NEW_LINE + SplFileObject::READ_CSV | + SplFileObject::SKIP_EMPTY | + SplFileObject::READ_AHEAD | + SplFileObject::DROP_NEW_LINE ); $this->file->setCsvControl( $delimiter, From 25eaf7e86c73e6e39b987ae106f4ef1b68000110 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:23:46 +0100 Subject: [PATCH 13/25] Fix Code Inspection findings --- src/CsvReaderFactory.php | 5 +++-- tests/CsvReaderFactoryTest.php | 5 +++-- tests/CsvReaderTest.php | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/CsvReaderFactory.php b/src/CsvReaderFactory.php index f985c30..18d8fab 100644 --- a/src/CsvReaderFactory.php +++ b/src/CsvReaderFactory.php @@ -3,6 +3,7 @@ namespace Port\Csv; use Port\Reader\ReaderFactory; +use SplFileObject; /** * Factory that creates CsvReaders @@ -58,11 +59,11 @@ public function __construct( } /** - * @param \SplFileObject $file + * @param SplFileObject $file * * @return CsvReader */ - public function getReader(\SplFileObject $file) + public function getReader(SplFileObject $file) { $reader = new CsvReader($file, $this->delimiter, $this->enclosure, $this->escape); diff --git a/tests/CsvReaderFactoryTest.php b/tests/CsvReaderFactoryTest.php index 2b71464..70afa83 100644 --- a/tests/CsvReaderFactoryTest.php +++ b/tests/CsvReaderFactoryTest.php @@ -4,19 +4,20 @@ use PHPUnit\Framework\TestCase; use Port\Csv\CsvReaderFactory; +use SplFileObject; class CsvReaderFactoryTest extends TestCase { public function testGetReader() { $factory = new CsvReaderFactory(); - $reader = $factory->getReader(new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv')); + $reader = $factory->getReader(new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv')); $this->assertInstanceOf('Port\Csv\CsvReader', $reader); $this->assertCount(4, $reader); $factory = new CsvReaderFactory(0); - $reader = $factory->getReader(new \SplFileObject(__DIR__.'/fixtures/data_column_headers.csv')); + $reader = $factory->getReader(new SplFileObject(__DIR__.'/fixtures/data_column_headers.csv')); $this->assertCount(3, $reader); } diff --git a/tests/CsvReaderTest.php b/tests/CsvReaderTest.php index e1e15ba..b2132a0 100644 --- a/tests/CsvReaderTest.php +++ b/tests/CsvReaderTest.php @@ -6,6 +6,7 @@ use Port\Csv\CsvReader; use Port\Exception\DuplicateHeadersException; use SplFileObject; +use SplTempFileObject; class CsvReaderTest extends TestCase { @@ -193,7 +194,7 @@ public function testLineBreaks() public function testDuplicateHeadersThrowsException() { - $this->expectException(\Port\Exception\DuplicateHeadersException::class);// description + $this->expectException(DuplicateHeadersException::class);// description $this->expectException(DuplicateHeadersException::class); $reader = $this->getReader('data_column_headers_duplicates.csv'); $reader->setHeaderRowNumber(0); @@ -271,7 +272,7 @@ public function testMaximumNesting() ini_set('xdebug.max_nesting_level', 200); - $file = new \SplTempFileObject(); + $file = new SplTempFileObject(); for($i = 0; $i < 500; $i++) { $file->fwrite("1,2,3\n"); } From 86414845b79e5c5517b684b2d941400e3f7f42ea Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:34:25 +0100 Subject: [PATCH 14/25] Fix Code Inspection findings, version updates --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 887c996..71c19dd 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ }, "require-dev": { "phpunit/phpunit": "^9.2", - "phpspec/phpspec": "^7.0" + "phpspec/phpspec": "^7.0", + "friends-of-phpspec/phpspec-code-coverage": "^6.0" }, "autoload": { "psr-4": { From 5f422a61019bd7e162fdb169fa92d396b3f2045c Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:36:43 +0100 Subject: [PATCH 15/25] Fix Code Inspection findings --- src/CsvReader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CsvReader.php b/src/CsvReader.php index b4db292..79cdff5 100644 --- a/src/CsvReader.php +++ b/src/CsvReader.php @@ -331,6 +331,7 @@ protected function readHeaderRow($rowNumber) $diff = array_diff_assoc($headers, array_unique($headers)); if (count($diff) > 0) { switch ($this->duplicateHeadersFlag) { + /** @noinspection PhpMissingBreakStatementInspection */ case self::DUPLICATE_HEADERS_INCREMENT: $headers = $this->incrementHeaders($headers); // Fall through From d5c83ff49074ca05df213be982bc9d401c62611c Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Tue, 1 Dec 2020 11:48:36 +0100 Subject: [PATCH 16/25] Fix Code Inspection findings --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 71c19dd..a2f9df4 100644 --- a/composer.json +++ b/composer.json @@ -27,9 +27,9 @@ "portphp/portphp": "^2.0.0|dev-master" }, "require-dev": { - "phpunit/phpunit": "^9.2", + "phpunit/phpunit": "^9.2.0", "phpspec/phpspec": "^7.0", - "friends-of-phpspec/phpspec-code-coverage": "^6.0" + "friends-of-phpspec/phpspec-code-coverage": "^5.0" }, "autoload": { "psr-4": { From 8b7702e14c40103bf934f45a041c5b1571f46f14 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 09:48:34 +0100 Subject: [PATCH 17/25] Update composer version dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a2f9df4..12d9df7 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "require-dev": { "phpunit/phpunit": "^9.2.0", "phpspec/phpspec": "^7.0", - "friends-of-phpspec/phpspec-code-coverage": "^5.0" + "friends-of-phpspec/phpspec-code-coverage": "^6.0" }, "autoload": { "psr-4": { From 6659b497431d490b68ffc77da105e0bb66fa52b9 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 10:06:07 +0100 Subject: [PATCH 18/25] Update composer version dependencies, force php-code-coverage min version --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 12d9df7..786232f 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,7 @@ }, "require-dev": { "phpunit/phpunit": "^9.2.0", + "phpunit/php-code-coverage": "^9.2.0", "phpspec/phpspec": "^7.0", "friends-of-phpspec/phpspec-code-coverage": "^6.0" }, From 81f4fc4d1a2d206e18963d2e30bd124c1372c92d Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 11:15:47 +0100 Subject: [PATCH 19/25] Travis CI fails --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 786232f..12d9df7 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,6 @@ }, "require-dev": { "phpunit/phpunit": "^9.2.0", - "phpunit/php-code-coverage": "^9.2.0", "phpspec/phpspec": "^7.0", "friends-of-phpspec/phpspec-code-coverage": "^6.0" }, From 397df7359bca7cb87d43ae7ff8fa1b8750935531 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 11:18:20 +0100 Subject: [PATCH 20/25] Travis CI fails --- .travis.yml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d95749e..c639e02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,15 +2,20 @@ dist: trusty language: php +cache: + directories: + - $HOME/.composer/cache/files + php: - 7.3 - 7.4 - - nightly + # - hhvm + # - nightly matrix: allow_failures: - - php: hhvm - - php: nightly + # - php: hhvm + # - php: nightly include: - php: 7.3 env: From 6dccd963d8f1276ea97d6924010de94e2c1c61b4 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 11:22:09 +0100 Subject: [PATCH 21/25] Travis CI fails - remove --prefer-lowest --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index c639e02..77ff246 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ cache: php: - 7.3 - 7.4 - # - hhvm + # - hhvm # - nightly matrix: @@ -19,7 +19,7 @@ matrix: include: - php: 7.3 env: - - COMPOSER_FLAGS="--prefer-lowest --prefer-stable" + - COMPOSER_FLAGS=" --prefer-stable" - COVERAGE=true - PHPUNIT_FLAGS="--coverage-clover=coverage.clover" From 4423c18e59d0ff1e9c5c297fde9d2aa78f18c07a Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 11:51:26 +0100 Subject: [PATCH 22/25] Travis CI devs --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 12d9df7..a2f9df4 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "require-dev": { "phpunit/phpunit": "^9.2.0", "phpspec/phpspec": "^7.0", - "friends-of-phpspec/phpspec-code-coverage": "^6.0" + "friends-of-phpspec/phpspec-code-coverage": "^5.0" }, "autoload": { "psr-4": { From 4243758b032ea56f4102501db04e345934181518 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 11:57:17 +0100 Subject: [PATCH 23/25] Travis CI devs --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 77ff246..7488031 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,6 @@ dist: trusty language: php -cache: - directories: - - $HOME/.composer/cache/files - php: - 7.3 - 7.4 From 48d6e4bbc4b2a5bcc0a3ce226a38f15fd90cc119 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 12:11:31 +0100 Subject: [PATCH 24/25] Travis CI devs --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7488031..f938b4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,9 @@ matrix: install: - travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction +before_script: + - export XDEBUG_MODE=coverage + script: - vendor/bin/phpunit ${PHPUNIT_FLAGS} From cf6f2b60f4f96dc84a5805b862bc1a909af0b191 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 2 Dec 2020 12:33:05 +0100 Subject: [PATCH 25/25] Remove dev-master dependencies from composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a2f9df4..31e39b9 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "require": { "php": ">=7.3", - "portphp/portphp": "^2.0.0|dev-master" + "portphp/portphp": "^2.0.0" }, "require-dev": { "phpunit/phpunit": "^9.2.0",