Completed
Push — Feature_LoremIpsum ( 9359ab )
by
unknown
01:58
created

Helper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 13.7 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 10
loc 73
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getApiBase() 0 4 1
B getMappedOptions() 0 40 1
C getLoremIpsum() 10 24 7

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 Flynt\Features\LoremIpsum;
4
5
class Helper
6
{
7
    public static function getApiBase()
8
    {
9
        return 'http://loripsum.net/api';
10
    }
11
12
    public static function getMappedOptions()
13
    {
14
        return [
15
            'dynamic' => [
16
                'plength',
17
                'length'
18
            ],
19
            'boolean' => [
20
                'decorate',
21
                'link',
22
                'ul',
23
                'ol',
24
                'dl',
25
                'bq',
26
                'code',
27
                'headers',
28
                'allcaps',
29
                'prude',
30
                'plaintext'
31
            ],
32
            'shortcode' => [
33
              'dynamic' => [
34
                'paragraphs' => 'plength',
35
                'length' => 'length'
36
              ],
37
              'boolean' => [
38
                'decorate' => 'decorate',
39
                'ul' => 'ul',
40
                'ol' => 'ol',
41
                'dl' => 'dl',
42
                'quote' => 'bq',
43
                'code' => 'code',
44
                'headers' => 'headers',
45
                'caps' => 'allcaps',
46
                'prude' => 'prude',
47
                'plain' => 'plaintext'
48
              ]
49
            ]
50
        ];
51
    }
52
    
53
    public static function getLoremIpsum($options = [])
54
    {
55
        $url = self::getApiBase();
56
        $mappedOptions = self::getMappedOptions();
57
58 View Code Duplication
        foreach ($mappedOptions['dynamic'] as $option) {
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...
59
            if (isset($options[$option])) {
60
                $url .= '/' . $options[$option];
61
            }
62
        }
63
64 View Code Duplication
        foreach ($mappedOptions['boolean'] as $option) {
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...
65
            if (isset($options[$option]) && $options[$option] === true) {
66
                $url .= '/' . $option;
67
            }
68
        }
69
70
        try {
71
            $response = wp_remote_get($url);
72
            return $response['body'];
73
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Flynt\Features\LoremIpsum\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
74
            return false;
75
        }
76
    }
77
}
78
79
add_shortcode('lorem-ipsum', function ($attr) {
80
    $options = [];
81
    $mappedOptions = Helper::getMappedOptions();
82
83
    foreach ($mappedOptions['shortcode'] as $type => $typeOptions) {
84
        foreach ($typeOptions as $attrName => $option) {
85 View Code Duplication
            if ($type === 'dynamic') {
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...
86
                if (isset($attr[$attrName])) {
87
                    $options[$option] = $attr[$attrName];
88
                }
89
            }
90
91
            if ($type === 'boolean') {
92 View Code Duplication
                if (isset($attr[$attrName]) && $attr[$attrName] === '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...
93
                    $options[$option] = true;
94
                }
95
            }
96
        }
97
    }
98
99
    $content = Helper::getLoremIpsum($options);
100
    if ($content) {
101
        return $content;
102
    }
103
});
104