Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a proper exception when calling getColumnName() with negative index #6505

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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