Skip to content

Commit 42cca54

Browse files
authored
Allow disabling return type and stub extensions (#884)
* Allow disabling return type and stub extensions * classes still need to be marked as a service * update README.md
1 parent a514941 commit 42cca54

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ parameters:
108108
classExtendsInternalClassRule: false
109109
```
110110

111+
#### Disabling extensions
112+
113+
You can disable various extensions. This is useful when contributing to Drupal Core to improve its types.
114+
115+
```neon
116+
parameters:
117+
drupal:
118+
extensions:
119+
entityFieldsViaMagicReflection: true
120+
entityFieldMethodsViaMagicReflection: true
121+
entityQuery: true
122+
entityRepository: true
123+
stubFiles: true
124+
```
125+
126+
Both options are enabled by default.
127+
111128
### Entity storage mappings.
112129

113130
The `EntityTypeManagerGetStorageDynamicReturnTypeExtension` service helps map dynamic return types. This inspects the

extension.neon

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ parameters:
2121
checkDeprecatedHooksInApiFiles: false
2222
checkCoreDeprecatedHooksInApiFiles: false
2323
checkContribDeprecatedHooksInApiFiles: false
24+
extensions:
25+
entityFieldsViaMagicReflection: true
26+
entityFieldMethodsViaMagicReflection: true
27+
entityQuery: true
28+
entityRepository: true
29+
stubFiles: true
2430
rules:
2531
testClassSuffixNameRule: false
2632
dependencySerializationTraitPropertyRule: false
@@ -248,6 +254,13 @@ parametersSchema:
248254
checkCoreDeprecatedHooksInApiFiles: boolean()
249255
checkContribDeprecatedHooksInApiFiles: boolean()
250256
])
257+
extensions: structure([
258+
entityFieldsViaMagicReflection: boolean()
259+
entityFieldMethodsViaMagicReflection: boolean()
260+
entityQuery: boolean()
261+
entityRepository: boolean()
262+
stubFiles: boolean()
263+
])
251264
rules: structure([
252265
testClassSuffixNameRule: boolean()
253266
dependencySerializationTraitPropertyRule: boolean()
@@ -282,10 +295,8 @@ services:
282295
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
283296
-
284297
class: mglaman\PHPStanDrupal\Type\EntityRepositoryReturnTypeExtension
285-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
286298
-
287299
class: mglaman\PHPStanDrupal\Type\EntityStorage\GetQueryReturnTypeExtension
288-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
289300
-
290301
class: mglaman\PHPStanDrupal\Type\ContainerDynamicReturnTypeExtension
291302
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
@@ -294,10 +305,8 @@ services:
294305
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
295306
-
296307
class: mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryDynamicReturnTypeExtension
297-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
298308
-
299309
class: mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryAccessCheckDynamicReturnTypeExtension
300-
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
301310
-
302311
class: mglaman\PHPStanDrupal\Type\EntityAccessControlHandlerReturnTypeExtension
303312
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
@@ -312,17 +321,12 @@ services:
312321
tags: [phpstan.broker.dynamicStaticMethodReturnTypeExtension]
313322
-
314323
class: mglaman\PHPStanDrupal\Reflection\EntityFieldsViaMagicReflectionExtension
315-
tags: [phpstan.broker.propertiesClassReflectionExtension]
316324
-
317325
class: mglaman\PHPStanDrupal\Reflection\EntityFieldMethodsViaMagicReflectionExtension
318-
tags: [phpstan.broker.methodsClassReflectionExtension]
319326
-
320327
class: mglaman\PHPStanDrupal\Drupal\DrupalStubFilesExtension
321-
tags: [phpstan.stubFilesExtension]
322328
-
323329
class: mglaman\PHPStanDrupal\Type\EntityQuery\AccessCheckTypeSpecifyingExtension
324-
tags:
325-
- phpstan.typeSpecifier.methodTypeSpecifyingExtension
326330
-
327331
class: mglaman\PHPStanDrupal\DeprecatedScope\GroupLegacyScope
328332
tags:
@@ -338,4 +342,22 @@ services:
338342
-
339343
class: mglaman\PHPStanDrupal\Type\InspectorTypeExtension
340344
tags:
341-
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
345+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
346+
347+
conditionalTags:
348+
mglaman\PHPStanDrupal\Reflection\EntityFieldsViaMagicReflectionExtension:
349+
phpstan.broker.propertiesClassReflectionExtension: %drupal.extensions.entityFieldsViaMagicReflection%
350+
mglaman\PHPStanDrupal\Reflection\EntityFieldMethodsViaMagicReflectionExtension:
351+
phpstan.broker.methodsClassReflectionExtension: %drupal.extensions.entityFieldMethodsViaMagicReflection%
352+
mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryDynamicReturnTypeExtension:
353+
phpstan.broker.dynamicMethodReturnTypeExtension: %drupal.extensions.entityQuery%
354+
mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryAccessCheckDynamicReturnTypeExtension:
355+
phpstan.broker.dynamicMethodReturnTypeExtension: %drupal.extensions.entityQuery%
356+
mglaman\PHPStanDrupal\Type\EntityQuery\AccessCheckTypeSpecifyingExtension:
357+
phpstan.typeSpecifier.methodTypeSpecifyingExtension: %drupal.extensions.entityQuery%
358+
mglaman\PHPStanDrupal\Type\EntityStorage\GetQueryReturnTypeExtension:
359+
phpstan.broker.dynamicMethodReturnTypeExtension: %drupal.extensions.entityQuery%
360+
mglaman\PHPStanDrupal\Type\EntityRepositoryReturnTypeExtension:
361+
phpstan.broker.dynamicMethodReturnTypeExtension: %drupal.extensions.entityRepository%
362+
mglaman\PHPStanDrupal\Drupal\DrupalStubFilesExtension:
363+
phpstan.stubFilesExtension: %drupal.extensions.stubFiles%

src/Reflection/EntityFieldMethodsViaMagicReflectionExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414
class EntityFieldMethodsViaMagicReflectionExtension implements MethodsClassReflectionExtension
1515
{
16-
1716
public function hasMethod(ClassReflection $classReflection, string $methodName): bool
1817
{
18+
1919
if ($classReflection->hasNativeMethod($methodName) || array_key_exists($methodName, $classReflection->getMethodTags())) {
2020
// Let other parts of PHPStan handle this.
2121
return false;

tests/src/Reflection/EntityFieldMethodsViaMagicReflectionExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ public static function dataHasMethod(): \Generator
5353
false,
5454
];
5555
}
56+
5657

5758
}

tests/src/Reflection/EntityFieldsViaMagicReflectionExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testGetPropertyFieldItemList(): void
123123
$propertyReflection = $this->extension->getProperty($classReflection, 'format');
124124
$readableType = $propertyReflection->getReadableType();
125125
self::assertInstanceOf(NullType::class, $readableType);
126-
127126
}
127+
128128

129129
}

0 commit comments

Comments
 (0)