Completed
Push — master ( 759093...8330b4 )
by Revin
01:29
created

Stack   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 17.09 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 4
dl 20
loc 117
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __toString() 0 24 4
A icon() 10 10 2
A text() 0 10 1
A on() 10 10 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
 * Stack.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\fontawesome\component;
9
10
use rmrevin\yii\fontawesome\FontAwesome;
11
use yii\helpers\ArrayHelper;
12
use yii\helpers\Html;
13
14
/**
15
 * Class Stack
16
 * @package rmrevin\yii\fontawesome\component
17
 */
18
class Stack
19
{
20
    /**
21
     * @var string
22
     */
23
    private $iconCssPrefix = 'fa';
24
25
    /**
26
     * @var array
27
     */
28
    private $options = [];
29
30
    /**
31
     * @var Icon
32
     */
33
    private $icon_front;
34
35
    /**
36
     * @var string
37
     */
38
    private $text_front = null;
39
40
    /**
41
     * @var Icon
42
     */
43
    private $icon_back;
44
45
    /**
46
     * @param string $iconCssPrefix
47
     * @param array $options
48
     */
49
    public function __construct($iconCssPrefix, $options = [])
50
    {
51
        $this->iconCssPrefix = $iconCssPrefix;
52
53
        Html::addCssClass($options, FontAwesome::$basePrefix . '-stack');
54
55
        $this->options = $options;
56
    }
57
58
    /**
59
     * @return string
60
     * @throws \yii\base\InvalidConfigException
61
     */
62
    public function __toString()
63
    {
64
        $options = $this->options;
65
66
        $tag = ArrayHelper::remove($options, 'tag', 'span');
67
68
        $template = ArrayHelper::remove($options, 'template', '{back}{front}');
69
70
        $iconBack = $this->icon_back instanceof Icon
71
            ? $this->icon_back->addCssClass(FontAwesome::$basePrefix . '-stack-2x')
72
            : null;
73
74
        if ($this->text_front !== null) {
75
            $contentFront = $this->text_front;
76
        } else {
77
            $contentFront = $this->icon_front instanceof Icon
78
                ? $this->icon_front->addCssClass(FontAwesome::$basePrefix . '-stack-1x')
79
                : null;
80
        }
81
82
        $content = str_replace(['{back}', '{front}'], [$iconBack, $contentFront], $template);
83
84
        return Html::tag($tag, $content, $options);
85
    }
86
87
    /**
88
     * @param string|Icon $icon
89
     * @param array $options
90
     * @return \rmrevin\yii\fontawesome\component\Stack
91
     */
92 View Code Duplication
    public function icon($icon, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
94
        if (is_string($icon)) {
95
            $icon = new Icon($this->iconCssPrefix, $icon, $options);
96
        }
97
98
        $this->icon_front = $icon;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param string $text
105
     * @param array $options
106
     * @return \rmrevin\yii\fontawesome\component\Stack
107
     */
108
    public function text($text = '', $options = [])
109
    {
110
        $tag = ArrayHelper::remove($options, 'tag', 'span');
111
112
        Html::addCssClass($options, FontAwesome::$basePrefix . '-stack-1x');
113
114
        $this->text_front = Html::tag($tag, $text, $options);
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param string|Icon $icon
121
     * @param array $options
122
     * @return \rmrevin\yii\fontawesome\component\Stack
123
     */
124 View Code Duplication
    public function on($icon, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
125
    {
126
        if (is_string($icon)) {
127
            $icon = new Icon($this->iconCssPrefix, $icon, $options);
128
        }
129
130
        $this->icon_back = $icon;
131
132
        return $this;
133
    }
134
}
135