Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Jan 17, 2024
1 parent f6a34b3 commit 6439355
Show file tree
Hide file tree
Showing 33 changed files with 425 additions and 55 deletions.
11 changes: 11 additions & 0 deletions src/Type/Doctrine/DescriptorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public function __construct(array $descriptors)
}
}

/**
* @throws DescriptorNotRegisteredException
*/
public function get(string $type): DoctrineTypeDescriptor
{
$typesMap = Type::getTypesMap();
Expand All @@ -36,4 +39,12 @@ public function get(string $type): DoctrineTypeDescriptor
return $this->descriptors[$typeClass];
}

public function getByClassName(string $className): DoctrineTypeDescriptor
{
if (!isset($this->descriptors[$className])) {
throw new DescriptorNotRegisteredException();
}
return $this->descriptors[$className];
}

}
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -24,7 +25,7 @@ public function getWritableToDatabaseType(): Type
return new \PHPStan\Type\ArrayType(new MixedType(), new MixedType());
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/AsciiStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

Expand All @@ -23,7 +24,7 @@ public function getWritableToDatabaseType(): Type
return new StringType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
5 changes: 3 additions & 2 deletions src/Type/Doctrine/Descriptors/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
Expand All @@ -23,10 +24,10 @@ public function getWritableToPropertyType(): Type

public function getWritableToDatabaseType(): Type
{
return TypeCombinator::union(new StringType(), new IntegerType());
return TypeCombinator::union(new StringType());
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new IntegerType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/BinaryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\MixedType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StringType;
Expand All @@ -25,7 +26,7 @@ public function getWritableToDatabaseType(): Type
return new MixedType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/BlobType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\MixedType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\Type;
Expand All @@ -24,7 +25,7 @@ public function getWritableToDatabaseType(): Type
return new MixedType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new MixedType();
}
Expand Down
9 changes: 8 additions & 1 deletion src/Type/Doctrine/Descriptors/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\PDO\PgSQL\Driver as PdoPgSQLDriver;
use Doctrine\DBAL\Driver\PgSQL\Driver as PgSQLDriver;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand All @@ -24,8 +27,12 @@ public function getWritableToDatabaseType(): Type
return new \PHPStan\Type\BooleanType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
if ($driver instanceof PgSQLDriver || $driver instanceof PdoPgSQLDriver) {
return new \PHPStan\Type\BooleanType();
}

return TypeCombinator::union(
new ConstantIntegerType(0),
new ConstantIntegerType(1)
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Doctrine\Descriptors;

use DateTimeImmutable;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -25,7 +26,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeImmutable::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateIntervalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Doctrine\Descriptors;

use DateInterval;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -25,7 +26,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateInterval::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateTimeImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Doctrine\Descriptors;

use DateTimeImmutable;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -25,7 +26,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeImmutable::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -26,7 +27,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeInterface::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateTimeTzImmutableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\Type\Doctrine\Descriptors;

use DateTimeImmutable;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -25,7 +26,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeImmutable::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateTimeTzType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -26,7 +27,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeInterface::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Driver;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
Expand All @@ -26,7 +27,7 @@ public function getWritableToDatabaseType(): Type
return new ObjectType(DateTimeInterface::class);
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
16 changes: 14 additions & 2 deletions src/Type/Doctrine/Descriptors/DecimalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\PDO\SQLite\Driver as PdoSqliteDriver;
use Doctrine\DBAL\Driver\SQLite3\Driver as Sqlite3Driver;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand All @@ -27,9 +31,17 @@ public function getWritableToDatabaseType(): Type
return TypeCombinator::union(new StringType(), new FloatType(), new IntegerType());
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return TypeCombinator::union(new FloatType(), new IntegerType());
if ($driver instanceof Sqlite3Driver || $driver instanceof PdoSqliteDriver) {
return new FloatType();
}

// TODO use mixed as fallback for any untested driver or some guess?
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]);
}

}
22 changes: 21 additions & 1 deletion src/Type/Doctrine/Descriptors/DoctrineTypeDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\Type;

/** @api */
Expand All @@ -13,10 +14,29 @@ interface DoctrineTypeDescriptor
*/
public function getType(): string;

/**
* This is used for inferring direct column results, e.g. SELECT e.field
* It should comply with convertToPHPValue return value
*/
public function getWritableToPropertyType(): Type;

public function getWritableToDatabaseType(): Type;

public function getDatabaseInternalType(): Type;
/**
* This is used for inferring how database fetches column of such type
* It should return the native type without stringification that may occur on certain PHP versions or driver configuration
*
* This is not used for direct column type inferring,
* but when such column appears in expression like SELECT MAX(e.field)
*
* See: https://github.com/janedbal/php-database-drivers-fetch-test
*
* mysql sqlite pdo_pgsql pgsql
* - decimal: string float string string
* - float: float float string float
* - bigint: int int int int
* - bool: int int bool bool
*/
public function getDatabaseInternalType(Driver $driver): Type;

}
15 changes: 13 additions & 2 deletions src/Type/Doctrine/Descriptors/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\PDO\PgSQL\Driver as PdoPgSQLDriver;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand All @@ -24,9 +29,15 @@ public function getWritableToDatabaseType(): Type
return TypeCombinator::union(new \PHPStan\Type\FloatType(), new IntegerType());
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return TypeCombinator::union(new \PHPStan\Type\FloatType(), new IntegerType());
if ($driver instanceof PdoPgSQLDriver) {
return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
]);
}
return new \PHPStan\Type\FloatType();
}

}
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/GuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

Expand All @@ -23,7 +24,7 @@ public function getWritableToDatabaseType(): Type
return new StringType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new StringType();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Descriptors/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Doctrine\Descriptors;

use Doctrine\DBAL\Driver;
use PHPStan\Type\Type;

class IntegerType implements DoctrineTypeDescriptor
Expand All @@ -22,7 +23,7 @@ public function getWritableToDatabaseType(): Type
return new \PHPStan\Type\IntegerType();
}

public function getDatabaseInternalType(): Type
public function getDatabaseInternalType(Driver $driver): Type
{
return new \PHPStan\Type\IntegerType();
}
Expand Down
Loading

0 comments on commit 6439355

Please sign in to comment.