Skip to content

Commit a02d89a

Browse files
committed
DriverDetector is a service
1 parent de11651 commit a02d89a

File tree

7 files changed

+60
-51
lines changed

7 files changed

+60
-51
lines changed

extension.neon

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ services:
8989
class: PHPStan\Type\Doctrine\DefaultDescriptorRegistry
9090
factory: @PHPStan\Type\Doctrine\DescriptorRegistryFactory::createRegistry
9191

92+
-
93+
class: PHPStan\Doctrine\Driver\DriverDetector
94+
arguments:
95+
failOnInvalidConnection: %featureToggles.bleedingEdge%
9296
-
9397
class: PHPStan\Reflection\Doctrine\DoctrineSelectableClassReflectionExtension
9498
-
@@ -317,8 +321,6 @@ services:
317321
-
318322
class: PHPStan\Type\Doctrine\Descriptors\BooleanType
319323
tags: [phpstan.doctrine.typeDescriptor]
320-
arguments:
321-
bleedingEdge: %featureToggles.bleedingEdge%
322324
-
323325
class: PHPStan\Type\Doctrine\Descriptors\DateImmutableType
324326
tags: [phpstan.doctrine.typeDescriptor]
@@ -343,13 +345,9 @@ services:
343345
-
344346
class: PHPStan\Type\Doctrine\Descriptors\DecimalType
345347
tags: [phpstan.doctrine.typeDescriptor]
346-
arguments:
347-
bleedingEdge: %featureToggles.bleedingEdge%
348348
-
349349
class: PHPStan\Type\Doctrine\Descriptors\FloatType
350350
tags: [phpstan.doctrine.typeDescriptor]
351-
arguments:
352-
bleedingEdge: %featureToggles.bleedingEdge%
353351
-
354352
class: PHPStan\Type\Doctrine\Descriptors\GuidType
355353
tags: [phpstan.doctrine.typeDescriptor]

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ parameters:
5353
-
5454
message: '#^Call to function method_exists\(\) with Doctrine\\DBAL\\Connection and ''getNativeConnection'' will always evaluate to true\.$#' # needed for older DBAL versions
5555
paths:
56-
- src/Doctrine/Driver/DriverType.php
56+
- src/Doctrine/Driver/DriverDetector.php
5757

5858
-
5959
messages: # needed for older DBAL versions

src/Doctrine/Driver/DriverType.php renamed to src/Doctrine/Driver/DriverDetector.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use function method_exists;
2424
use function strpos;
2525

26-
class DriverType
26+
class DriverDetector
2727
{
2828

2929
public const IBM_DB2 = 'ibm_db2';
@@ -38,11 +38,18 @@ class DriverType
3838
public const SQLITE3 = 'sqlite3';
3939
public const SQLSRV = 'sqlsrv';
4040

41+
/** @var bool */
42+
private $failOnInvalidConnection;
43+
44+
public function __construct(bool $failOnInvalidConnection)
45+
{
46+
$this->failOnInvalidConnection = $failOnInvalidConnection;
47+
}
4148

4249
/**
4350
* @return self::*|null
4451
*/
45-
public static function detect(Connection $connection, bool $failOnInvalidConnection): ?string
52+
public function detect(Connection $connection): ?string
4653
{
4754
$driver = $connection->getDriver();
4855

@@ -99,7 +106,7 @@ public static function detect(Connection $connection, bool $failOnInvalidConnect
99106
try {
100107
$nativeConnection = $connection->getNativeConnection();
101108
} catch (Throwable $e) {
102-
if ($failOnInvalidConnection) {
109+
if ($this->failOnInvalidConnection) {
103110
throw $e;
104111
}
105112
return null; // connection cannot be established

src/Type/Doctrine/Descriptors/BooleanType.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
namespace PHPStan\Type\Doctrine\Descriptors;
44

55
use Doctrine\DBAL\Connection;
6-
use PHPStan\Doctrine\Driver\DriverType;
6+
use PHPStan\Doctrine\Driver\DriverDetector;
77
use PHPStan\Type\Constant\ConstantIntegerType;
88
use PHPStan\Type\Type;
99
use PHPStan\Type\TypeCombinator;
10+
use function in_array;
1011

1112
class BooleanType implements DoctrineTypeDescriptor, DoctrineTypeDriverAwareDescriptor
1213
{
1314

14-
/** @var bool */
15-
private $bleedingEdge;
15+
/** @var DriverDetector */
16+
private $driverDetector;
1617

17-
public function __construct(bool $bleedingEdge)
18+
public function __construct(DriverDetector $driverDetector)
1819
{
19-
$this->bleedingEdge = $bleedingEdge;
20+
$this->driverDetector = $driverDetector;
2021
}
2122

2223
public function getType(): string
@@ -45,18 +46,18 @@ public function getDatabaseInternalType(): Type
4546

4647
public function getDatabaseInternalTypeForDriver(Connection $connection): Type
4748
{
48-
$driverType = DriverType::detect($connection, $this->bleedingEdge);
49+
$driverType = $this->driverDetector->detect($connection);
4950

50-
if ($driverType === DriverType::PGSQL || $driverType === DriverType::PDO_PGSQL) {
51+
if ($driverType === DriverDetector::PGSQL || $driverType === DriverDetector::PDO_PGSQL) {
5152
return new \PHPStan\Type\BooleanType();
5253
}
5354

54-
if (
55-
$driverType === DriverType::SQLITE3
56-
|| $driverType === DriverType::PDO_SQLITE
57-
|| $driverType === DriverType::MYSQLI
58-
|| $driverType === DriverType::PDO_MYSQL
59-
) {
55+
if (in_array($driverType, [
56+
DriverDetector::SQLITE3,
57+
DriverDetector::PDO_SQLITE,
58+
DriverDetector::MYSQLI,
59+
DriverDetector::PDO_MYSQL,
60+
], true)) {
6061
return TypeCombinator::union(
6162
new ConstantIntegerType(0),
6263
new ConstantIntegerType(1)

src/Type/Doctrine/Descriptors/DecimalType.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,25 @@
33
namespace PHPStan\Type\Doctrine\Descriptors;
44

55
use Doctrine\DBAL\Connection;
6-
use PHPStan\Doctrine\Driver\DriverType;
6+
use PHPStan\Doctrine\Driver\DriverDetector;
77
use PHPStan\Type\Accessory\AccessoryNumericStringType;
88
use PHPStan\Type\FloatType;
99
use PHPStan\Type\IntegerType;
1010
use PHPStan\Type\IntersectionType;
1111
use PHPStan\Type\StringType;
1212
use PHPStan\Type\Type;
1313
use PHPStan\Type\TypeCombinator;
14+
use function in_array;
1415

1516
class DecimalType implements DoctrineTypeDescriptor, DoctrineTypeDriverAwareDescriptor
1617
{
1718

18-
/** @var bool */
19-
private $bleedingEdge;
19+
/** @var DriverDetector */
20+
private $driverDetector;
2021

21-
public function __construct(bool $bleedingEdge)
22+
public function __construct(DriverDetector $driverDetector)
2223
{
23-
$this->bleedingEdge = $bleedingEdge;
24+
$this->driverDetector = $driverDetector;
2425
}
2526

2627
public function getType(): string
@@ -45,18 +46,18 @@ public function getDatabaseInternalType(): Type
4546

4647
public function getDatabaseInternalTypeForDriver(Connection $connection): Type
4748
{
48-
$driverType = DriverType::detect($connection, $this->bleedingEdge);
49+
$driverType = $this->driverDetector->detect($connection);
4950

50-
if ($driverType === DriverType::SQLITE3 || $driverType === DriverType::PDO_SQLITE) {
51+
if ($driverType === DriverDetector::SQLITE3 || $driverType === DriverDetector::PDO_SQLITE) {
5152
return TypeCombinator::union(new FloatType(), new IntegerType());
5253
}
5354

54-
if (
55-
$driverType === DriverType::MYSQLI
56-
|| $driverType === DriverType::PDO_MYSQL
57-
|| $driverType === DriverType::PGSQL
58-
|| $driverType === DriverType::PDO_PGSQL
59-
) {
55+
if (in_array($driverType, [
56+
DriverDetector::MYSQLI,
57+
DriverDetector::PDO_MYSQL,
58+
DriverDetector::PGSQL,
59+
DriverDetector::PDO_PGSQL,
60+
], true)) {
6061
return new IntersectionType([
6162
new StringType(),
6263
new AccessoryNumericStringType(),

src/Type/Doctrine/Descriptors/FloatType.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
namespace PHPStan\Type\Doctrine\Descriptors;
44

55
use Doctrine\DBAL\Connection;
6-
use PHPStan\Doctrine\Driver\DriverType;
6+
use PHPStan\Doctrine\Driver\DriverDetector;
77
use PHPStan\Type\Accessory\AccessoryNumericStringType;
88
use PHPStan\Type\IntegerType;
99
use PHPStan\Type\IntersectionType;
1010
use PHPStan\Type\StringType;
1111
use PHPStan\Type\Type;
1212
use PHPStan\Type\TypeCombinator;
13+
use function in_array;
1314

1415
class FloatType implements DoctrineTypeDescriptor, DoctrineTypeDriverAwareDescriptor
1516
{
1617

17-
/** @var bool */
18-
private $bleedingEdge;
18+
/** @var DriverDetector */
19+
private $driverDetector;
1920

20-
public function __construct(bool $bleedingEdge)
21+
public function __construct(DriverDetector $driverDetector)
2122
{
22-
$this->bleedingEdge = $bleedingEdge;
23+
$this->driverDetector = $driverDetector;
2324
}
2425

2526
public function getType(): string
@@ -44,22 +45,22 @@ public function getDatabaseInternalType(): Type
4445

4546
public function getDatabaseInternalTypeForDriver(Connection $connection): Type
4647
{
47-
$driverType = DriverType::detect($connection, $this->bleedingEdge);
48+
$driverType = $this->driverDetector->detect($connection);
4849

49-
if ($driverType === DriverType::PDO_PGSQL) {
50+
if ($driverType === DriverDetector::PDO_PGSQL) {
5051
return new IntersectionType([
5152
new StringType(),
5253
new AccessoryNumericStringType(),
5354
]);
5455
}
5556

56-
if (
57-
$driverType === DriverType::SQLITE3
58-
|| $driverType === DriverType::PDO_SQLITE
59-
|| $driverType === DriverType::MYSQLI
60-
|| $driverType === DriverType::PDO_MYSQL
61-
|| $driverType === DriverType::PGSQL
62-
) {
57+
if (in_array($driverType, [
58+
DriverDetector::SQLITE3,
59+
DriverDetector::PDO_SQLITE,
60+
DriverDetector::MYSQLI,
61+
DriverDetector::PDO_MYSQL,
62+
DriverDetector::PGSQL,
63+
], true)) {
6364
return new \PHPStan\Type\FloatType();
6465
}
6566

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Composer\InstalledVersions;
88
use Doctrine\DBAL\Types\Type;
99
use Iterator;
10+
use PHPStan\Doctrine\Driver\DriverDetector;
1011
use PHPStan\Rules\Rule;
1112
use PHPStan\Testing\RuleTestCase;
1213
use PHPStan\Type\Doctrine\DefaultDescriptorRegistry;
@@ -70,7 +71,7 @@ protected function getRule(): Rule
7071
new DateTimeImmutableType(),
7172
new DateTimeType(),
7273
new DateType(),
73-
new DecimalType(true),
74+
new DecimalType(new DriverDetector(true)),
7475
new JsonType(),
7576
new IntegerType(),
7677
new StringType(),

0 commit comments

Comments
 (0)