Completed
Pull Request — 8.x-3.x (#509)
by Sebastian
05:24 queued 02:06
created

LanguageSwitchLinks::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\LanguageSwitch;
4
5
use Drupal\Core\Language\LanguageInterface;
6
use Drupal\Core\Language\LanguageManagerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\Core\Url;
9
use Drupal\graphql\GraphQL\Buffers\SubRequestBuffer;
10
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Youshido\GraphQL\Execution\ResolveInfo;
13
14
/**
15
 * @GraphQLField(
16
 *   id = "url_language_switch_links",
17
 *   secure = true,
18
 *   name = "languageSwitchLinks",
19
 *   type = "[LanguageSwitchLink]",
20
 *   parents = {"InternalUrl"}
21
 * )
22
 */
23
class LanguageSwitchLinks extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
24
25
  /**
26
   * The language manager.
27
   *
28
   * @var \Drupal\Core\Language\LanguageManagerInterface
29
   */
30
  protected $languageManager;
31
32
  /**
33
   * The subrequest buffer service.
34
   *
35
   * @var \Drupal\graphql\GraphQL\Buffers\SubRequestBuffer
36
   */
37
  protected $subRequestBuffer;
38
39
  /**
40
   * {@inheritdoc}
41
   */
42
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
43
    return new static(
44
      $configuration,
45
      $plugin_id,
46
      $plugin_definition,
47
      $container->get('language_manager'),
48
      $container->get('graphql.buffer.subrequest')
49
    );
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function __construct(
56
    array $configuration,
57
    $pluginId,
58
    $pluginDefinition,
59
    LanguageManagerInterface $languageManager,
60
    SubRequestBuffer $subRequestBuffer
61
  ) {
62
    parent::__construct($configuration, $pluginId, $pluginDefinition);
63
    $this->languageManager = $languageManager;
64
    $this->subRequestBuffer = $subRequestBuffer;
65
  }
66
67
  /**
68
   * {@inheritdoc}
69
   */
70
  protected function resolveValues($value, array $args, ResolveInfo $info) {
71
    if ($value instanceof Url) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Url does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
72
      $resolve = $this->subRequestBuffer->add($value, function (Url $url) {
73
        $links = $this->languageManager->getLanguageSwitchLinks(LanguageInterface::TYPE_URL, $url);
74
        $current = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL);
75
76
        return [$current, $links];
77
      });
78
79
      return function ($value, $args, ResolveInfo $info) use ($resolve) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return function ($value,...t)); } } }; (Closure) is incompatible with the return type of the parent method Drupal\graphql\Plugin\Gr...uginBase::resolveValues of type Generator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $info is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
        list($current, $links) = $resolve();
81
82
        if (!empty($links->links)) {
83
          foreach ($links->links as $link) {
84
            // Yield the link array and the language object of the language
85
            // context resolved from the sub-request.
86
            yield [
87
              'link' => $link,
88
              'context' => $current,
89
            ];
90
          }
91
        }
92
      };
93
    }
94
  }
95
96
}
97