Skip to content

Commit da66c94

Browse files
authored
test(phpstan): Start fixing PHPStan level 6 warnings (#1142)
1 parent fdadd90 commit da66c94

66 files changed

Lines changed: 271 additions & 197 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/graphql_composable/src/Plugin/GraphQL/SchemaExtension/ComposableSchemaExampleExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ComposableSchemaExampleExtension extends SdlSchemaExtensionPluginBase {
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function registerResolvers(ResolverRegistryInterface $registry) {
24+
public function registerResolvers(ResolverRegistryInterface $registry): void {
2525
$builder = new ResolverBuilder();
2626

2727
$registry->addFieldResolver('Query', 'article',

examples/graphql_example/src/Plugin/GraphQL/Schema/ExampleSchema.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function getResolverRegistry() {
3535
* @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
3636
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
3737
*/
38-
protected function addArticleFields(ResolverRegistry $registry, ResolverBuilder $builder) {
38+
protected function addArticleFields(ResolverRegistry $registry, ResolverBuilder $builder): void {
3939
$registry->addFieldResolver('Article', 'id',
4040
$builder->produce('entity_id')
4141
->map('entity', $builder->fromParent())
@@ -64,7 +64,7 @@ protected function addArticleFields(ResolverRegistry $registry, ResolverBuilder
6464
* @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
6565
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
6666
*/
67-
protected function addQueryFields(ResolverRegistry $registry, ResolverBuilder $builder) {
67+
protected function addQueryFields(ResolverRegistry $registry, ResolverBuilder $builder): void {
6868
$registry->addFieldResolver('Query', 'article',
6969
$builder->produce('entity_load')
7070
->map('type', $builder->fromValue('node'))
@@ -84,7 +84,7 @@ protected function addQueryFields(ResolverRegistry $registry, ResolverBuilder $b
8484
* @param \Drupal\graphql\GraphQL\ResolverRegistry $registry
8585
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
8686
*/
87-
protected function addConnectionFields($type, ResolverRegistry $registry, ResolverBuilder $builder) {
87+
protected function addConnectionFields($type, ResolverRegistry $registry, ResolverBuilder $builder): void {
8888
$registry->addFieldResolver($type, 'total',
8989
$builder->callback(function (QueryConnection $connection) {
9090
return $connection->total();

examples/graphql_example/src/Plugin/GraphQL/SchemaExtension/ExampleSchemaExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ExampleSchemaExtension extends SdlSchemaExtensionPluginBase {
1919
/**
2020
* {@inheritdoc}
2121
*/
22-
public function registerResolvers(ResolverRegistryInterface $registry) {
22+
public function registerResolvers(ResolverRegistryInterface $registry): void {
2323
$builder = new ResolverBuilder();
2424

2525
$this->addQueryFields($registry, $builder);
@@ -30,7 +30,7 @@ public function registerResolvers(ResolverRegistryInterface $registry) {
3030
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
3131
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
3232
*/
33-
protected function addPageFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
33+
protected function addPageFields(ResolverRegistryInterface $registry, ResolverBuilder $builder): void {
3434
$registry->addFieldResolver('Page', 'id',
3535
$builder->produce('entity_id')
3636
->map('entity', $builder->fromParent())
@@ -50,7 +50,7 @@ protected function addPageFields(ResolverRegistryInterface $registry, ResolverBu
5050
* @param \Drupal\graphql\GraphQL\ResolverRegistryInterface $registry
5151
* @param \Drupal\graphql\GraphQL\ResolverBuilder $builder
5252
*/
53-
protected function addQueryFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
53+
protected function addQueryFields(ResolverRegistryInterface $registry, ResolverBuilder $builder): void {
5454
$registry->addFieldResolver('Query', 'page',
5555
$builder->produce('entity_load')
5656
->map('type', $builder->fromValue('node'))

graphql.install

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use GraphQL\Error\DebugFlag;
1111
/**
1212
* Implements hook_requirements().
1313
*/
14-
function graphql_requirements($phase) {
14+
function graphql_requirements(string $phase): array {
1515
// This is the first reference into the library performed by the module.
1616
$libraryAvailable = class_exists('\GraphQL\GraphQL');
1717

@@ -31,7 +31,7 @@ function graphql_requirements($phase) {
3131
/**
3232
* Implements hook_uninstall().
3333
*/
34-
function graphql_uninstall() {
34+
function graphql_uninstall(): void {
3535
// Remove the config keys set in GraphQLConfigOverrides::loadOverrides().
3636
/** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
3737
$configFactory = \Drupal::getContainer()->get('config.factory');
@@ -46,7 +46,7 @@ function graphql_uninstall() {
4646
/**
4747
* Update GraphQL Server debug configuration value.
4848
*/
49-
function graphql_update_8001() {
49+
function graphql_update_8001(): void {
5050
// The `debug` config item has changed to `debug_flag`. It is no longer a
5151
// boolean toggle but instead a set of flags providing more control. In this
5252
// case we default to debug messages and a backtrace in case debugging was

graphql.module

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
/**
99
* Implements hook_help().
1010
*/
11-
function graphql_help($routeName) {
11+
function graphql_help(string $routeName): ?string {
1212
if ($routeName !== 'help.page.graphql') {
13-
return;
13+
return NULL;
1414
}
1515

1616
$title = t('About');
@@ -32,7 +32,7 @@ EOT;
3232
/**
3333
* Implements hook_theme().
3434
*/
35-
function graphql_theme() {
35+
function graphql_theme(): array {
3636
return [
3737
'page__graphql_explorer' => [
3838
'render element' => 'elements',

phpcs.xml.dist

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
<exclude-pattern>src/GraphQL/ResolverBuilder.php</exclude-pattern>
1818
</rule>
1919

20+
<!-- We want to document the return type on the base class even when there is
21+
no return statement on the default implementation. -->
22+
<rule ref="Drupal.Commenting.FunctionComment.InvalidNoReturn">
23+
<exclude-pattern>src/GraphQL/Buffers/BufferBase.php</exclude-pattern>
24+
</rule>
25+
2026
<rule ref="Drupal.Commenting.DocComment.MissingShort">
2127
<exclude-pattern>src/Plugin/GraphQL/Schema/SdlSchemaPluginBase.php</exclude-pattern>
2228
<exclude-pattern>src/Plugin/GraphQL/Schema/ComposableSchema.php</exclude-pattern>

phpstan.neon

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ parameters:
88
# PHPStan cannot find files in this test directory automatically.
99
scanDirectories:
1010
- ../../core/tests/Drupal/Tests
11-
level: 5
11+
level: 6
1212
customRulesetUsed: true
1313
paths:
1414
- .
1515
# We test with PHPStan on Drupal 8 and 9 and might have different ignored
1616
# errors on both.
1717
reportUnmatchedIgnoredErrors: false
18+
# We inherit a lot of Drupal core docs that don't specify iterable types on
19+
# arrays, not sure we can fix this in a good way.
20+
checkMissingIterableValueType: false
21+
# Not sure we can specify generic types properly with Drupal coding standards
22+
# yet, disable for now.
23+
checkGenericClassInNonGenericObjectType: false
1824
ignoreErrors:
1925
# @todo Ignore phpstan-drupal extension's rules for now, activate later.
2026
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
@@ -46,3 +52,35 @@ parameters:
4652
count: 1
4753
path: src/Plugin/LanguageNegotiation/OperationLanguageNegotiation.php
4854
- "#^Parameter .+ of method Drupal\\\\Core\\\\Entity\\\\Query\\\\QueryInterface\\:\\:range\\(\\) expects null, int given\\.$#"
55+
# We forgot to use return type hints on some interfaces, cannot be changed
56+
# in stable 4.0.
57+
# @todo use return type hints everywhere for 5.0.
58+
-
59+
message: "#^Method Drupal\\\\graphql\\\\Entity\\\\ServerInterface\\:\\:addPersistedQueryInstance\\(\\) has no return typehint specified\\.$#"
60+
count: 1
61+
path: src/Entity/ServerInterface.php
62+
63+
-
64+
message: "#^Method Drupal\\\\graphql\\\\Entity\\\\ServerInterface\\:\\:removePersistedQueryInstance\\(\\) has no return typehint specified\\.$#"
65+
count: 1
66+
path: src/Entity/ServerInterface.php
67+
68+
-
69+
message: "#^Method Drupal\\\\graphql\\\\Entity\\\\ServerInterface\\:\\:removeAllPersistedQueryInstances\\(\\) has no return typehint specified\\.$#"
70+
count: 1
71+
path: src/Entity/ServerInterface.php
72+
73+
-
74+
message: "#^Method Drupal\\\\graphql\\\\Plugin\\\\PersistedQueryPluginInterface\\:\\:setWeight\\(\\) has no return typehint specified\\.$#"
75+
count: 1
76+
path: src/Plugin/PersistedQueryPluginInterface.php
77+
78+
-
79+
message: "#^Method Drupal\\\\graphql\\\\Plugin\\\\SchemaExtensionPluginInterface\\:\\:registerResolvers\\(\\) has no return typehint specified\\.$#"
80+
count: 1
81+
path: src/Plugin/SchemaExtensionPluginInterface.php
82+
83+
-
84+
message: "#^Unable to resolve the template type ExpectedType in call to method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\)$#"
85+
count: 1
86+
path: tests/src/Kernel/ResolverBuilderTest.php

src/Controller/ExplorerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ExplorerController implements ContainerInjectionInterface {
4545
*
4646
* @codeCoverageIgnore
4747
*/
48-
public static function create(ContainerInterface $container) {
48+
public static function create(ContainerInterface $container): self {
4949
return new static(
5050
$container->get('url_generator'),
5151
$container->get('graphql.introspection'),

src/Controller/RequestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RequestController implements ContainerInjectionInterface {
2525
*
2626
* @codeCoverageIgnore
2727
*/
28-
public static function create(ContainerInterface $container) {
28+
public static function create(ContainerInterface $container): self {
2929
return new static($container->getParameter('graphql.config'));
3030
}
3131

src/Controller/SubrequestExtractionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SubrequestExtractionController extends ControllerBase {
3434
*
3535
* @codeCoverageIgnore
3636
*/
37-
public static function create(ContainerInterface $container) {
37+
public static function create(ContainerInterface $container): self {
3838
return new static(
3939
$container->get('request_stack'),
4040
$container->get('language_manager'),

0 commit comments

Comments
 (0)