From 31cc41a1abca7767929da5c02cb484b5ae9e0230 Mon Sep 17 00:00:00 2001 From: Ondra Date: Wed, 22 Nov 2023 23:27:29 +0100 Subject: [PATCH 1/2] support php8 --- .github/workflows/push.yml | 8 +- Dockerfile | 4 +- composer.json | 2 +- ignore-by-php-version.neon.php | 18 ++ phpstan-baseline-8+.neon | 496 +++++++++++++++++++++++++++++++++ phpstan.neon | 2 +- src/CsvReader.php | 6 + src/CsvWriter.php | 26 +- tests/CsvWriteTest.php | 17 +- 9 files changed, 569 insertions(+), 10 deletions(-) create mode 100644 ignore-by-php-version.neon.php create mode 100644 phpstan-baseline-8+.neon diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 26855f0..3f60cdb 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -9,17 +9,21 @@ env: jobs: tests: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php_version: [ 8.2, 8.1, 8.0, 7.4 ] steps: - name: 'Check out the repo' - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: 'Docker login' if: env.DOCKERHUB_TOKEN run: 'docker login --username "$DOCKERHUB_USER" --password "$DOCKERHUB_TOKEN"' - name: 'Build image' - run: 'docker build -t $APP_IMAGE .' + run: 'docker build --build-arg PHP_VERSION=${{ matrix.php_version }} -t $APP_IMAGE .' - name: 'Run tests' run: 'docker run ${{env.APP_IMAGE}} composer ci' \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 462ebcb..be91d1f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ -FROM php:7.4 +ARG PHP_VERSION=7.4 + +FROM php:${PHP_VERSION:-7.4}-cli-buster ARG DEBIAN_FRONTEND=noninteractive ARG COMPOSER_FLAGS="--prefer-dist --no-interaction" diff --git a/composer.json b/composer.json index ee47623..306ac93 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "keboola/coding-standard": "^13.0", "php-parallel-lint/php-parallel-lint": "^1.3", "phpstan/phpstan": "^1.4", - "phpunit/phpunit": ">=7.5", + "phpunit/phpunit": ">=7.5 <=9.6", "phpstan/phpdoc-parser": "1.5.*" }, "scripts": { diff --git a/ignore-by-php-version.neon.php b/ignore-by-php-version.neon.php new file mode 100644 index 0000000..8b59f51 --- /dev/null +++ b/ignore-by-php-version.neon.php @@ -0,0 +1,18 @@ += 80000) { + $config = array_merge_recursive($config, $adapter->load(__DIR__ . '/phpstan-baseline-8+.neon')); +} else { + $config = array_merge_recursive($config, $adapter->load(__DIR__ . '/phpstan-baseline.neon')); +} + +$config['parameters']['phpVersion'] = PHP_VERSION_ID; + +return $config; \ No newline at end of file diff --git a/phpstan-baseline-8+.neon b/phpstan-baseline-8+.neon new file mode 100644 index 0000000..45a2f3a --- /dev/null +++ b/phpstan-baseline-8+.neon @@ -0,0 +1,496 @@ +parameters: + ignoreErrors: + - + message: "#^Method Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:closeFile\\(\\) has no return type specified\\.$#" + count: 1 + path: src/AbstractCsvFile.php + + - + message: "#^Method Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:openCsvFile\\(\\) has no return type specified\\.$#" + count: 1 + path: src/AbstractCsvFile.php + + - + message: "#^Method Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:openCsvFile\\(\\) has parameter \\$fileName with no type specified\\.$#" + count: 1 + path: src/AbstractCsvFile.php + + - + message: "#^Method Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:setFile\\(\\) has no return type specified\\.$#" + count: 1 + path: src/AbstractCsvFile.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvOptions\\:\\:validateDelimiter\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvOptions.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvOptions\\:\\:validateEnclosure\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvOptions.php + + - + message: "#^Class Keboola\\\\Csv\\\\CsvReader implements generic interface Iterator but does not specify its types\\: TKey, TValue$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvReader\\:\\:getHeader\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvReader\\:\\:openCsvFile\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvReader\\:\\:openCsvFile\\(\\) has parameter \\$fileName with no type specified\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvReader\\:\\:readLine\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvReader\\:\\:validateSkipLines\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Offset 'message' does not exist on array\\{type\\: int, message\\: string, file\\: string, line\\: int\\}\\|null\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Parameter \\#1 \\$header of static method Keboola\\\\Csv\\\\UTF8BOMHelper\\:\\:detectAndRemoveBOM\\(\\) expects array, array\\|false\\|null given\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Parameter \\#1 \\$sample of static method Keboola\\\\Csv\\\\LineBreaksHelper\\:\\:detectLineBreaks\\(\\) expects string, string\\|false given\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Parameter \\#1 \\$string of function trim expects string, string\\|false given\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Property Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:\\$filePointer \\(resource\\) does not accept resource\\|false\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Property Keboola\\\\Csv\\\\CsvReader\\:\\:\\$currentRow type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Property Keboola\\\\Csv\\\\CsvReader\\:\\:\\$header type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvReader.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:openCsvFile\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:rowToStr\\(\\) has parameter \\$row with no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:setLineBreak\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:validateLineBreak\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:writeRow\\(\\) has no return type specified\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\CsvWriter\\:\\:writeRow\\(\\) has parameter \\$row with no value type specified in iterable type array\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Offset 'message' does not exist on array\\{type\\: int, message\\: string, file\\: string, line\\: int\\}\\|null\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, bool\\|float\\|int\\|object\\|string given\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Property Keboola\\\\Csv\\\\AbstractCsvFile\\:\\:\\$filePointer \\(resource\\) does not accept resource\\|false\\.$#" + count: 1 + path: src/CsvWriter.php + + - + message: "#^Method Keboola\\\\Csv\\\\LineBreaksHelper\\:\\:clearCsvValues\\(\\) should return string but returns string\\|null\\.$#" + count: 1 + path: src/LineBreaksHelper.php + + - + message: "#^Method Keboola\\\\Csv\\\\UTF8BOMHelper\\:\\:detectAndRemoveBOM\\(\\) has parameter \\$header with no value type specified in iterable type array\\.$#" + count: 1 + path: src/UTF8BOMHelper.php + + - + message: "#^Method Keboola\\\\Csv\\\\UTF8BOMHelper\\:\\:detectAndRemoveBOM\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: src/UTF8BOMHelper.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvOptionsTest\\:\\:testAccessors\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvOptionsTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvOptionsTest\\:\\:testInvalidDelimiter\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvOptionsTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvOptionsTest\\:\\:testInvalidDelimiterEmpty\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvOptionsTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvOptionsTest\\:\\:testInvalidEnclosure\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvOptionsTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:bomProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:getPerformanceTestInputs\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:invalidDelimiterProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:invalidEnclosureProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:invalidSkipLinesProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testAccessors\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testColumnsCount\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testException\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testExistingFileShouldBeCreated\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testHeaderNoRewindOnGetHeader\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInitInvalidFileShouldThrowException\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidDelimiter\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidEnclosureShouldThrowException\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidFile\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidLineBreak\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidPointer\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testInvalidSkipLines\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testIterator\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testLineBreakWithoutRewind\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testLineEndingsDetection\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testParse\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testParseEscapedBy\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testParseMacLineEndsInField\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testPerformance\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testRead\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testReadPointer\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testSkipNoLines\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testSkipsHeaders\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testSkipsMultipleLines\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testSkipsOverflow\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testUtf8BOM\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testUtf8BOM\\(\\) has parameter \\$bomFile with no type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testValidWithoutRewind\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:testWriteReadInTheMiddle\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:validCsvFiles\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvReadTest\\:\\:validLineBreaksData\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Parameter \\#1 \\$file of class Keboola\\\\Csv\\\\CsvReader constructor expects resource\\|string, array\\ given\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Parameter \\#1 \\$file of class Keboola\\\\Csv\\\\CsvReader constructor expects resource\\|string, resource\\|false given\\.$#" + count: 2 + path: tests/CsvReadTest.php + + - + message: "#^Parameter \\#1 \\$stream of function fread expects resource, resource\\|false given\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Parameter \\#1 \\$stream of function rewind expects resource, resource\\|false given\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Parameter \\#5 \\$skipLines of class Keboola\\\\Csv\\\\CsvReader constructor expects int, mixed given\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Unreachable statement \\- code above always terminates\\.$#" + count: 1 + path: tests/CsvReadTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:invalidFileNameProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testAccessors\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testInvalidFile\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testInvalidFileName\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testInvalidPointer\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testInvalidPointer2\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testNewFileShouldBeCreated\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testNonStringWrite\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testWrite\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testWriteInvalidObject\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testWriteLineBreak\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testWritePointer\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\CsvWriteTest\\:\\:testWriteValidObject\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Parameter \\#1 \\$file of class Keboola\\\\Csv\\\\CsvWriter constructor expects resource\\|string, array\\ given\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Parameter \\#1 \\$file of class Keboola\\\\Csv\\\\CsvWriter constructor expects resource\\|string, resource\\|false given\\.$#" + count: 3 + path: tests/CsvWriteTest.php + + - + message: "#^Parameter \\#1 \\$stream of function fclose expects resource, resource\\|false given\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Parameter \\#1 \\$stream of function fwrite expects resource, resource\\|false given\\.$#" + count: 1 + path: tests/CsvWriteTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\LineBreaksHelperTest\\:\\:getDataSet\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/LineBreaksHelperTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\LineBreaksHelperTest\\:\\:testLineBreaksDetection\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/LineBreaksHelperTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\UTF8BOMHelperTest\\:\\:bomProvider\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/UTF8BOMHelperTest.php + + - + message: "#^Method Keboola\\\\Csv\\\\Tests\\\\UTF8BOMHelperTest\\:\\:testDetectAndRemoveBOM\\(\\) has no return type specified\\.$#" + count: 1 + path: tests/UTF8BOMHelperTest.php + + - + message: "#^Parameter \\#1 \\$header of static method Keboola\\\\Csv\\\\UTF8BOMHelper\\:\\:detectAndRemoveBOM\\(\\) expects array, mixed given\\.$#" + count: 1 + path: tests/UTF8BOMHelperTest.php diff --git a/phpstan.neon b/phpstan.neon index 2ee6a55..d1a1e72 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +1,2 @@ includes: - - phpstan-baseline.neon + - ignore-by-php-version.neon.php diff --git a/src/CsvReader.php b/src/CsvReader.php index de343f0..40e42ee 100644 --- a/src/CsvReader.php +++ b/src/CsvReader.php @@ -5,6 +5,7 @@ namespace Keboola\Csv; use Iterator; +use ReturnTypeWillChange; class CsvReader extends AbstractCsvFile implements Iterator { @@ -169,6 +170,7 @@ public function getLineBreak() /** * @inheritdoc */ + #[ReturnTypeWillChange] public function rewind() { rewind($this->getFilePointer()); @@ -217,6 +219,7 @@ public function getLineBreakAsText() /** * @inheritdoc */ + #[ReturnTypeWillChange] public function current() { return $this->currentRow; @@ -225,6 +228,7 @@ public function current() /** * @inheritdoc */ + #[ReturnTypeWillChange] public function next() { $this->currentRow = $this->readLine(); @@ -234,6 +238,7 @@ public function next() /** * @inheritdoc */ + #[ReturnTypeWillChange] public function key() { return $this->rowCounter; @@ -242,6 +247,7 @@ public function key() /** * @inheritdoc */ + #[ReturnTypeWillChange] public function valid() { return $this->currentRow !== false; diff --git a/src/CsvWriter.php b/src/CsvWriter.php index 10364ff..20e0599 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -2,6 +2,9 @@ namespace Keboola\Csv; +use TypeError; +use ValueError; + class CsvWriter extends AbstractCsvFile { /** @@ -63,7 +66,15 @@ private function validateLineBreak($lineBreak) */ protected function openCsvFile($fileName) { - $this->filePointer = @fopen($fileName, 'w'); + try { + $this->filePointer = @fopen($fileName, 'w'); + } catch (ValueError $e) { + throw new Exception( + "Cannot open file {$fileName} " . $e->getMessage(), + Exception::FILE_NOT_EXISTS, + $e + ); + } if (!$this->filePointer) { throw new Exception( "Cannot open file {$fileName} " . error_get_last()['message'], @@ -79,7 +90,18 @@ protected function openCsvFile($fileName) public function writeRow(array $row) { $str = $this->rowToStr($row); - $ret = @fwrite($this->getFilePointer(), $str); + try { + $ret = @fwrite($this->getFilePointer(), $str); + } catch (TypeError $e) { + throw new Exception( + 'Cannot write to CSV file ' . $this->fileName . + 'Error: ' . $e->getMessage() . + ' Return: false' . + ' To write: ' . strlen($str) . ' Written: 0', + Exception::WRITE_ERROR, + $e + ); + } /* According to http://php.net/fwrite the fwrite() function should return false on error. However not writing the full diff --git a/tests/CsvWriteTest.php b/tests/CsvWriteTest.php index a1058e7..72cd733 100644 --- a/tests/CsvWriteTest.php +++ b/tests/CsvWriteTest.php @@ -150,9 +150,16 @@ public function testInvalidFileName($filename, $message) public function invalidFileNameProvider() { + if (PHP_VERSION_ID < 80000) { + return [ + ['', 'Filename cannot be empty'], + ["\0", 'fopen() expects parameter 1 to be a valid path, string given'], + ]; + } + return [ - ['', 'Filename cannot be empty'], - ["\0", 'fopen() expects parameter 1 to be a valid path, string given'], + ['', 'Path cannot be empty'], + ["\0", 'Argument #1 ($filename) must not contain any null bytes'], ]; } @@ -212,6 +219,10 @@ public function testInvalidPointer() 'Cannot write to CSV file Error: fwrite(): ' . 'write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0' ), + new StringContains( + 'Cannot write to CSV file Error: fwrite(): ' . + 'Write of 14 bytes failed with errno=9 Bad file descriptor Return: false To write: 14 Written: 0' + ), ]); self::assertThat($e->getMessage(), $or); } @@ -274,4 +285,4 @@ public function testWriteLineBreak() $data ); } -} +} \ No newline at end of file From 7079eb7f31dee646924b7744f47a34eb72ca5783 Mon Sep 17 00:00:00 2001 From: Ondra Date: Wed, 22 Nov 2023 23:28:46 +0100 Subject: [PATCH 2/2] fix phpcs --- ignore-by-php-version.neon.php | 2 +- tests/CsvWriteTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ignore-by-php-version.neon.php b/ignore-by-php-version.neon.php index 8b59f51..8a26db4 100644 --- a/ignore-by-php-version.neon.php +++ b/ignore-by-php-version.neon.php @@ -15,4 +15,4 @@ $config['parameters']['phpVersion'] = PHP_VERSION_ID; -return $config; \ No newline at end of file +return $config; diff --git a/tests/CsvWriteTest.php b/tests/CsvWriteTest.php index 72cd733..c35425c 100644 --- a/tests/CsvWriteTest.php +++ b/tests/CsvWriteTest.php @@ -285,4 +285,4 @@ public function testWriteLineBreak() $data ); } -} \ No newline at end of file +}