Skip to content

Commit dad9d59

Browse files
authored
Phpstan first batch of fixes (#1075)
1 parent f2ebeab commit dad9d59

12 files changed

Lines changed: 44 additions & 24 deletions

.travis.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ install:
6868
# Download Drupal 8 core from the Github mirror because it is faster.
6969
- git clone --branch $DRUPAL_CORE --depth 1 https://github.com/drupal/drupal.git $DRUPAL_BUILD_DIR
7070

71-
# Reference the module in the build site.
72-
- ln -s $TRAVIS_BUILD_DIR $DRUPAL_BUILD_DIR/modules/graphql
71+
# Copy the module to the build site. This makes PHPStan find drupal reliably.
72+
- cp -r $TRAVIS_BUILD_DIR $DRUPAL_BUILD_DIR/modules/graphql
7373

7474
# Copy the customized phpunit configuration file to the core directory so
7575
# the relative paths are correct.
@@ -94,6 +94,12 @@ install:
9494
- travis_retry composer global require drupal/coder:8.3.10
9595
- $HOME/.composer/vendor/bin/phpcs --config-set installed_paths $HOME/.composer/vendor/drupal/coder/coder_sniffer
9696

97+
# Install Phpstan to check for Drupal standards.
98+
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require mglaman/phpstan-drupal ^0.12.3 phpstan/phpstan-deprecation-rules ^0.12.2
99+
# Require redirect module to prevent PHPStan error on optional dependency
100+
# injection of RedirectRepository in RouteLoad data producer.
101+
- composer --no-interaction --working-dir=$DRUPAL_BUILD_DIR require drupal/redirect
102+
97103
script:
98104
# Run the unit tests using phpdbg if the environment variable is 'true'.
99105
- if [[ "$WITH_PHPDBG_COVERAGE" == "true" ]];
@@ -107,6 +113,9 @@ script:
107113
- if [[ "$WITH_PHPDBG_COVERAGE" != "true" ]];
108114
then $DRUPAL_BUILD_DIR/vendor/bin/phpunit --configuration $DRUPAL_BUILD_DIR/core/phpunit.xml $TRAVIS_BUILD_DIR;
109115
fi
116+
117+
# Run Phpstan
118+
- cd $DRUPAL_BUILD_DIR/modules/graphql && $DRUPAL_BUILD_DIR/vendor/bin/phpstan analyse
110119

111120
after_success:
112121
- if [[ "$WITH_PHPDBG_COVERAGE" == "true" ]];

phpstan.neon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
includes:
2+
- ../../vendor/phpstan/phpstan-deprecation-rules/rules.neon
3+
- ../../vendor/mglaman/phpstan-drupal/extension.neon
4+
5+
parameters:
6+
level: 1
7+
customRulesetUsed: true
8+
paths:
9+
- .
10+
ignoreErrors:
11+
# @todo Ignore phpstan-drupal extension's rules for now, activate later.
12+
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
13+
# new static() is a best practice in Drupal, so we cannot fix that.
14+
- "#^Unsafe usage of new static\\(\\)\\.$#"
15+
# Drupal allows object property access to custom fields, so we cannot fix
16+
# that.
17+
- "#^Access to an undefined property Drupal\\\\#"

src/Plugin/DataProducerPluginManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct(
6969

7070
$this->alterInfo('graphql_data_producer');
7171
$this->useCaches(empty($config['development']));
72-
$this->setCacheBackend($definitionCacheBackend, 'producer', ['graphql']);
72+
$this->setCacheBackend($definitionCacheBackend, 'graphql_data_producer', ['graphql_data_producer']);
7373

7474
$this->requestStack = $requestStack;
7575
$this->contextsManager = $contextsManager;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __construct(
130130
/**
131131
* @param string $type
132132
* @param string $id
133-
* @param string $language
133+
* @param string|null $language
134134
* @param array|null $bundles
135135
* @param bool|null $access
136136
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
@@ -139,7 +139,7 @@ public function __construct(
139139
*
140140
* @return \GraphQL\Deferred
141141
*/
142-
public function resolve($type, $id, $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
142+
public function resolve($type, $id, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
143143
$resolver = $this->entityBuffer->add($type, $id);
144144

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,16 @@ public function __construct(
130130
/**
131131
* @param string $type
132132
* @param string $uuid
133-
* @param array|string $language
134-
* @param array|string $bundles
133+
* @param array|null $language
134+
* @param array|null $bundles
135135
* @param bool|null $access
136136
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
137137
* @param string|null $accessOperation
138138
* @param \Drupal\graphql\GraphQL\Execution\FieldContext $context
139139
*
140140
* @return \GraphQL\Deferred
141141
*/
142-
public function resolve($type, $uuid, $language, $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
142+
public function resolve($type, $uuid, ?array $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
143143
$resolver = $this->entityBuffer->add($type, $uuid);
144144

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __construct(
130130
/**
131131
* @param string $type
132132
* @param array $ids
133-
* @param string $language
133+
* @param string|null $language
134134
* @param array|null $bundles
135135
* @param bool $access
136136
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
@@ -139,7 +139,7 @@ public function __construct(
139139
*
140140
* @return \GraphQL\Deferred
141141
*/
142-
public function resolve($type, array $ids, $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) {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1010
use Drupal\Core\Render\RenderContext;
1111
use Drupal\Core\Render\RendererInterface;
12-
use Drupal\graphql\GraphQL\Execution\ResolveContext;
1312
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
1413
use Symfony\Component\DependencyInjection\ContainerInterface;
1514

@@ -117,11 +116,4 @@ public function resolve(EntityInterface $entity, $mode, RefinableCacheableDepend
117116
return (string) $result;
118117
}
119118

120-
/**
121-
* {@inheritdoc}
122-
*/
123-
protected function shouldLookupEdgeCache(array $values, ResolveContext $context, ResolveInfo $info) {
124-
return TRUE;
125-
}
126-
127119
}

src/Plugin/GraphQL/DataProducer/Field/EntityReference.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\Core\Entity\TranslatableInterface;
1010
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
1111
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12+
use Drupal\Core\Session\AccountInterface;
1213
use Drupal\graphql\GraphQL\Buffers\EntityBuffer;
1314
use Drupal\graphql\GraphQL\Execution\FieldContext;
1415
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
@@ -132,7 +133,7 @@ public function __construct(
132133
/**
133134
* @param \Drupal\Core\Entity\EntityInterface $entity
134135
* @param string $field
135-
* @param string $language
136+
* @param string|null $language
136137
* @param array|null $bundles
137138
* @param bool|null $access
138139
* @param \Drupal\Core\Session\AccountInterface|null $accessUser
@@ -141,7 +142,7 @@ public function __construct(
141142
*
142143
* @return \GraphQL\Deferred|null
143144
*/
144-
public function resolve(EntityInterface $entity, $field, $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
145+
public function resolve(EntityInterface $entity, $field, ?string $language, ?array $bundles, ?bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
145146
if (!$entity instanceof FieldableEntityInterface || !$entity->hasField($field)) {
146147
return NULL;
147148
}

src/Plugin/GraphQL/DataProducer/Routing/RouteLoad.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Path\PathValidatorInterface;
77
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
88
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
9+
use Drupal\redirect\RedirectRepository;
910
use Symfony\Component\DependencyInjection\ContainerInterface;
1011

1112
/**
@@ -79,7 +80,7 @@ public function __construct(
7980
$pluginId,
8081
$pluginDefinition,
8182
PathValidatorInterface $pathValidator,
82-
RedirectRepository $redirectRepository = NULL
83+
?RedirectRepository $redirectRepository = NULL
8384
) {
8485
parent::__construct($configuration, $pluginId, $pluginDefinition);
8586
$this->pathValidator = $pathValidator;

src/Plugin/PersistedQueryPluginManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(
4545

4646
$this->alterInfo('graphql_persisted_query');
4747
$this->useCaches(empty($config['development']));
48-
$this->setCacheBackend($definitionCacheBackend, 'persisted_query', ['graphql']);
48+
$this->setCacheBackend($definitionCacheBackend, 'graphql_persisted_query', ['graphql_persisted_query']);
4949
}
5050

5151
}

0 commit comments

Comments
 (0)