Completed
Pull Request — 8.x-3.x (#399)
by Philipp
02:23
created

MenuLinks   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 5
loc 46
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 4 1
A resolveValues() 5 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Menu;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Menu\MenuLinkInterface;
7
use Drupal\Core\Menu\MenuLinkTreeElement;
8
use Drupal\Core\Menu\MenuLinkTreeInterface;
9
use Drupal\Core\Menu\MenuTreeParameters;
10
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
12
use Drupal\system\MenuInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Youshido\GraphQL\Execution\ResolveInfo;
15
16
/**
17
 * Retrieves a menus links.
18
 *
19
 * @GraphQLField(
20
 *   id = "menu_links",
21
 *   secure = true,
22
 *   name = "links",
23
 *   type = "MenuLink",
24
 *   parents = {"Menu"},
25
 *   multi = true
26
 * )
27
 */
28
class MenuLinks extends FieldPluginBase implements ContainerFactoryPluginInterface {
29
  use DependencySerializationTrait;
30
31
  /**
32
   * The menu link tree.
33
   *
34
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
35
   */
36
  protected $menuLinkTree;
37
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
42
    return new static($configuration, $pluginId, $pluginDefinition, $container->get('menu.link_tree'));
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function __construct(array $configuration, $pluginId, $pluginDefinition, MenuLinkTreeInterface $menuLinkTree) {
49
    $this->menuLinkTree = $menuLinkTree;
50
    parent::__construct($configuration, $pluginId, $pluginDefinition);
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public function resolveValues($value, array $args, ResolveInfo $info) {
57
    if ($value instanceof MenuInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\system\MenuInterface 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...
58
      $tree = $this->menuLinkTree->load($value->id(), new MenuTreeParameters());
59
60
      $manipulators = [
61
        ['callable' => 'menu.default_tree_manipulators:checkAccess'],
62
        ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
63
      ];
64
65 View Code Duplication
      foreach (array_filter($this->menuLinkTree->transform($tree, $manipulators), function (MenuLinkTreeElement $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
        return $item->link instanceof MenuLinkInterface && $item->link->isEnabled();
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Menu\MenuLinkInterface 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...
67
      }) as $branch) {
68
        yield $branch;
69
      }
70
    }
71
  }
72
73
}
74