Completed
Pull Request — 8.x-3.x (#442)
by Sebastian
02:26
created

BatchedField::getBatchedFieldResolver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Field;
4
5
use Drupal\graphql\GraphQL\Batching\BatchedFieldInterface;
6
use Youshido\GraphQL\Execution\ResolveInfo;
7
8
class BatchedField extends Field implements BatchedFieldInterface {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function getBatchedFieldResolver() {
14
    if ($this->plugin instanceof BatchedFieldInterface) {
15
      return $this->plugin->getBatchedFieldResolver();
16
    }
17
18
    return NULL;
19
  }
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function getBatchId($parent, array $arguments, ResolveInfo $info) {
25
    if ($this->plugin instanceof BatchedFieldInterface) {
26
      return $this->plugin->getBatchId($parent, $arguments, $info);
0 ignored issues
show
Bug introduced by
The method getBatchId does only exist in Drupal\graphql\Plugin\Gr...\Fields\FieldPluginBase, but not in Drupal\graphql\Plugin\Gr...ions\MutationPluginBase.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
27
    }
28
29
    return NULL;
30
  }
31
32
  /**
33
   * {@inheritdoc}
34
   */
35
  public function resolveBatch(array $batch) {
36
    if ($this->plugin instanceof BatchedFieldInterface) {
37
      return $this->plugin->resolveBatch($batch);
38
    }
39
40
    return NULL;
41
  }
42
}
43