Skip to content

Commit 93d31c6

Browse files
committed
minor #1130 [Platform] Remove unused code in Vector constructor and add missing tests (camilleislasse)
This PR was merged into the main branch. Discussion ---------- [Platform] Remove unused code in Vector constructor and add missing tests | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | License | MIT The `Vector` constructor had duplicate validation code. The second check (line 36-38) could never be reached because the first check (line 28-30) already handles the same condition. Given [`$dimensions` is typed `?int`](https://github.com/symfony/ai/blob/main/src/platform/src/Vector/Vector.php#L26): - `null !== $dimensions` = "dimensions is not null" - `\is_int($dimensions)` = "dimensions is an int" When `$dimensions` is not null, it is an int. So both conditions are equivalent, making the second check unused code. This PR: - Removes the unreachable duplicate check - Adds missing test coverage for dimension validation Commits ------- 325377e [Platform] Remove dead code in Vector constructor and add missing tests
2 parents ef8a308 + 325377e commit 93d31c6

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/platform/src/Vector/Vector.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public function __construct(
3333
throw new InvalidArgumentException('Vector must have at least one dimension.');
3434
}
3535

36-
if (\is_int($dimensions) && \count($data) !== $dimensions) {
37-
throw new InvalidArgumentException(\sprintf('Vector must have %d dimensions', $dimensions));
38-
}
39-
4036
if (null === $this->dimensions) {
4137
$this->dimensions = \count($data);
4238
}

src/platform/tests/Vector/VectorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\AI\Platform\Tests\Vector;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Platform\Exception\InvalidArgumentException;
1516
use Symfony\AI\Platform\Vector\Vector;
1617
use Symfony\AI\Platform\Vector\VectorInterface;
1718

@@ -32,4 +33,27 @@ public function testWithDimensionNull()
3233
$this->assertSame($vectors, $vector->getData());
3334
$this->assertSame(3, $vector->getDimensions());
3435
}
36+
37+
public function testWithExplicitDimensions()
38+
{
39+
$vector = new Vector([1.0, 2.0, 3.0], 3);
40+
41+
$this->assertSame(3, $vector->getDimensions());
42+
}
43+
44+
public function testThrowsOnDimensionMismatch()
45+
{
46+
$this->expectException(InvalidArgumentException::class);
47+
$this->expectExceptionMessage('Vector must have 5 dimensions');
48+
49+
new Vector([1.0, 2.0], 5);
50+
}
51+
52+
public function testThrowsOnEmptyData()
53+
{
54+
$this->expectException(InvalidArgumentException::class);
55+
$this->expectExceptionMessage('Vector must have at least one dimension');
56+
57+
new Vector([]);
58+
}
3559
}

0 commit comments

Comments
 (0)