Skip to content

Commit c96ba87

Browse files
authored
feat: Default missing columns to empty strings instead of throwing in CSVArray::toArray (#48)
- Default missing columns to empty strings instead of throwing in CSVArray::toArray - Ensure extensibility by using new static over new self everywhere
1 parent 5ddeacd commit c96ba87

File tree

11 files changed

+67
-26
lines changed

11 files changed

+67
-26
lines changed

CHANGELOG.md

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

1010
## Unreleased
1111

12+
## v5.12.0
13+
14+
### Added
15+
16+
- Default missing columns to empty strings instead of throwing in `CSVArray::toArray`
17+
- Ensure extensibility by using `new static` over `new self` everywhere
18+
1219
## v5.11.0
1320

1421
### Added

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ parameters:
99
# Install https://plugins.jetbrains.com/plugin/7677-awesome-console to make those links clickable
1010
editorUrl: '%%relFile%%:%%line%%'
1111
editorUrlTitle: '%%relFile%%:%%line%%'
12+
ignoreErrors:
13+
# This is a library, so it should be extendable
14+
- '#Unsafe usage of new static.*#'

src/CSVArray.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
/** @phpstan-type CSVPrimitive bool|float|int|string|\Stringable|null */
88
class CSVArray
99
{
10+
public const DEFAULT_EMPTY_VALUE = '';
11+
1012
/**
1113
* TODO: fix parsing multiline-content in csv.
1214
*
@@ -36,12 +38,8 @@ public static function toArray(string $csv, string $delimiter = ';', string $enc
3638

3739
/** @var array<int, string> $entries */
3840
$entries = str_getcsv($line, $delimiter, $enclosure, $escape);
39-
if (count($entries) !== count($columnHeaders)) {
40-
throw new \Exception("The number of columns in row {$index} does not match the headers in CSV: {$firstLine}");
41-
}
42-
4341
foreach ($columnHeaders as $columnIndex => $columnName) {
44-
$result[$index + 1][$columnName] = $entries[$columnIndex];
42+
$result[$index + 1][$columnName] = $entries[$columnIndex] ?? self::DEFAULT_EMPTY_VALUE;
4543
}
4644
}
4745

src/IlluminaSampleSheet/V2/BclConvert/CycleType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ public function __construct(string $value)
1818

1919
public static function READ_CYCLE(): self
2020
{
21-
return new self(self::READ_CYCLE);
21+
return new static(self::READ_CYCLE);
2222
}
2323

2424
public static function TRIMMED_CYCLE(): self
2525
{
26-
return new self(self::TRIMMED_CYCLE);
26+
return new static(self::TRIMMED_CYCLE);
2727
}
2828

2929
public static function UMI_CYCLE(): self
3030
{
31-
return new self(self::UMI_CYCLE);
31+
return new static(self::UMI_CYCLE);
3232
}
3333

3434
public static function INDEX_CYCLE(): self
3535
{
36-
return new self(self::INDEX_CYCLE);
36+
return new static(self::INDEX_CYCLE);
3737
}
3838
}

src/IlluminaSampleSheet/V2/Enums/FastQCompressionFormat.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function __construct(string $value)
1616

1717
public static function GZIP(): self
1818
{
19-
return new self(self::GZIP);
19+
return new static(self::GZIP);
2020
}
2121

2222
public static function DRAGEN(): self
2323
{
24-
return new self(self::DRAGEN);
24+
return new static(self::DRAGEN);
2525
}
2626
}

src/Microplate/Coordinates.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(string $row, int $column, CoordinateSystem $coordina
5454
*/
5555
public static function fromArray(array $coordinates, CoordinateSystem $coordinateSystem): self
5656
{
57-
return new self($coordinates['row'], $coordinates['column'], $coordinateSystem);
57+
return new static($coordinates['row'], $coordinates['column'], $coordinateSystem);
5858
}
5959

6060
/**
@@ -88,7 +88,7 @@ public static function fromString(string $coordinatesString, CoordinateSystem $c
8888
throw new \InvalidArgumentException("Expected coordinates between {$firstValidExample} and {$lastValidExample} for {$coordinateSystemClass}, got: {$coordinatesString}.");
8989
}
9090

91-
return new self($matches[1], (int) $matches[2], $coordinateSystem);
91+
return new static($matches[1], (int) $matches[2], $coordinateSystem);
9292
}
9393

9494
/**
@@ -104,14 +104,14 @@ public static function fromPosition(int $position, FlowDirection $direction, Coo
104104

105105
switch ($direction->value) {
106106
case FlowDirection::COLUMN:
107-
return new self(
107+
return new static(
108108
$coordinateSystem->rowForColumnFlowPosition($position),
109109
$coordinateSystem->columnForColumnFlowPosition($position),
110110
$coordinateSystem
111111
);
112112

113113
case FlowDirection::ROW:
114-
return new self(
114+
return new static(
115115
$coordinateSystem->rowForRowFlowPosition($position),
116116
$coordinateSystem->columnForRowFlowPosition($position),
117117
$coordinateSystem

src/Microplate/Enums/FlowDirection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function __construct(string $value)
1616

1717
public static function ROW(): self
1818
{
19-
return new self(self::ROW);
19+
return new static(self::ROW);
2020
}
2121

2222
public static function COLUMN(): self
2323
{
24-
return new self(self::COLUMN);
24+
return new static(self::COLUMN);
2525
}
2626
}

src/Tecan/LiquidClass/MLLLiquidClass.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ public function __construct(string $value)
1919

2020
public static function DNA_DILUTION(): self
2121
{
22-
return new self(self::DNA_DILUTION);
22+
return new static(self::DNA_DILUTION);
2323
}
2424

2525
public static function DNA_DILUTION_WATER(): self
2626
{
27-
return new self(self::DNA_DILUTION_WATER);
27+
return new static(self::DNA_DILUTION_WATER);
2828
}
2929

3030
public static function TRANSFER_PCR_PRODUKT(): self
3131
{
32-
return new self(self::TRANSFER_PCR_PRODUKT);
32+
return new static(self::TRANSFER_PCR_PRODUKT);
3333
}
3434

3535
public static function TRANSFER_MASTERMIX_MP(): self
3636
{
37-
return new self(self::TRANSFER_MASTERMIX_MP);
37+
return new static(self::TRANSFER_MASTERMIX_MP);
3838
}
3939

4040
public static function TRANSFER_TEMPLATE(): self
4141
{
42-
return new self(self::TRANSFER_TEMPLATE);
42+
return new static(self::TRANSFER_TEMPLATE);
4343
}
4444

4545
public function name(): string

src/Tecan/ReagentDistribution/ReagentDistributionDirection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function __construct(int $value)
1616

1717
public static function LEFT_TO_RIGHT(): self
1818
{
19-
return new self(self::LEFT_TO_RIGHT);
19+
return new static(self::LEFT_TO_RIGHT);
2020
}
2121

2222
public static function RIGHT_TO_LEFT(): self
2323
{
24-
return new self(self::RIGHT_TO_LEFT);
24+
return new static(self::RIGHT_TO_LEFT);
2525
}
2626
}

src/Tecan/TipMask/TipMask.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ public function __construct(string $value)
2020

2121
public static function FOUR_TIPS(): self
2222
{
23-
return new self(self::FOUR_TIPS);
23+
return new static(self::FOUR_TIPS);
2424
}
2525

2626
public static function EIGHT_TIPS(): self
2727
{
28-
return new self(self::EIGHT_TIPS);
28+
return new static(self::EIGHT_TIPS);
2929
}
3030

3131
public static function firstTip(): int

0 commit comments

Comments
 (0)