Skip to content

Commit 4fc43bc

Browse files
committed
[SCA] Restrict the possible values for the casing options under the Portability\ NS
1 parent a9777b6 commit 4fc43bc

File tree

7 files changed

+31
-7
lines changed

7 files changed

+31
-7
lines changed

src/Portability/Converter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use function is_string;
1111
use function rtrim;
1212

13+
use const CASE_LOWER;
14+
use const CASE_UPPER;
15+
1316
final class Converter
1417
{
1518
/** @var callable */
@@ -35,6 +38,8 @@ final class Converter
3538
* @param bool $rightTrimString Whether each string should right-trimmed
3639
* @param int|null $case Convert the case of the column names
3740
* (one of {@see CASE_LOWER} and {@see CASE_UPPER})
41+
*
42+
* @phpstan-param CASE_LOWER|CASE_UPPER|null $case
3843
*/
3944
public function __construct(bool $convertEmptyStringToNull, bool $rightTrimString, ?int $case)
4045
{
@@ -185,6 +190,8 @@ private function createConvertValue(bool $convertEmptyStringToNull, bool $rightT
185190
* @param callable|null $function The function that will convert each value
186191
* @param int|null $case Column name case
187192
*
193+
* @phpstan-param CASE_LOWER|CASE_UPPER|null $case
194+
*
188195
* @return callable|null The resulting function or NULL if no conversion is needed
189196
*/
190197
private function createConvertRow(?callable $function, ?int $case): ?callable

src/Portability/Driver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ final class Driver extends AbstractDriverMiddleware
1818
{
1919
private int $mode;
2020

21+
/** @phpstan-var 0|ColumnCase::LOWER|ColumnCase::UPPER */
2122
private int $case;
2223

24+
/** @phpstan-param 0|ColumnCase::LOWER|ColumnCase::UPPER $case */
2325
public function __construct(DriverInterface $driver, int $mode, int $case)
2426
{
2527
parent::__construct($driver);
@@ -55,6 +57,8 @@ public function connect(
5557

5658
if ($nativeConnection instanceof PDO) {
5759
$portability &= ~Connection::PORTABILITY_FIX_CASE;
60+
// @todo: Create a mapping in order to handle the values for `PDO::ATTR_CASE` in a proper way, using the `PDO::CASE_*` constants.
61+
// @see: https://www.php.net/manual/en/pdo.setattribute.php.
5862
$nativeConnection->setAttribute(PDO::ATTR_CASE, $this->case);
5963
} else {
6064
$case = $this->case === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER;

src/Portability/Middleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
namespace Doctrine\DBAL\Portability;
66

7+
use Doctrine\DBAL\ColumnCase;
78
use Doctrine\DBAL\Driver as DriverInterface;
89
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
910

1011
final class Middleware implements MiddlewareInterface
1112
{
1213
private int $mode;
1314

15+
/** @phpstan-var 0|ColumnCase::LOWER|ColumnCase::UPPER */
1416
private int $case;
1517

18+
/** @phpstan-param 0|ColumnCase::LOWER|ColumnCase::UPPER $case */
1619
public function __construct(int $mode, int $case)
1720
{
1821
$this->mode = $mode;

src/Portability/OptimizeFlags.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class OptimizeFlags
1717
* Platform-specific portability flags that need to be excluded from the user-provided mode
1818
* since the platform already operates in this mode to avoid unnecessary conversion overhead.
1919
*
20-
* @var array<string,int>
20+
* @var array<class-string, int>
2121
*/
2222
private static array $platforms = [
2323
DB2Platform::class => 0,

tests/Functional/PortabilityTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function testFullFetchMode(): void
4646
}
4747

4848
/**
49-
* @param list<string> $expected
49+
* @param 0|ColumnCase::LOWER|ColumnCase::UPPER $case
50+
* @param list<string> $expected
5051
*
5152
* @dataProvider caseProvider
5253
*/
@@ -61,7 +62,7 @@ public function testCaseConversion(int $case, array $expected): void
6162
self::assertSame($expected, array_keys($row));
6263
}
6364

64-
/** @return iterable<string, array{int, list<string>}> */
65+
/** @return iterable<string, array{ColumnCase::LOWER|ColumnCase::UPPER, list<string>}> */
6566
public static function caseProvider(): iterable
6667
{
6768
yield 'lower' => [ColumnCase::LOWER, ['test_int', 'test_string', 'test_null']];
@@ -137,6 +138,7 @@ public function testGetDatabaseName(): void
137138
self::assertNotNull($this->connection->getDatabase());
138139
}
139140

141+
/** @phpstan-param 0|ColumnCase::LOWER|ColumnCase::UPPER $case */
140142
private function connectWithPortability(int $mode, int $case): void
141143
{
142144
// closing the default connection prior to 4.0.0 to prevent connection leak

tests/Portability/ConnectionTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use LogicException;
1010
use PHPUnit\Framework\TestCase;
1111

12+
use const CASE_LOWER;
13+
1214
class ConnectionTest extends TestCase
1315
{
1416
public function testGetServerVersion(): void
@@ -18,7 +20,7 @@ public function testGetServerVersion(): void
1820
->method('getServerVersion')
1921
->willReturn('1.2.3');
2022

21-
$connection = new Connection($driverConnection, new Converter(false, false, 0));
23+
$connection = new Connection($driverConnection, new Converter(false, false, CASE_LOWER));
2224

2325
self::assertSame('1.2.3', $connection->getServerVersion());
2426
}
@@ -27,7 +29,7 @@ public function testGetServerVersionFailsWithLegacyConnection(): void
2729
{
2830
$connection = new Connection(
2931
$this->createMock(DriverConnection::class),
30-
new Converter(false, false, 0),
32+
new Converter(false, false, CASE_LOWER),
3133
);
3234

3335
$this->expectException(LogicException::class);
@@ -43,7 +45,7 @@ public function testGetNativeConnection(): void
4345
$driverConnection->method('getNativeConnection')
4446
->willReturn($nativeConnection);
4547

46-
$connection = new Connection($driverConnection, new Converter(false, false, 0));
48+
$connection = new Connection($driverConnection, new Converter(false, false, CASE_LOWER));
4749

4850
self::assertSame($nativeConnection, $connection->getNativeConnection());
4951
}
@@ -52,7 +54,7 @@ public function testGetNativeConnectionFailsWithLegacyConnection(): void
5254
{
5355
$connection = new Connection(
5456
$this->createMock(DriverConnection::class),
55-
new Converter(false, false, 0),
57+
new Converter(false, false, CASE_LOWER),
5658
);
5759

5860
$this->expectException(LogicException::class);

tests/Portability/ConverterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77

88
use const CASE_LOWER;
9+
use const CASE_UPPER;
910

1011
class ConverterTest extends TestCase
1112
{
@@ -64,6 +65,8 @@ public static function convertNumericProvider(): iterable
6465
* @param array<string,mixed>|false $row
6566
* @param array<string,mixed>|false $expected
6667
*
68+
* @phpstan-param CASE_LOWER|CASE_UPPER|null $case
69+
*
6770
* @dataProvider convertAssociativeProvider
6871
*/
6972
public function testConvertAssociative(
@@ -279,6 +282,8 @@ public static function convertAllNumericProvider(): iterable
279282
* @param list<array<string,mixed>> $row
280283
* @param list<array<string,mixed>> $expected
281284
*
285+
* @phpstan-param CASE_LOWER|CASE_UPPER|null $case
286+
*
282287
* @dataProvider convertAllAssociativeProvider
283288
*/
284289
public function testConvertAllAssociative(
@@ -499,6 +504,7 @@ public static function convertFirstColumnProvider(): iterable
499504
];
500505
}
501506

507+
/** @phpstan-param CASE_LOWER|CASE_UPPER|null $case */
502508
private function createConverter(bool $convertEmptyStringToNull, bool $rightTrimString, ?int $case): Converter
503509
{
504510
return new Converter($convertEmptyStringToNull, $rightTrimString, $case);

0 commit comments

Comments
 (0)