Skip to content

Commit 031bf50

Browse files
committed
Fix getColumnName() with negative index
1 parent a41d81d commit 031bf50

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/Cache/ArrayResult.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,8 @@ public function columnCount(): int
8888

8989
public function getColumnName(int $index): string
9090
{
91-
if ($this->data === [] || $index > count($this->data[0])) {
92-
throw InvalidColumnIndex::new($index);
93-
}
94-
95-
return array_keys($this->data[0])[$index];
91+
return array_keys($this->data[0] ?? [])[$index]
92+
?? throw InvalidColumnIndex::new($index);
9693
}
9794

9895
public function free(): void

src/Driver/PDO/Result.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PDO;
1010
use PDOException;
1111
use PDOStatement;
12+
use ValueError;
1213

1314
final class Result implements ResultInterface
1415
{
@@ -78,15 +79,17 @@ public function getColumnName(int $index): string
7879
{
7980
try {
8081
$meta = $this->statement->getColumnMeta($index);
81-
82-
if ($meta === false) {
83-
throw InvalidColumnIndex::new($index);
84-
}
85-
86-
return $meta['name'];
82+
} catch (ValueError $exception) {
83+
throw InvalidColumnIndex::new($index, $exception);
8784
} catch (PDOException $exception) {
8885
throw Exception::new($exception);
8986
}
87+
88+
if ($meta === false) {
89+
throw InvalidColumnIndex::new($index);
90+
}
91+
92+
return $meta['name'];
9093
}
9194

9295
public function free(): void

src/Exception/InvalidColumnIndex.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66

77
use Doctrine\DBAL\Exception;
88
use LogicException;
9+
use Throwable;
910

1011
use function sprintf;
1112

1213
/** @psalm-immutable */
1314
final class InvalidColumnIndex extends LogicException implements Exception
1415
{
15-
public static function new(int $index): self
16+
public static function new(int $index, ?Throwable $previous = null): self
1617
{
17-
return new self(sprintf('Invalid column index "%s".', $index));
18+
return new self(sprintf('Invalid column index "%s".', $index), previous: $previous);
1819
}
1920
}

tests/Cache/ArrayStatementTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace Doctrine\DBAL\Tests\Cache;
66

77
use Doctrine\DBAL\Cache\ArrayResult;
8+
use Doctrine\DBAL\Exception\InvalidColumnIndex;
9+
use PHPUnit\Framework\Attributes\TestWith;
810
use PHPUnit\Framework\TestCase;
911

1012
use function array_values;
@@ -49,6 +51,16 @@ public function testColumnNames(): void
4951
self::assertSame('active', $statement->getColumnName(1));
5052
}
5153

54+
#[TestWith([2])]
55+
#[TestWith([-1])]
56+
public function testColumnNameWithInvalidIndex(int $index): void
57+
{
58+
$statement = $this->createTestArrayStatement();
59+
$this->expectException(InvalidColumnIndex::class);
60+
61+
$statement->getColumnName($index);
62+
}
63+
5264
public function testRowCount(): void
5365
{
5466
$statement = $this->createTestArrayStatement();

tests/Functional/ResultMetadataTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Exception\InvalidColumnIndex;
88
use Doctrine\DBAL\Schema\Table;
99
use Doctrine\DBAL\Tests\FunctionalTestCase;
10+
use PHPUnit\Framework\Attributes\TestWith;
1011

1112
use function strtolower;
1213

@@ -36,7 +37,9 @@ public function testColumnNameWithResults(): void
3637
self::assertEquals('alternate_name', strtolower($result->getColumnName(1)));
3738
}
3839

39-
public function testColumnNameWithInvalidIndex(): void
40+
#[TestWith([2])]
41+
#[TestWith([-1])]
42+
public function testColumnNameWithInvalidIndex(int $index): void
4043
{
4144
$sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table';
4245

@@ -47,7 +50,7 @@ public function testColumnNameWithInvalidIndex(): void
4750

4851
$this->expectException(InvalidColumnIndex::class);
4952

50-
$result->getColumnName(2);
53+
$result->getColumnName($index);
5154
}
5255

5356
public function testColumnNameWithoutResults(): void

0 commit comments

Comments
 (0)