Skip to content

Commit 2c6deea

Browse files
committed
Run entity mapping rules without objectManagerLoader only on bleedingEdge
1 parent b86c38c commit 2c6deea

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

rules.neon

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ services:
3838
arguments:
3939
reportUnknownTypes: %doctrine.reportUnknownTypes%
4040
allowNullablePropertyForRequiredField: %doctrine.allowNullablePropertyForRequiredField%
41+
bleedingEdge: %featureToggles.bleedingEdge%
4142
tags:
4243
- phpstan.rules.rule
4344
-
@@ -48,5 +49,6 @@ services:
4849
class: PHPStan\Rules\Doctrine\ORM\EntityRelationRule
4950
arguments:
5051
allowNullablePropertyForRequiredField: %doctrine.allowNullablePropertyForRequiredField%
52+
bleedingEdge: %featureToggles.bleedingEdge%
5153
tags:
5254
- phpstan.rules.rule

src/Rules/Doctrine/ORM/EntityColumnRule.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,24 @@ class EntityColumnRule implements Rule
4545
/** @var bool */
4646
private $allowNullablePropertyForRequiredField;
4747

48+
/** @var bool */
49+
private $bleedingEdge;
50+
4851
public function __construct(
4952
ObjectMetadataResolver $objectMetadataResolver,
5053
DescriptorRegistry $descriptorRegistry,
5154
ReflectionProvider $reflectionProvider,
5255
bool $reportUnknownTypes,
53-
bool $allowNullablePropertyForRequiredField
56+
bool $allowNullablePropertyForRequiredField,
57+
bool $bleedingEdge
5458
)
5559
{
5660
$this->objectMetadataResolver = $objectMetadataResolver;
5761
$this->descriptorRegistry = $descriptorRegistry;
5862
$this->reflectionProvider = $reflectionProvider;
5963
$this->reportUnknownTypes = $reportUnknownTypes;
6064
$this->allowNullablePropertyForRequiredField = $allowNullablePropertyForRequiredField;
65+
$this->bleedingEdge = $bleedingEdge;
6166
}
6267

6368
public function getNodeType(): string
@@ -67,6 +72,9 @@ public function getNodeType(): string
6772

6873
public function processNode(Node $node, Scope $scope): array
6974
{
75+
if (!$this->bleedingEdge && !$this->objectMetadataResolver->hasObjectManagerLoader()) {
76+
return [];
77+
}
7078
$class = $scope->getClassReflection();
7179
if ($class === null) {
7280
return [];

src/Rules/Doctrine/ORM/EntityRelationRule.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ class EntityRelationRule implements Rule
2828
/** @var bool */
2929
private $allowNullablePropertyForRequiredField;
3030

31+
/** @var bool */
32+
private $bleedingEdge;
33+
3134
public function __construct(
3235
ObjectMetadataResolver $objectMetadataResolver,
33-
bool $allowNullablePropertyForRequiredField
36+
bool $allowNullablePropertyForRequiredField,
37+
bool $bleedingEdge
3438
)
3539
{
3640
$this->objectMetadataResolver = $objectMetadataResolver;
3741
$this->allowNullablePropertyForRequiredField = $allowNullablePropertyForRequiredField;
42+
$this->bleedingEdge = $bleedingEdge;
3843
}
3944

4045
public function getNodeType(): string
@@ -44,6 +49,10 @@ public function getNodeType(): string
4449

4550
public function processNode(Node $node, Scope $scope): array
4651
{
52+
if (!$this->bleedingEdge && !$this->objectMetadataResolver->hasObjectManagerLoader()) {
53+
return [];
54+
}
55+
4756
$class = $scope->getClassReflection();
4857
if ($class === null) {
4958
return [];

src/Type/Doctrine/ObjectMetadataResolver.php

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public function __construct(
4242
$this->repositoryClass = $repositoryClass;
4343
}
4444

45+
public function hasObjectManagerLoader(): bool
46+
{
47+
return $this->objectManagerLoader !== null;
48+
}
49+
4550
/** @api */
4651
public function getObjectManager(): ?ObjectManager
4752
{

tests/Rules/Doctrine/ORM/EntityColumnRuleTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ protected function getRule(): Rule
7676
]),
7777
$this->createReflectionProvider(),
7878
true,
79-
$this->allowNullablePropertyForRequiredField
79+
$this->allowNullablePropertyForRequiredField,
80+
true
8081
);
8182
}
8283

tests/Rules/Doctrine/ORM/EntityRelationRuleTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ protected function getRule(): Rule
2323
{
2424
return new EntityRelationRule(
2525
new ObjectMetadataResolver($this->createReflectionProvider(), $this->objectManagerLoader, null),
26-
$this->allowNullablePropertyForRequiredField
26+
$this->allowNullablePropertyForRequiredField,
27+
true
2728
);
2829
}
2930

0 commit comments

Comments
 (0)