Skip to content

Commit

Permalink
Raise a proper exception when calling getColumnName() with negative…
Browse files Browse the repository at this point in the history
… index (#6505)

|      Q       |   A
|------------- | -----------
| Type         | bug
| Fixed issues | N/A

#### Summary

Calling `getColumnName()` with a negative index is not valid. We should
make sure that we handle this case consistently for all implementations.
  • Loading branch information
derrabus authored Aug 26, 2024
1 parent a41d81d commit 94dfede
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/Cache/ArrayResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ public function columnCount(): int

public function getColumnName(int $index): string
{
if ($this->data === [] || $index > count($this->data[0])) {
throw InvalidColumnIndex::new($index);
}

return array_keys($this->data[0])[$index];
return array_keys($this->data[0] ?? [])[$index]
?? throw InvalidColumnIndex::new($index);
}

public function free(): void
Expand Down
15 changes: 9 additions & 6 deletions src/Driver/PDO/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PDO;
use PDOException;
use PDOStatement;
use ValueError;

final class Result implements ResultInterface
{
Expand Down Expand Up @@ -78,15 +79,17 @@ public function getColumnName(int $index): string
{
try {
$meta = $this->statement->getColumnMeta($index);

if ($meta === false) {
throw InvalidColumnIndex::new($index);
}

return $meta['name'];
} catch (ValueError $exception) {
throw InvalidColumnIndex::new($index, $exception);
} catch (PDOException $exception) {
throw Exception::new($exception);
}

if ($meta === false) {
throw InvalidColumnIndex::new($index);
}

return $meta['name'];
}

public function free(): void
Expand Down
5 changes: 3 additions & 2 deletions src/Exception/InvalidColumnIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@

use Doctrine\DBAL\Exception;
use LogicException;
use Throwable;

use function sprintf;

/** @psalm-immutable */
final class InvalidColumnIndex extends LogicException implements Exception
{
public static function new(int $index): self
public static function new(int $index, ?Throwable $previous = null): self
{
return new self(sprintf('Invalid column index "%s".', $index));
return new self(sprintf('Invalid column index "%s".', $index), previous: $previous);
}
}
12 changes: 12 additions & 0 deletions tests/Cache/ArrayStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Doctrine\DBAL\Tests\Cache;

use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;

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

#[TestWith([2])]
#[TestWith([-1])]
public function testColumnNameWithInvalidIndex(int $index): void
{
$statement = $this->createTestArrayStatement();
$this->expectException(InvalidColumnIndex::class);

$statement->getColumnName($index);
}

public function testRowCount(): void
{
$statement = $this->createTestArrayStatement();
Expand Down
7 changes: 5 additions & 2 deletions tests/Functional/ResultMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use PHPUnit\Framework\Attributes\TestWith;

use function strtolower;

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

public function testColumnNameWithInvalidIndex(): void
#[TestWith([2])]
#[TestWith([-1])]
public function testColumnNameWithInvalidIndex(int $index): void
{
$sql = 'SELECT test_int, test_int AS alternate_name FROM result_metadata_table';

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

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

$result->getColumnName(2);
$result->getColumnName($index);
}

public function testColumnNameWithoutResults(): void
Expand Down

0 comments on commit 94dfede

Please sign in to comment.