Completed
Pull Request — 8.x-3.x (#497)
by Sebastian
04:55
created

Field::isSecure()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
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\CacheableEdgeInterface;
6
use Drupal\graphql\GraphQL\CacheableEdgeTrait;
7
use Drupal\graphql\GraphQL\PluginReferenceInterface;
8
use Drupal\graphql\GraphQL\PluginReferenceTrait;
9
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
10
use Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase;
11
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
12
use Drupal\graphql\Plugin\GraphQL\TypeSystemPluginInterface;
13
use Youshido\GraphQL\Config\Field\FieldConfig;
14
use Youshido\GraphQL\Execution\ResolveInfo;
15
use Youshido\GraphQL\Field\AbstractField;
16
17
class Field extends AbstractField implements PluginReferenceInterface, CacheableEdgeInterface {
18
  use PluginReferenceTrait;
19
  use CacheableEdgeTrait;
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
  public function __construct(TypeSystemPluginInterface $plugin, PluggableSchemaBuilderInterface $builder, array $config = []) {
25
    $this->plugin = $plugin;
26
    $this->builder = $builder;
27
    $this->config = new FieldConfig($config, $this, TRUE);
28
  }
29
30
  /**
31
   * {@inheritdoc}
32
   */
33
  public function resolve($value, array $args, ResolveInfo $info) {
34
    if (($plugin = $this->getPlugin()) && ($plugin instanceof FieldPluginBase || $plugin instanceof MutationPluginBase)) {
35
      return $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...
36
    }
37
38
    return NULL;
39
  }
40
41
  /**
42
   * {@inheritdoc}
43
   */
44
  public function getType() {
45
    return $this->getConfigValue('type');
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function getName() {
52
    return $this->getConfigValue('name');
53
  }
54
}
55