From 2e646e2daf4085005e7d7df1c6997e4633b98c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Fri, 24 Mar 2023 14:18:08 +0100 Subject: [PATCH 1/5] Fix writing bool false value and little refactor to make code more readable --- src/CsvWriter.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/CsvWriter.php b/src/CsvWriter.php index 24452d4..a8f9163 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -142,9 +142,12 @@ public function rowToStr(array $row) ); } - $return[] = $this->getEnclosure() . - str_replace($this->getEnclosure(), str_repeat($this->getEnclosure(), 2), $column ?? '') . - $this->getEnclosure(); + $enclosure = $this->getEnclosure(); + $escapedEnclosure = str_repeat($enclosure, 2); + $columnValue = ($column === false) ? '0' : ($column ?? ''); + + $escapedColumn = str_replace($enclosure, $escapedEnclosure, $columnValue); + $return[] = sprintf("%s%s%s", $enclosure, $escapedColumn, $enclosure); } return implode($this->getDelimiter(), $return) . $this->lineBreak; } From d0f641dd79b6c2e6c79154858e6cde6d21cc995a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Fri, 24 Mar 2023 14:18:29 +0100 Subject: [PATCH 2/5] Test writing different data types --- tests/CsvWriteTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/CsvWriteTest.php b/tests/CsvWriteTest.php index c35425c..0f68603 100644 --- a/tests/CsvWriteTest.php +++ b/tests/CsvWriteTest.php @@ -53,6 +53,21 @@ public function testWrite() [ 'column with \n \t \\\\', 'second col', ], + [ + 1, true, + ], + [ + 2, false, + ], + [ + 3, null, + ], + [ + 'true', 1.123 + ], + [ + '1', 'null' + ] ]; foreach ($rows as $row) { @@ -70,6 +85,11 @@ public function testWrite() '"column with enclosure "", and comma inside text","second column enclosure in text """', "\"columns with\nnew line\",\"columns with\ttab\"", '"column with \\n \\t \\\\","second col"', + '"1","1"', + '"2","0"', + '"3",""', + '"true","1.123"', + '"1","null"', '', ] ), From d1fca0d10b0bb11975e54ab4ba8e6bfdfb4af6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Fri, 24 Mar 2023 14:20:55 +0100 Subject: [PATCH 3/5] PHPCS fixes --- src/CsvWriter.php | 2 +- tests/CsvWriteTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CsvWriter.php b/src/CsvWriter.php index a8f9163..790f15e 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -147,7 +147,7 @@ public function rowToStr(array $row) $columnValue = ($column === false) ? '0' : ($column ?? ''); $escapedColumn = str_replace($enclosure, $escapedEnclosure, $columnValue); - $return[] = sprintf("%s%s%s", $enclosure, $escapedColumn, $enclosure); + $return[] = sprintf('%s%s%s', $enclosure, $escapedColumn, $enclosure); } return implode($this->getDelimiter(), $return) . $this->lineBreak; } diff --git a/tests/CsvWriteTest.php b/tests/CsvWriteTest.php index 0f68603..d3d5507 100644 --- a/tests/CsvWriteTest.php +++ b/tests/CsvWriteTest.php @@ -63,11 +63,11 @@ public function testWrite() 3, null, ], [ - 'true', 1.123 + 'true', 1.123, ], [ - '1', 'null' - ] + '1', 'null', + ], ]; foreach ($rows as $row) { From 0a6843cca7bd35a75734b20ed8d77e8122ea4802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Fri, 24 Mar 2023 14:25:46 +0100 Subject: [PATCH 4/5] Update PHPStan baseline --- phpstan-baseline.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index a850342..89f517e 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -136,7 +136,7 @@ parameters: path: src/CsvWriter.php - - message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, bool\\|float\\|int\\|object\\|string given\\.$#" + message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, float\\|int\\|object\\|string\\|true given\\.$#" count: 1 path: src/CsvWriter.php From 283f002e5a8e293967810ee7ac7edfa3a5ece07d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=BDborn=C3=BD=20Adam?= Date: Thu, 6 Apr 2023 09:47:05 +0200 Subject: [PATCH 5/5] Fix PHPStan on PHP 8+ --- phpstan-baseline-8+.neon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpstan-baseline-8+.neon b/phpstan-baseline-8+.neon index 45a2f3a..e528d50 100644 --- a/phpstan-baseline-8+.neon +++ b/phpstan-baseline-8+.neon @@ -131,7 +131,7 @@ parameters: path: src/CsvWriter.php - - message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, bool\\|float\\|int\\|object\\|string given\\.$#" + message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, float\\|int\\|object\\|string\\|true given\\.$#" count: 1 path: src/CsvWriter.php