Skip to content

Commit

Permalink
Merge branch '4.2.x' into 5.0.x
Browse files Browse the repository at this point in the history
* 4.2.x:
  Fix more PHPStan errors
  Use generators for large data providers (doctrine#6508)
  PHPStan 1.12 (doctrine#6507)
  Raise a proper exception when calling `getColumnName()` with negative index (doctrine#6505)
  • Loading branch information
derrabus committed Aug 28, 2024
2 parents 261590e + b9183ca commit d53d332
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 359 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"doctrine/coding-standard": "12.0.0",
"fig/log-test": "^1",
"jetbrains/phpstorm-stubs": "2023.2",
"phpstan/phpstan": "1.11.10",
"phpstan/phpstan": "1.12.0",
"phpstan/phpstan-phpunit": "1.4.0",
"phpstan/phpstan-strict-rules": "^1.6",
"phpunit/phpunit": "10.5.30",
Expand Down
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
2 changes: 1 addition & 1 deletion src/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function getMariaDbMysqlVersionNumber(string $versionString): string
'/^(?:5\.5\.5-)?(mariadb-)?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)/i',
$versionString,
$versionParts,
) === 0
) !== 1
) {
throw InvalidPlatformVersion::new(
$versionString,
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);
}
}
17 changes: 10 additions & 7 deletions src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,22 @@ protected function _getPortableTableColumnList(string $table, string $database,
*/
protected function _getPortableTableColumnDefinition(array $tableColumn): Column
{
preg_match('/^([^()]*)\\s*(\\(((\\d+)(,\\s*(\\d+))?)\\))?/', $tableColumn['type'], $matches);
$matchResult = preg_match('/^([^()]*)\\s*(\\(((\\d+)(,\\s*(\\d+))?)\\))?/', $tableColumn['type'], $matches);
assert($matchResult === 1);

$dbType = trim(strtolower($matches[1]));

$length = $precision = $unsigned = null;
$length = $precision = null;
$fixed = $unsigned = false;
$scale = 0;

if (count($matches) >= 6) {
$precision = (int) $matches[4];
$scale = (int) $matches[6];
} elseif (count($matches) >= 4) {
$length = (int) $matches[4];
if (isset($matches[4])) {
if (isset($matches[6])) {
$precision = (int) $matches[4];
$scale = (int) $matches[6];
} else {
$length = (int) $matches[4];
}
}

if (str_contains($dbType, ' unsigned')) {
Expand Down
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
Loading

0 comments on commit d53d332

Please sign in to comment.