Skip to content

Commit 69ccbbd

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

20 files changed

Lines changed: 61 additions & 36 deletions

.travis.yml

Lines changed: 14 additions & 9 deletions
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

Lines changed: 8 additions & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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",

0 commit comments

Comments
 (0)