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

Field::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql\GraphQL\Field;
4
5
use Drupal\graphql\GraphQL\SecureFieldInterface;
6
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
7
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceInterface;
8
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginReferenceTrait;
9
use Youshido\GraphQL\Config\Field\FieldConfig;
10
use Youshido\GraphQL\Execution\ResolveInfo;
11
use Youshido\GraphQL\Field\AbstractField;
12
13
class Field extends AbstractField implements SecureFieldInterface, TypeSystemPluginReferenceInterface {
14
  use TypeSystemPluginReferenceTrait;
15
16
  /**
17
   * The associated type system plugin.
18
   *
19
   * @var \Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase|\Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase
20
   */
21
  protected $plugin;
22
23
  /**
24
   * Indicates if the field is considered secure.
25
   *
26
   * @var bool
27
   */
28
  protected $secure;
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function __construct(FieldPluginBase $plugin, $secure, array $config = []) {
34
    $this->plugin = $plugin;
35
    $this->secure = $secure;
36
    $this->config = new FieldConfig($config, $this, TRUE);
37
  }
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public function resolve($value, array $args, ResolveInfo $info) {
43
    return $this->plugin->resolve($value, $args, $info);
0 ignored issues
show
Bug introduced by
The method resolve 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...
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function getType() {
50
    return $this->getConfigValue('type');
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function getName() {
57
    return $this->getConfigValue('name');
58
  }
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function isSecure() {
64
    return $this->secure;
65
  }
66
}
67