Skip to content

Commit f3f1dae

Browse files
committed
Fix PHPStan
1 parent ce17b0e commit f3f1dae

File tree

9 files changed

+86
-29
lines changed

9 files changed

+86
-29
lines changed

compatibility/orm-3-baseline.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Composer\InstalledVersions;
4+
5+
$includes = [];
6+
7+
$ormVersion = InstalledVersions::getVersion('doctrine/orm');
8+
$hasOrm3 = $ormVersion !== null && strpos($ormVersion, '3.') === 0;
9+
if ($hasOrm3) {
10+
$includes[] = __DIR__ . '/../phpstan-baseline-orm-3.neon';
11+
}
12+
13+
$config = [];
14+
$config['includes'] = $includes;
15+
16+
return $config;

phpstan-baseline-orm-3.neon

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Call to function method_exists\\(\\) with 'Doctrine\\\\\\\\ORM\\\\\\\\EntityManager' and 'create' will always evaluate to false\\.$#"
5+
count: 1
6+
path: src/Doctrine/Mapping/ClassMetadataFactory.php
7+
8+
-
9+
message: "#^Caught class Doctrine\\\\ORM\\\\ORMException not found\\.$#"
10+
count: 1
11+
path: src/Type/Doctrine/CreateQueryDynamicReturnTypeExtension.php
12+
13+
-
14+
message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ArrayType not found\\.$#"
15+
count: 1
16+
path: src/Type/Doctrine/Descriptors/ArrayType.php
17+
18+
-
19+
message: "#^Method PHPStan\\\\Type\\\\Doctrine\\\\Descriptors\\\\ArrayType\\:\\:getType\\(\\) should return class\\-string\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\> but returns string\\.$#"
20+
count: 1
21+
path: src/Type/Doctrine/Descriptors/ArrayType.php
22+
23+
-
24+
message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ObjectType not found\\.$#"
25+
count: 1
26+
path: src/Type/Doctrine/Descriptors/ObjectType.php
27+
28+
-
29+
message: "#^Method PHPStan\\\\Type\\\\Doctrine\\\\Descriptors\\\\ObjectType\\:\\:getType\\(\\) should return class\\-string\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\> but returns string\\.$#"
30+
count: 1
31+
path: src/Type/Doctrine/Descriptors/ObjectType.php
32+
33+
-
34+
message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#"
35+
count: 1
36+
path: src/Type/Doctrine/Query/QueryResultTypeWalker.php
37+
38+
-
39+
message: "#^Only booleans are allowed in \\|\\|, mixed given on the left side\\.$#"
40+
count: 2
41+
path: src/Type/Doctrine/Query/QueryResultTypeWalker.php
42+
43+
-
44+
message: "#^Caught class Doctrine\\\\ORM\\\\ORMException not found\\.$#"
45+
count: 1
46+
path: src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php
47+
48+
-
49+
message: "#^Class Doctrine\\\\DBAL\\\\Types\\\\ArrayType not found\\.$#"
50+
count: 1
51+
path: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php
52+
53+
-
54+
message: "#^Parameter \\#2 \\$className of static method Doctrine\\\\DBAL\\\\Types\\\\Type\\:\\:addType\\(\\) expects class\\-string\\<Doctrine\\\\DBAL\\\\Types\\\\Type\\>, string given\\.$#"
55+
count: 1
56+
path: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ includes:
33
- rules.neon
44
- phpstan-baseline.neon
55
- phpstan-baseline-dbal-3.neon
6+
- compatibility/orm-3-baseline.php
67
- vendor/phpstan/phpstan-strict-rules/rules.neon
78
- vendor/phpstan/phpstan-phpunit/extension.neon
89
- vendor/phpstan/phpstan-phpunit/rules.neon

src/Type/Doctrine/Descriptors/BigIntType.php

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ private function hasDbal4(): bool
4545
}
4646

4747
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
48+
if ($dbalVersion === null) {
49+
return false;
50+
}
4851

4952
return strpos($dbalVersion, '4.') === 0;
5053
}

src/Type/Doctrine/Query/QueryResultTypeWalker.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Type\Doctrine\Query;
44

55
use BackedEnum;
6+
use Doctrine\DBAL\Types\Types;
67
use Doctrine\ORM\EntityManagerInterface;
78
use Doctrine\ORM\Mapping\ClassMetadata;
89
use Doctrine\ORM\Query;
@@ -815,7 +816,7 @@ public function walkSelectExpression($selectExpression): string
815816
$type = $this->unmarshalType($expr->dispatch($this));
816817

817818
if (class_exists(TypedExpression::class) && $expr instanceof TypedExpression) {
818-
$enforcedType = $this->resolveDoctrineType($expr->getReturnType()->getName());
819+
$enforcedType = $this->resolveDoctrineType(Types::INTEGER);
819820
$type = TypeTraverser::map($type, static function (Type $type, callable $traverse) use ($enforcedType): Type {
820821
if ($type instanceof UnionType || $type instanceof IntersectionType) {
821822
return $traverse($type);

tests/Rules/Doctrine/ORM/DqlRuleTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function getRule(): Rule
2323
public function testRule(): void
2424
{
2525
$ormVersion = InstalledVersions::getVersion('doctrine/orm');
26-
if (strpos($ormVersion, '3.') === 0) {
26+
if ($ormVersion !== null && strpos($ormVersion, '3.') === 0) {
2727
$lexer = 'TokenType';
2828
} else {
2929
$lexer = 'Lexer';

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function testRule(?string $objectManagerLoader): void
171171
];
172172

173173
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
174-
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
174+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
175175
if (!$hasDbal4) {
176176
array_unshift($errors, [
177177
'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
242242
];
243243

244244
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
245-
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
245+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
246246
if (!$hasDbal4) {
247247
array_unshift($errors, [
248248
'Property PHPStan\Rules\Doctrine\ORM\MyBrokenEntity::$id type mapping mismatch: database can contain string but property expects int|null.',

tests/Rules/Doctrine/ORM/FakeTestingUuidType.php

+3-23
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
use Doctrine\DBAL\Types\GuidType;
88
use Ramsey\Uuid\Uuid;
99
use Ramsey\Uuid\UuidInterface;
10-
use Throwable;
11-
use function is_object;
1210
use function is_string;
13-
use function method_exists;
1411

1512
/**
1613
* From https://github.com/ramsey/uuid-doctrine/blob/fafebbe972cdaba9274c286ea8923e2de2579027/src/UuidType.php
@@ -36,13 +33,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?UuidInte
3633
return null;
3734
}
3835

39-
try {
40-
$uuid = Uuid::fromString($value);
41-
} catch (Throwable $e) {
42-
throw ConversionException::conversionFailed($value, self::NAME);
43-
}
44-
45-
return $uuid;
36+
return Uuid::fromString($value);
4637
}
4738

4839
/**
@@ -56,18 +47,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
5647
return null;
5748
}
5849

59-
if (
60-
$value instanceof UuidInterface
61-
|| (
62-
(is_string($value)
63-
|| (is_object($value) && method_exists($value, '__toString')))
64-
&& Uuid::isValid((string) $value)
65-
)
66-
) {
67-
return (string) $value;
68-
}
69-
70-
throw ConversionException::conversionFailed($value, self::NAME);
50+
return (string) $value;
7151
}
7252

7353
public function getName(): string
@@ -81,7 +61,7 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
8161
}
8262

8363
/**
84-
* @return string[]
64+
* @return array<int, string>
8565
*/
8666
public function getMappedDatabaseTypes(AbstractPlatform $platform): array
8767
{

tests/Type/Doctrine/Query/QueryResultTypeWalkerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ public function test(Type $expectedType, string $dql, ?string $expectedException
240240
public function getTestData(): iterable
241241
{
242242
$ormVersion = InstalledVersions::getVersion('doctrine/orm');
243-
$hasOrm3 = strpos($ormVersion, '3.') === 0;
243+
$hasOrm3 = $ormVersion !== null && strpos($ormVersion, '3.') === 0;
244244

245245
$dbalVersion = InstalledVersions::getVersion('doctrine/dbal');
246-
$hasDbal4 = strpos($dbalVersion, '4.') === 0;
246+
$hasDbal4 = $dbalVersion !== null && strpos($dbalVersion, '4.') === 0;
247247

248248
yield 'just root entity' => [
249249
new ObjectType(One::class),

0 commit comments

Comments
 (0)