Skip to content

Commit 9a3576b

Browse files
authored
CSVArray::toArray() returns array<int, array<string, string>> (#7)
1 parent 7958dd8 commit 9a3576b

File tree

8 files changed

+82
-64
lines changed

8 files changed

+82
-64
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/php-utils/releases).
99

1010
## Unreleased
1111

12+
## v1.6.1
13+
14+
### Fixed
15+
16+
- `CSVArray::toArray()` returns `array<int, array<string, string>>`
17+
1218
## v1.6.0
1319

1420
### Added

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ coverage: vendor ## Collects coverage from running unit tests with phpunit
1313

1414
.PHONY: fix
1515
fix: vendor
16-
vendor/bin/php-cs-fixer fix
1716
vendor/bin/rector process
17+
vendor/bin/php-cs-fixer fix
1818

1919
.PHONY: infection
2020
infection: vendor ## Runs mutation tests with infection

phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
</source>
1212

1313
<testsuites>
14-
<testsuite name="Unit">
15-
<directory>tests/Unit</directory>
14+
<testsuite name="Tests">
15+
<directory>tests</directory>
1616
</testsuite>
1717
</testsuites>
1818
</phpunit>

rector.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
use Rector\Set\ValueObject\SetList;
66

77
return static function (RectorConfig $rectorConfig): void {
8-
$rectorConfig->import(SetList::CODE_QUALITY);
9-
$rectorConfig->import(SetList::PHP_71);
10-
8+
$rectorConfig->sets([
9+
SetList::PHP_71,
10+
SetList::PHP_72,
11+
SetList::PHP_73,
12+
SetList::PHP_74,
13+
SetList::CODE_QUALITY,
14+
]);
1115
$rectorConfig->skip([
12-
// skip csv test file to keep `\r` and `\n` for readability
1316
JoinStringConcatRector::class => [
14-
// single file
15-
__DIR__ . '/tests/Unit/CSVArrayTest.php',
17+
__DIR__ . '/tests/CSVArrayTest.php', // keep `\r\n` for readability
1618
],
1719
]);
18-
19-
// paths to refactor; solid alternative to CLI arguments
2020
$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/tests']);
21-
22-
// Path to PHPStan with extensions, that PHPStan in Rector uses to determine types
2321
$rectorConfig->phpstanConfig(__DIR__ . '/phpstan.neon');
2422
};

src/CSVArray.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ final class CSVArray
1212
/**
1313
* TODO: fix parsing multiline-content in csv.
1414
*
15-
* @return array<int, array<string, CSVPrimitive>>
15+
* @return array<int, array<string, string>>
1616
*/
1717
public static function toArray(string $csv, string $delimiter = ';', string $enclosure = '"', string $escape = '\\'): array
1818
{
19+
/** @var array<int, array<string, string>> $result */
1920
$result = [];
2021

2122
$lines = StringUtil::splitLines($csv);
@@ -35,6 +36,7 @@ public static function toArray(string $csv, string $delimiter = ';', string $enc
3536
continue;
3637
}
3738

39+
/** @var array<int, string> $entries */
3840
$entries = str_getcsv($line, $delimiter, $enclosure, $escape);
3941
if (count($entries) !== count($columnHeaders)) {
4042
throw new \Exception("The number of columns in row {$index} does not match the headers in CSV: {$firstLine}");

tests/Unit/CSVArrayTest.php renamed to tests/CSVArrayTest.php

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,74 +5,86 @@
55
use MLL\Utils\CSVArray;
66
use PHPUnit\Framework\TestCase;
77

8-
class CSVArrayTest extends TestCase
8+
/**
9+
* @phpstan-import-type CSVPrimitive from CSVArray
10+
*/
11+
final class CSVArrayTest extends TestCase
912
{
10-
public const ARRAY_CONTENT = [
11-
1 => [
12-
'Spalte1' => 'Wert11',
13-
'Spalte2' => 'Wert21',
14-
],
15-
2 => [
16-
'Spalte1' => 'Wert12',
17-
'Spalte2' => 'Wert22',
18-
],
19-
];
20-
21-
public const CSV_CONTENT
22-
= "Spalte1;Spalte2\r\n"
23-
. "Wert11;Wert21\r\n"
24-
. "Wert12;Wert22\r\n";
25-
26-
protected function setUp(): void
27-
{
28-
parent::setUp();
29-
}
30-
31-
public function testToArray(): void
13+
/** @return iterable<array{string, array<int, array<string, string>>}> */
14+
public static function csvAndArrayStringValues(): iterable
3215
{
33-
self::assertSame(
34-
self::ARRAY_CONTENT,
35-
CSVArray::toArray(self::CSV_CONTENT)
36-
);
16+
yield [
17+
"Spalte1;Spalte2\r\n"
18+
. "Wert11;Wert21\r\n"
19+
. "Wert12;Wert22\r\n",
20+
[
21+
1 => [
22+
'Spalte1' => 'Wert11',
23+
'Spalte2' => 'Wert21',
24+
],
25+
2 => [
26+
'Spalte1' => 'Wert12',
27+
'Spalte2' => 'Wert22',
28+
],
29+
],
30+
];
31+
yield [
32+
"empty;int;float;bool;null\r\n"
33+
. ";1;2.3;true;null\r\n",
34+
[
35+
1 => [
36+
'empty' => '',
37+
'int' => '1',
38+
'float' => '2.3',
39+
'bool' => 'true',
40+
'null' => 'null',
41+
],
42+
],
43+
];
3744
}
3845

39-
public function testToCSV(): void
46+
/**
47+
* @dataProvider csvAndArrayStringValues
48+
*
49+
* @param array<int, array<string, string>> $array
50+
*/
51+
public function testStringValues(string $csv, array $array): void
4052
{
41-
self::assertSame(
42-
self::CSV_CONTENT,
43-
CSVArray::toCSV(self::ARRAY_CONTENT)
44-
);
53+
self::assertSame($array, CSVArray::toArray($csv));
54+
self::assertSame($csv, CSVArray::toCSV($array));
4555
}
4656

47-
public function testToCSVAndToArrayAreInverse(): void
57+
public function testEscapesDelimiter(): void
4858
{
4959
self::assertSame(
50-
self::ARRAY_CONTENT,
51-
CSVArray::toArray(
52-
CSVArray::toCSV(self::ARRAY_CONTENT)
53-
)
54-
);
55-
56-
self::assertSame(
57-
self::CSV_CONTENT,
60+
"foo\r\n"
61+
. "\"bar;baz\"\r\n",
5862
CSVArray::toCSV(
59-
CSVArray::toArray(self::CSV_CONTENT)
63+
[
64+
1 => [
65+
'foo' => 'bar;baz',
66+
],
67+
]
6068
)
6169
);
6270
}
6371

64-
public function testEscapesDelimiter(): void
72+
public function testPrimitives(): void
6573
{
6674
self::assertSame(
67-
"foo\r\n"
68-
. "\"bar;baz\"\r\n",
75+
"empty;int;float;bool;null\r\n"
76+
. ";1;2.3;1;\r\n",
6977
CSVArray::toCSV(
7078
[
7179
1 => [
72-
'foo' => 'bar;baz',
80+
'empty' => '',
81+
'int' => 1,
82+
'float' => 2.3,
83+
'bool' => true,
84+
'null' => null,
7385
],
74-
]
75-
)
86+
],
87+
),
7688
);
7789
}
7890

tests/Unit/NumberTest.php renamed to tests/NumberTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace MLL\Utils\Tests\Unit;
3+
namespace MLL\Utils\Tests;
44

55
use MLL\Utils\Number;
66
use PHPUnit\Framework\TestCase;

tests/Unit/StringUtilTest.php renamed to tests/StringUtilTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace MLL\Utils\Tests\Unit;
3+
namespace MLL\Utils\Tests;
44

55
use MLL\Utils\StringUtil;
66
use PHPUnit\Framework\TestCase;

0 commit comments

Comments
 (0)