Skip to content

Commit 69ccbbd

Browse files
authored
tests(phpstan): Enable PHPStan level 2 (drupal-graphql#1083)
1 parent 3c5c8fa commit 69ccbbd

20 files changed

+61
-36
lines changed

.travis.yml

+14-9
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,26 @@ install:
8888
# We don't care about any dirty changes in the vendor directory, throw them
8989
# all away.
9090
- composer --working-dir=$DRUPAL_BUILD_DIR config discard-changes true
91-
# Bring in the module dependencies without requiring a merge plugin. The
92-
# require also triggers a full 'composer install'.
93-
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require webonyx/graphql-php:^0.13.1 drupal/typed_data:^1.0
91+
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR install
92+
# Make sure to work with the latest PHPUnit version in Drupal 8.
9493
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR run-script drupal-phpunit-upgrade
94+
# Bring in the module dependencies without requiring a merge plugin.
95+
# Require redirect module to prevent PHPStan error on an optional dependency.
96+
# Install PHPStan to check for Drupal standards.
97+
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require \
98+
webonyx/graphql-php:^0.13.1 \
99+
drupal/typed_data:^1.0 \
100+
drupal/redirect:^1.6 \
101+
phpstan/phpstan:^0.12.50 \
102+
mglaman/phpstan-drupal:^0.12.3 \
103+
phpstan/phpstan-deprecation-rules:^0.12.2 \
104+
jangregor/phpstan-prophecy:^0.8 \
105+
phpstan/phpstan-phpunit:^0.12
95106

96107
# Install PHPCS to check for Drupal coding standards.
97108
- travis_retry composer global require drupal/coder:8.3.10
98109
- $HOME/.composer/vendor/bin/phpcs --config-set installed_paths $HOME/.composer/vendor/drupal/coder/coder_sniffer
99110

100-
# Install Phpstan to check for Drupal standards.
101-
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require mglaman/phpstan-drupal ^0.12.3 phpstan/phpstan-deprecation-rules ^0.12.2
102-
# Require redirect module to prevent PHPStan error on optional dependency
103-
# injection of RedirectRepository in RouteLoad data producer.
104-
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require drupal/redirect
105-
106111
script:
107112
# Run the unit tests using phpdbg if the environment variable is 'true'.
108113
- if [[ "$WITH_PHPDBG_COVERAGE" == "true" ]];

phpcs.xml.dist

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<rule ref="Drupal"></rule>
99
<rule ref="DrupalPractice"></rule>
1010

11+
<!-- TODO: Bug in Coder with variadic parameters -->
12+
<rule ref="Drupal.Commenting.FunctionComment.ParamTypeSpaces">
13+
<exclude-pattern>src/GraphQL/ResolverBuilder.php</exclude-pattern>
14+
</rule>
15+
<rule ref="Drupal.Commenting.FunctionComment.SpacingAfterParamType">
16+
<exclude-pattern>src/GraphQL/ResolverBuilder.php</exclude-pattern>
17+
</rule>
18+
1119
<!-- TODO: those rules are disabled for now until we fix the coding standards for them. -->
1220
<rule ref="Drupal.Commenting.ClassComment.Missing">
1321
<exclude-pattern>src/PermissionProvider.php</exclude-pattern>

phpstan.neon

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
includes:
22
- ../../vendor/phpstan/phpstan-deprecation-rules/rules.neon
33
- ../../vendor/mglaman/phpstan-drupal/extension.neon
4+
- ../../vendor/jangregor/phpstan-prophecy/extension.neon
5+
- ../../vendor/phpstan/phpstan-phpunit/extension.neon
46

57
parameters:
68
# PHPStan cannot find files in this test directory automatically.
79
scanDirectories:
810
- ../../core/tests/Drupal/Tests
9-
level: 1
11+
level: 2
1012
customRulesetUsed: true
1113
paths:
1214
- .
@@ -26,3 +28,4 @@ parameters:
2628
# Symfony 4 deprecations in Drupal 9 that we don't care about.
2729
- "#deprecated class Symfony\\\\Component\\\\EventDispatcher\\\\Event#"
2830
- "#deprecated class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseEvent#"
31+
- "#^Method Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\:\\:dispatch\\(\\) invoked with 2 parameters, 1 required\\.$#"

src/Access/QueryAccessCheck.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public function access(AccountInterface $account, ServerInterface $graphql_serve
5151

5252
$request = $this->requestStack->getCurrentRequest();
5353
/** @var \GraphQL\Server\OperationParams[] $operations */
54-
if (!$operations = $request->attributes->get('operations', [])) {
54+
$operations = $request->attributes->get('operations', []);
55+
if (!$operations) {
5556
return AccessResult::forbidden();
5657
}
5758

src/Form/PersistedQueriesForm.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class PersistedQueriesForm extends EntityForm {
1717
*/
1818
protected $persistedQueryPluginManager;
1919

20+
/**
21+
* The entity being used by this form.
22+
*
23+
* @var \Drupal\graphql\Entity\Server
24+
*/
25+
protected $entity;
26+
2027
/**
2128
* PersistedQueriesForm constructor.
2229
*
@@ -48,7 +55,7 @@ public function getFormId() {
4855
public function form(array $form, FormStateInterface $form_state) {
4956
$form = parent::form($form, $form_state);
5057

51-
/** @var PersistedQueryPluginInterface[] $plugins */
58+
/** @var \Drupal\graphql\Plugin\PersistedQueryPluginInterface[] $plugins */
5259
$plugins = $this->entity->getPersistedQueryInstances();
5360
$all_plugins = $this->getAllPersistedQueryPlugins();
5461
$form['#tree'] = TRUE;

src/Form/ServerForm.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ public function validateForm(array &$form, FormStateInterface $formState) {
218218
$formState->setErrorByName('endpoint', 'The endpoint path contains invalid characters.');
219219
}
220220

221-
/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
222221
$schema = $formState->getValue('schema');
222+
/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
223223
$instance = $this->schemaManager->createInstance($schema);
224224
if (!empty($form['schema_configuration'][$schema]) && $instance instanceof PluginFormInterface && $instance instanceof ConfigurableInterface) {
225225
$state = SubformState::createForSubform($form['schema_configuration'][$schema], $form, $formState);
@@ -233,8 +233,8 @@ public function validateForm(array &$form, FormStateInterface $formState) {
233233
public function submitForm(array &$form, FormStateInterface $formState) {
234234
parent::submitForm($form, $formState);
235235

236-
/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
237236
$schema = $formState->getValue('schema');
237+
/** @var \Drupal\graphql\Plugin\SchemaPluginInterface $instance */
238238
$instance = $this->schemaManager->createInstance($schema);
239239
if ($instance instanceof PluginFormInterface && $instance instanceof ConfigurableInterface) {
240240
$state = SubformState::createForSubform($form['schema_configuration'][$schema], $form, $formState);
@@ -248,13 +248,14 @@ public function submitForm(array &$form, FormStateInterface $formState) {
248248
* @throws \Drupal\Component\Plugin\Exception\PluginException
249249
*/
250250
public function save(array $form, FormStateInterface $formState) {
251-
parent::save($form, $formState);
251+
$save_result = parent::save($form, $formState);
252252

253253
$this->messenger()->addMessage($this->t('Saved the %label server.', [
254254
'%label' => $this->entity->label(),
255255
]));
256256

257257
$formState->setRedirect('entity.graphql_server.collection');
258+
return $save_result;
258259
}
259260

260261
}

src/GraphQL/Buffers/EntityRevisionBuffer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public function resolveBufferArray(array $buffer) {
6666
$vids = array_values(array_unique($vids));
6767

6868
// Load the buffered entities.
69-
$entities = $this->entityTypeManager
70-
->getStorage($type)
71-
->loadMultipleRevisions($vids);
69+
/** @var \Drupal\Core\Entity\RevisionableStorageInterface $storage */
70+
$storage = $this->entityTypeManager->getStorage($type);
71+
$entities = $storage->loadMultipleRevisions($vids);
7272

7373
return array_map(function ($item) use ($entities) {
7474
if (is_array($item['vid'])) {

src/GraphQL/ResolverBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function produce($id, array $config = []) {
3434
}
3535

3636
/**
37-
* @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface[]|array $resolvers
37+
* @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface ...$resolvers
3838
*
3939
* @return \Drupal\graphql\GraphQL\Resolver\Composite
4040
*/

src/PermissionProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PermissionProvider {
1111
/**
1212
* The entity type manager service.
1313
*
14-
* @var \Drupal\Core\Authentication\AuthenticationCollectorInterface
14+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
1515
*/
1616
protected $entityTypeManager;
1717

src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* required = FALSE
3131
* ),
3232
* "language" = @ContextDefinition("string",
33-
* label = @Translation("Entity languages"),
33+
* label = @Translation("Entity language"),
3434
* required = FALSE
3535
* ),
3636
* "bundles" = @ContextDefinition("string",

src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
* label = @Translation("Unique identifier")
3030
* ),
3131
* "language" = @ContextDefinition("string",
32-
* label = @Translation("Entity languages(s)"),
33-
* multiple = TRUE,
32+
* label = @Translation("Entity language"),
3433
* required = FALSE
3534
* ),
3635
* "bundles" = @ContextDefinition("string",
@@ -130,7 +129,7 @@ public function __construct(
130129
/**
131130
* @param string $type
132131
* @param string $uuid
133-
* @param array|null $language
132+
* @param string|null $language
134133
* @param array|null $bundles
135134
* @param bool|null $access
136135
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
@@ -139,7 +138,7 @@ public function __construct(
139138
*
140139
* @return \GraphQL\Deferred
141140
*/
142-
public function resolve($type, $uuid, ?array $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
141+
public function resolve($type, $uuid, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
143142
$resolver = $this->entityBuffer->add($type, $uuid);
144143

145144
return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {

src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,13 @@ public function __construct(
139139
*
140140
* @return \GraphQL\Deferred
141141
*/
142-
public function resolve($type, array $ids, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, string $accessOperation, FieldContext $context) {
142+
public function resolve($type, array $ids, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
143143
$resolver = $this->entityBuffer->add($type, $ids);
144144

145145
return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {
146146
/** @var \Drupal\Core\Entity\EntityInterface[] $entities */
147-
if (!$entities = $resolver()) {
147+
$entities = $resolver();
148+
if (!$entities) {
148149
// If there is no entity with this id, add the list cache tags so that
149150
// the cache entry is purged whenever a new entity of this type is
150151
// saved.

src/Plugin/GraphQL/DataProducer/Entity/EntityRendered.php

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public function resolve(EntityInterface $entity, $mode, RefinableCacheableDepend
104104
$view = $builder->view($entity, $mode, $entity->language()->getId());
105105

106106
$context = new RenderContext();
107-
/** @var \GraphQL\Executor\ExecutionResult|\GraphQL\Executor\ExecutionResult[] $result */
108107
$result = $this->renderer->executeInRenderContext($context, function () use ($view) {
109108
return $this->renderer->render($view);
110109
});

src/Plugin/GraphQL/DataProducer/Entity/EntityTranslation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public function __construct(array $configuration, $pluginId, $pluginDefinition,
9191
* @param \Drupal\Core\Entity\EntityInterface $entity
9292
* @param string $language
9393
* @param bool|null $access
94-
* @param \Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\AccountInterface|null $accessUser
94+
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
9595
* @param string|null $accessOperation
9696
*
97-
* @return |null
97+
* @return \Drupal\Core\Entity\EntityInterface|null
9898
*/
9999
public function resolve(EntityInterface $entity, $language, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation) {
100100
if ($entity instanceof TranslatableInterface && $entity->isTranslatable()) {

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields.php

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public function resolve(
122122
FieldContext $field_context
123123
): \Iterator {
124124
$entity_definition->getBundleEntityType();
125-
/** @var \Drupal\Core\Entity\ContentEntityType $value */
126125
if ($entity_definition instanceof ContentEntityType) {
127126
if ($bundle_context) {
128127
$key = $bundle_context['key'];

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/DefaultValue.php

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function resolve(FieldDefinitionInterface $entity_definition_field) {
4949
else {
5050
return $default_value;
5151
}
52+
return NULL;
5253
}
5354

5455
}

src/Plugin/GraphQL/DataProducer/EntityDefinition/Fields/Settings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Settings extends DataProducerPluginBase {
3636
* @param array|null $entity_form_display_context
3737
* Entity form display context.
3838
*
39-
* @return array
39+
* @return \Iterator
4040
* Field settings.
4141
*/
4242
public function resolve(

src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ public function resolve(string $vid, int $parent, ?int $max_depth, ?string $lang
159159

160160
return new Deferred(function () use ($language, $resolver, $context, $access, $accessUser, $accessOperation) {
161161
/** @var \Drupal\Core\Entity\EntityInterface[] $entities */
162-
if (!$entities = $resolver()) {
162+
$entities = $resolver();
163+
if (!$entities) {
163164
// If there is no entity with this id, add the list cache tags so that
164165
// the cache entry is purged whenever a new entity of this type is
165166
// saved.

src/Plugin/GraphQL/DataProducer/User/PasswordReset.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function resolve(string $email): ResponseInterface {
135135
// Show general error message also in case of unexpected response. Log to
136136
// watchdog for potential further investigation.
137137
if ($controller_response->getStatusCode() !== 200) {
138-
$this->logger->warning("Unexpected response code @code during password reset.", ['@code' => $response->getStatusCode()]);
138+
$this->logger->warning("Unexpected response code @code during password reset.", ['@code' => $controller_response->getStatusCode()]);
139139
$response->addViolation($this->t('Unable to reset password, please try again later.'));
140140
}
141141

tests/src/Traits/MockingTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ trait MockingTrait {
4646
* @param mixed $value
4747
* The return value. Can also be a value callback.
4848
*
49-
* @return \PHPUnit_Framework_MockObject_Stub_ReturnCallback
49+
* @return \PHPUnit\Framework\MockObject\Stub\ReturnCallback
5050
* The return callback promise.
5151
*/
5252
protected function toPromise($value) {
@@ -63,7 +63,7 @@ protected function toPromise($value) {
6363
* @param mixed $scope
6464
* The resolver's bound object and class scope.
6565
*
66-
* @return \PHPUnit_Framework_MockObject_Stub_ReturnCallback
66+
* @return \PHPUnit\Framework\MockObject\Stub\ReturnCallback
6767
* The return callback promise.
6868
*/
6969
protected function toBoundPromise($value, $scope) {

0 commit comments

Comments
 (0)