Skip to content

Commit 3fd0488

Browse files
lookymanondrejmirtes
authored andcommitted
Option to report unknown types
1 parent 0cb27d7 commit 3fd0488

11 files changed

+69
-11
lines changed

Diff for: rules.neon

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
doctrine:
33
reportDynamicQueryBuilders: false
4+
reportUnknownTypes: false
45

56
parametersSchema:
67
doctrine: structure([
@@ -11,13 +12,13 @@ parametersSchema:
1112
searchOtherMethodsForQueryBuilderBeginning: bool()
1213
queryBuilderFastAlgorithm: bool()
1314
reportDynamicQueryBuilders: bool()
15+
reportUnknownTypes: bool()
1416
])
1517

1618
rules:
1719
- PHPStan\Rules\Doctrine\ORM\DqlRule
1820
- PHPStan\Rules\Doctrine\ORM\MagicRepositoryMethodCallRule
1921
- PHPStan\Rules\Doctrine\ORM\RepositoryMethodCallRule
20-
- PHPStan\Rules\Doctrine\ORM\EntityColumnRule
2122
- PHPStan\Rules\Doctrine\ORM\EntityRelationRule
2223

2324
services:
@@ -27,3 +28,9 @@ services:
2728
reportDynamicQueryBuilders: %doctrine.reportDynamicQueryBuilders%
2829
tags:
2930
- phpstan.rules.rule
31+
-
32+
class: PHPStan\Rules\Doctrine\ORM\EntityColumnRule
33+
arguments:
34+
reportUnknownTypes: %doctrine.reportUnknownTypes%
35+
tags:
36+
- phpstan.rules.rule

Diff for: src/Rules/Doctrine/ORM/EntityColumnRule.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ class EntityColumnRule implements Rule
2323
/** @var \PHPStan\Type\Doctrine\DescriptorRegistry */
2424
private $descriptorRegistry;
2525

26-
public function __construct(ObjectMetadataResolver $objectMetadataResolver, DescriptorRegistry $descriptorRegistry)
26+
/** @var bool */
27+
private $reportUnknownTypes;
28+
29+
public function __construct(
30+
ObjectMetadataResolver $objectMetadataResolver,
31+
DescriptorRegistry $descriptorRegistry,
32+
bool $reportUnknownTypes
33+
)
2734
{
2835
$this->objectMetadataResolver = $objectMetadataResolver;
2936
$this->descriptorRegistry = $descriptorRegistry;
37+
$this->reportUnknownTypes = $reportUnknownTypes;
3038
}
3139

3240
public function getNodeType(): string
@@ -79,7 +87,12 @@ public function processNode(Node $node, Scope $scope): array
7987
try {
8088
$descriptor = $this->descriptorRegistry->get($fieldMapping['type']);
8189
} catch (DescriptorNotRegisteredException $e) {
82-
return [];
90+
return $this->reportUnknownTypes ? [sprintf(
91+
'Property %s::$%s: Doctrine type "%s" does not have any registered descriptor.',
92+
$className,
93+
$propertyName,
94+
$fieldMapping['type']
95+
)] : [];
8396
}
8497

8598
$identifier = null;

Diff for: tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\Doctrine\Descriptors\BinaryType;
1212
use PHPStan\Type\Doctrine\Descriptors\DateTimeImmutableType;
1313
use PHPStan\Type\Doctrine\Descriptors\DateTimeType;
14+
use PHPStan\Type\Doctrine\Descriptors\IntegerType;
1415
use PHPStan\Type\Doctrine\Descriptors\ReflectionDescriptor;
1516
use PHPStan\Type\Doctrine\Descriptors\StringType;
1617
use PHPStan\Type\Doctrine\ObjectMetadataResolver;
@@ -32,8 +33,10 @@ protected function getRule(): Rule
3233
new DateTimeType(),
3334
new DateTimeImmutableType(),
3435
new BinaryType(),
36+
new IntegerType(),
3537
new ReflectionDescriptor(CustomType::NAME, $this->createBroker()),
36-
])
38+
]),
39+
true
3740
);
3841
}
3942

@@ -115,4 +118,14 @@ public function testCustomType(): void
115118
]);
116119
}
117120

121+
public function testUnknownType(): void
122+
{
123+
$this->analyse([__DIR__ . '/data/EntityWithUnknownType.php'], [
124+
[
125+
'Property PHPStan\Rules\Doctrine\ORM\EntityWithUnknownType::$foo: Doctrine type "unknown" does not have any registered descriptor.',
126+
24,
127+
],
128+
]);
129+
}
130+
118131
}

Diff for: tests/Rules/Doctrine/ORM/data/AnotherEntity.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AnotherEntity
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithBrokenManyToManyRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithBrokenManyToManyRelations
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithBrokenManyToOneRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithBrokenManyToOneRelations
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithBrokenOneToManyRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithBrokenOneToManyRelations
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithBrokenOneToOneRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithBrokenOneToOneRelations
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithCustomType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithCustomType
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;

Diff for: tests/Rules/Doctrine/ORM/data/EntityWithRelations.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EntityWithRelations
1212

1313
/**
1414
* @ORM\Id()
15-
* @ORM\Column(type="int")
15+
* @ORM\Column(type="integer")
1616
* @var int
1717
*/
1818
private $id;
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Doctrine\ORM;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity()
9+
*/
10+
class EntityWithUnknownType
11+
{
12+
13+
/**
14+
* @ORM\Id()
15+
* @ORM\Column(type="integer")
16+
* @var int
17+
*/
18+
private $id;
19+
20+
/**
21+
* @ORM\Column(type="unknown")
22+
* @var int
23+
*/
24+
private $foo;
25+
}

0 commit comments

Comments
 (0)