Completed
Push — 8.x-1.x ( 4b08f9...2f413c )
by Philipp
01:33
created

GraphQLTwigBlock   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 13.73 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 7
loc 51
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A blockForm() 0 16 4
A blockSubmit() 4 8 2
A build() 3 12 2

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_twig\Plugin\Block;
4
5
use Drupal\Core\Annotation\Translation;
6
use Drupal\Core\Block\Annotation\Block;
7
use Drupal\Core\Block\BlockBase;
8
use Drupal\Core\Form\FormStateInterface;
9
10
/**
11
 * Class GraphQLTwigBlock
12
 *
13
 * @Block(
14
 *   id="graphql_twig",
15
 *   category=@Translation("GraphQL Twig"),
16
 *   deriver="\Drupal\graphql_twig\Plugin\Deriver\GraphQLTwigBlockDeriver"
17
 * )
18
 */
19
class GraphQLTwigBlock extends BlockBase {
20
21
  /**
22
   * @inheritdoc
23
   */
24
  public function blockForm($form, FormStateInterface $form_state) {
25
    $form = parent::blockForm($form, $form_state);
26
27
    foreach ($this->pluginDefinition['graphql_parameters'] as $name => $el) {
28
      foreach ($el as $key => $value) {
29
        if (in_array($key, ['title', 'description'])) {
30
          $form['graphql_block'][$name]['#' . $key] = $this->t($value);
31
        }
32
        else {
33
          $form['graphql_block'][$name]['#' . $key] = $value;
34
        }
35
      }
36
    }
37
38
    return $form;
39
  }
40
41
  /**
42
   * @inheritdoc
43
   */
44
  public function blockSubmit($form, FormStateInterface $form_state) {
45
    parent::blockSubmit($form, $form_state);
46
47 View Code Duplication
    foreach (array_keys($this->pluginDefinition['graphql_parameters']) as $arg) {
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...
48
      $values = $form_state->getValues();
49
      $this->configuration['graphql_block'][$arg] = $values['graphql_block'][$arg];
50
    }
51
  }
52
53
  /**
54
   * @inheritdoc
55
   */
56
  public function build() {
57
    $arguments = [];
58
59 View Code Duplication
    foreach (array_keys($this->pluginDefinition['graphql_parameters']) as $arg) {
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...
60
      $arguments[$arg] = $this->configuration['graphql_block'][$arg];
61
    }
62
63
    return [
64
      '#theme' => $this->pluginDefinition['graphql_theme_hook'],
65
      '#graphql_arguments' => $arguments,
66
    ];
67
  }
68
69
}
70