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

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