Completed
Pull Request — master (#157)
by
unknown
01:35
created

LinkDrupal8   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 19.35 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 12
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D assignPropertyNames() 12 29 10
A processValue() 0 22 3

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
namespace Drupal\Driver\Plugin\DriverField;
3
4
use Drupal\Driver\Plugin\DriverFieldPluginDrupal8Base;
5
6
/**
7
 * A driver field plugin for link fields.
8
 *
9
 * @DriverField(
10
 *   id = "link",
11
 *   version = 8,
12
 *   fieldTypes = {
13
 *     "link",
14
 *   },
15
 *   weight = -100,
16
 * )
17
 */
18
class LinkDrupal8 extends DriverFieldPluginDrupal8Base
19
{
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
    protected function assignPropertyNames($value)
25
    {
26
        // For links we support unkeyed arrays in which the first item is the title,
27
        // the second is the uri and third is options.
28
        $keyedValue = $value;
29
        if (!is_array($value)) {
30
            $keyedValue = ['uri' => $value];
31
        } elseif (count($value) === 1) {
32
            $keyedValue = ['uri' => end($value)];
33
        } // Convert unkeyed array.
34
        else {
35 View Code Duplication
            if (!isset($value['uri']) && isset($value[1])) {
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...
36
                $keyedValue['uri'] = $value[1];
37
                unset($keyedValue[1]);
38
            }
39 View Code Duplication
            if (!isset($value['title']) && isset($value[0])) {
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...
40
                $keyedValue['title'] = $value[0];
41
                unset($keyedValue[0]);
42
            }
43 View Code Duplication
            if (!isset($value['options']) && isset($value[2])) {
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...
44
                $keyedValue['options'] = $value[2];
45
                unset($keyedValue[2]);
46
            }
47
        }
48
        if (!isset($keyedValue['uri'])) {
49
            throw new \Exception("Uri could not be identified from passed value: " . print_r($value, true));
50
        }
51
        return $keyedValue;
52
    }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
    protected function processValue($value)
58
    {
59
        // 'options' is required to be an array, otherwise the utility class
60
        // Drupal\Core\Utility\UnroutedUrlAssembler::assemble() will complain.
61
        $options = [];
62
        if (!empty($value['options'])) {
63
            parse_str($value['options'], $options);
64
        }
65
66
        // Default title to uri.
67
        $title = $value['uri'];
68
        if (isset($value['title'])) {
69
            $title = $value['title'];
70
        }
71
72
        $processedValue = [
73
        'uri' => $value['uri'],
74
        'title' => $title,
75
        'options' => $options,
76
        ];
77
        return $processedValue;
78
    }
79
}
80