Skip to content

Commit

Permalink
Fix PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 9, 2024
1 parent ce17b0e commit f3f1dae
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 29 deletions.
16 changes: 16 additions & 0 deletions compatibility/orm-3-baseline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

use Composer\InstalledVersions;

$includes = [];

$ormVersion = InstalledVersions::getVersion('doctrine/orm');
$hasOrm3 = $ormVersion !== null && strpos($ormVersion, '3.') === 0;
if ($hasOrm3) {
$includes[] = __DIR__ . '/../phpstan-baseline-orm-3.neon';
}

$config = [];
$config['includes'] = $includes;

return $config;
56 changes: 56 additions & 0 deletions phpstan-baseline-orm-3.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
parameters:
ignoreErrors:
-
message: "#^Call to function method_exists\\(\\) with 'Doctrine\\\\\\\\ORM\\\\\\\\EntityManager' and 'create' will always evaluate to false\\.$#"
count: 1
path: src/Doctrine/Mapping/ClassMetadataFactory.php

-
message: "#^Caught class Doctrine\\\\ORM\\\\ORMException not found\\.$#"
count: 1
path: src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php

-
message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ArrayType not found\\.$#"
count: 1
path: src/Type/Doctrine/Descriptors/ArrayType.php

-
message: "#^Method PHPStan\\\\Type\\\\Doctrine\\\\Descriptors\\\\ArrayType\\:\\:getType\\(\\) should return class\\-string\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\> 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\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\> 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\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\>, string given\\.$#"
count: 1
path: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Doctrine/Descriptors/BigIntType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ private function hasDbal4(): bool
}

$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
if ($dbalVersion === null) {
return false;
}

return strpos($dbalVersion, '4.') === 0;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Type/Doctrine/Query/QueryResultTypeWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Rules/Doctrine/ORM/DqlRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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.',
Expand Down
26 changes: 3 additions & 23 deletions tests/Rules/Doctrine/ORM/FakeTestingUuidType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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
Expand All @@ -81,7 +61,7 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
}

/**
* @return string[]
* @return array<int, string>
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit f3f1dae

Please sign in to comment.