From f3f1dae3551ce4b10f5e2fb2c24e3004e6c34fe1 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Fri, 9 Feb 2024 14:30:24 +0100 Subject: [PATCH] Fix PHPStan --- compatibility/orm-3-baseline.php | 16 ++++++ phpstan-baseline-orm-3.neon | 56 +++++++++++++++++++ phpstan.neon | 1 + src/Type/Doctrine/Descriptors/BigIntType.php | 3 + .../Doctrine/Query/QueryResultTypeWalker.php | 3 +- tests/Rules/Doctrine/ORM/DqlRuleTest.php | 2 +- .../Doctrine/ORM/EntityColumnRuleTest.php | 4 +- .../Doctrine/ORM/FakeTestingUuidType.php | 26 +-------- .../Query/QueryResultTypeWalkerTest.php | 4 +- 9 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 compatibility/orm-3-baseline.php create mode 100644 phpstan-baseline-orm-3.neon diff --git a/compatibility/orm-3-baseline.php b/compatibility/orm-3-baseline.php new file mode 100644 index 00000000..69f1c589 --- /dev/null +++ b/compatibility/orm-3-baseline.php @@ -0,0 +1,16 @@ + but returns string\\.$#" + count: 1 + path: src/Type/Doctrine/Descriptors/ArrayType.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ObjectType not found\\.$#" + count: 1 + path: src/Type/Doctrine/Descriptors/ObjectType.php + + - + message: "#^Method PHPStan\\\\Type\\\\Doctrine\\\\Descriptors\\\\ObjectType\\:\\:getType\\(\\) should return class\\-string\\ but returns string\\.$#" + count: 1 + path: src/Type/Doctrine/Descriptors/ObjectType.php + + - + message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#" + count: 1 + path: src/Type/Doctrine/Query/QueryResultTypeWalker.php + + - + message: "#^Only booleans are allowed in \\|\\|, mixed given on the left side\\.$#" + count: 2 + path: src/Type/Doctrine/Query/QueryResultTypeWalker.php + + - + message: "#^Caught class Doctrine\\\\ORM\\\\ORMException not found\\.$#" + count: 1 + path: src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php + + - + message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ArrayType not found\\.$#" + count: 1 + path: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php + + - + message: "#^Parameter \\#2 \\$className of static method Doctrine\\\\DBAL\\\\Types\\\\Type\\:\\:addType\\(\\) expects class\\-string\\, string given\\.$#" + count: 1 + path: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php diff --git a/phpstan.neon b/phpstan.neon index aabd4939..b3c5d642 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,7 @@ includes: - rules.neon - phpstan-baseline.neon - phpstan-baseline-dbal-3.neon + - compatibility/orm-3-baseline.php - vendor/phpstan/phpstan-strict-rules/rules.neon - vendor/phpstan/phpstan-phpunit/extension.neon - vendor/phpstan/phpstan-phpunit/rules.neon diff --git a/src/Type/Doctrine/Descriptors/BigIntType.php b/src/Type/Doctrine/Descriptors/BigIntType.php index f74eea95..14b3ca2a 100644 --- a/src/Type/Doctrine/Descriptors/BigIntType.php +++ b/src/Type/Doctrine/Descriptors/BigIntType.php @@ -45,6 +45,9 @@ private function hasDbal4(): bool } $dbalVersion = InstalledVersions::getVersion('doctrine/dbal'); + if ($dbalVersion === null) { + return false; + } return strpos($dbalVersion, '4.') === 0; } diff --git a/src/Type/Doctrine/Query/QueryResultTypeWalker.php b/src/Type/Doctrine/Query/QueryResultTypeWalker.php index b4b5ddf5..c186a5e3 100644 --- a/src/Type/Doctrine/Query/QueryResultTypeWalker.php +++ b/src/Type/Doctrine/Query/QueryResultTypeWalker.php @@ -3,6 +3,7 @@ namespace PHPStan\Type\Doctrine\Query; use BackedEnum; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Query; @@ -815,7 +816,7 @@ public function walkSelectExpression($selectExpression): string $type = $this->unmarshalType($expr->dispatch($this)); if (class_exists(TypedExpression::class) && $expr instanceof TypedExpression) { - $enforcedType = $this->resolveDoctrineType($expr->getReturnType()->getName()); + $enforcedType = $this->resolveDoctrineType(Types::INTEGER); $type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($enforcedType): Type { if ($type instanceof UnionType || $type instanceof IntersectionType) { return $traverse($type); diff --git a/tests/Rules/Doctrine/ORM/DqlRuleTest.php b/tests/Rules/Doctrine/ORM/DqlRuleTest.php index c601fe54..007ed40b 100644 --- a/tests/Rules/Doctrine/ORM/DqlRuleTest.php +++ b/tests/Rules/Doctrine/ORM/DqlRuleTest.php @@ -23,7 +23,7 @@ protected function getRule(): Rule public function testRule(): void { $ormVersion = InstalledVersions::getVersion('doctrine/orm'); - if (strpos($ormVersion, '3.') === 0) { + if ($ormVersion !== null && strpos($ormVersion, '3.') === 0) { $lexer = 'TokenType'; } else { $lexer = 'Lexer'; diff --git a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php index 36308989..57561ef4 100644 --- a/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php +++ b/tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php @@ -171,7 +171,7 @@ public function testRule(?string $objectManagerLoader): void ]; $dbalVersion = InstalledVersions::getVersion('doctrine/dbal'); - $hasDbal4 = strpos($dbalVersion, '4.') === 0; + $hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0; if (!$hasDbal4) { array_unshift($errors, [ 'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.', @@ -242,7 +242,7 @@ public function testRuleWithAllowedNullableProperty(?string $objectManagerLoader ]; $dbalVersion = InstalledVersions::getVersion('doctrine/dbal'); - $hasDbal4 = strpos($dbalVersion, '4.') === 0; + $hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0; if (!$hasDbal4) { array_unshift($errors, [ 'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.', diff --git a/tests/Rules/Doctrine/ORM/FakeTestingUuidType.php b/tests/Rules/Doctrine/ORM/FakeTestingUuidType.php index 12388012..fcb28f7e 100644 --- a/tests/Rules/Doctrine/ORM/FakeTestingUuidType.php +++ b/tests/Rules/Doctrine/ORM/FakeTestingUuidType.php @@ -7,10 +7,7 @@ use Doctrine\DBAL\Types\GuidType; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; -use Throwable; -use function is_object; use function is_string; -use function method_exists; /** * From https://github.com/ramsey/uuid-doctrine/blob/fafebbe972cdaba9274c286ea8923e2de2579027/src/UuidType.php @@ -36,13 +33,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte return null; } - try { - $uuid = Uuid::fromString($value); - } catch (Throwable $e) { - throw ConversionException::conversionFailed($value, self::NAME); - } - - return $uuid; + return Uuid::fromString($value); } /** @@ -56,18 +47,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str return null; } - if ( - $value instanceof UuidInterface - || ( - (is_string($value) - || (is_object($value) && method_exists($value, '__toString'))) - && Uuid::isValid((string) $value) - ) - ) { - return (string) $value; - } - - throw ConversionException::conversionFailed($value, self::NAME); + return (string) $value; } public function getName(): string @@ -81,7 +61,7 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool } /** - * @return string[] + * @return array */ public function getMappedDatabaseTypes(AbstractPlatform $platform): array { diff --git a/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php b/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php index 5b49bff8..27d1acab 100644 --- a/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php +++ b/tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php @@ -240,10 +240,10 @@ public function test(Type $expectedType, string $dql, ?string $expectedException public function getTestData(): iterable { $ormVersion = InstalledVersions::getVersion('doctrine/orm'); - $hasOrm3 = strpos($ormVersion, '3.') === 0; + $hasOrm3 = $ormVersion !== null && strpos($ormVersion, '3.') === 0; $dbalVersion = InstalledVersions::getVersion('doctrine/dbal'); - $hasDbal4 = strpos($dbalVersion, '4.') === 0; + $hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0; yield 'just root entity' => [ new ObjectType(One::class),