diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 01162324..7831329e 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,9 +1,20 @@ +build: + nodes: + analysis: + project_setup: + override: true + tests: + override: + - php-scrutinizer-run --enable-security-analysis filter: - paths: [src/*] - excluded_paths: [tests/*] + paths: + - src/ + excluded_paths: + - tests/ checks: php: code_rating: true + duplication: true tools: external_code_coverage: timeout: 600 @@ -11,7 +22,11 @@ tools: php_code_coverage: false php_loc: enabled: true - excluded_dirs: [tests, vendor] - php_cpd: + excluded_dirs: + - tests/ + - vendor/ + php_cpd: false + php_sim: enabled: true - excluded_dirs: [tests, vendor] + filter: + paths: ['src/'] \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index 408252e7..a0065fad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,8 +29,8 @@ install: script: - composer phpunit + - if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi + - if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi after_script: - if [ "$COLLECT_COVERAGE" == "true" ]; then wget https://scrutinizer-ci.com/ocular.phar && php ocular.phar code-coverage:upload --format=php-clover build/clover.xml; fi - - if [ "$VALIDATE_CODING_STYLE" == "true" ]; then composer phpcs; fi - - if [ "$RUN_PHPSTAN" == "true" ]; then composer phpstan; fi diff --git a/src/CharsetConverter.php b/src/CharsetConverter.php index 671839fd..99600608 100644 --- a/src/CharsetConverter.php +++ b/src/CharsetConverter.php @@ -208,17 +208,17 @@ public function __invoke(array $record): array /** * Walker method to convert the offset and the value of a CSV record field * - * @param string|null $value - * @param string|int $offset + * @param mixed $value + * @param mixed $offset */ protected function encodeField(&$value, &$offset) { - if (null !== $value) { - $value = mb_convert_encoding($value, $this->output_encoding, $this->input_encoding); + if (null !== $value && !is_numeric($value)) { + $value = mb_convert_encoding((string) $value, $this->output_encoding, $this->input_encoding); } - if (!is_int($offset)) { - $offset = mb_convert_encoding($offset, $this->output_encoding, $this->input_encoding); + if (!is_numeric($offset)) { + $offset = mb_convert_encoding((string) $offset, $this->output_encoding, $this->input_encoding); } } diff --git a/src/Writer.php b/src/Writer.php index 645a0876..557f0342 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -129,7 +129,7 @@ public function insertOne(array $record): int $record = array_reduce($this->formatters, [$this, 'formatRecord'], $record); $this->validateRecord($record); $bytes = $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape); - if (false !== $bytes && null !== $bytes) { + if ('' !== (string) $bytes) { return $bytes + $this->consolidate(); } diff --git a/tests/CharsetConverterTest.php b/tests/CharsetConverterTest.php index 3bee2f7d..6f4f8e48 100644 --- a/tests/CharsetConverterTest.php +++ b/tests/CharsetConverterTest.php @@ -150,4 +150,43 @@ public function testOnCreateFailedWithWrongParams() $converter->filtername = CharsetConverter::FILTERNAME.'.foo/bar'; $this->assertFalse($converter->onCreate()); } + + /** + * @covers ::convert + * @covers ::encodeField + * + * @dataProvider converterProvider + * @param array $record + * @param array $expected + */ + public function testConvertOnlyStringField(array $record, array $expected) + { + $converter = (new CharsetConverter()) + ->inputEncoding('iso-8859-15') + ->outputEncoding('utf-8'); + $res = $converter->convert([$record]); + $this->assertSame($expected, $res[0]); + } + + public function converterProvider() + { + return [ + 'only numeric values' => [ + 'record' => [1, 2, 3], + 'expected' => [1, 2, 3], + ], + 'only string values' => [ + 'record' => ['1', '2', '3'], + 'expected' => ['1', '2', '3'], + ], + 'mixed values' => [ + 'record' => [1, '2', 3], + 'expected' => [1, '2', 3], + ], + 'mixed offset' => [ + 'record' => [1 => 1, '2' => '2', 3 => 3], + 'expected' => [1 => 1, '2' => '2', 3 => 3], + ], + ]; + } }