Passed
Push — master ( 7a576b...99159c )
by zyt
04:01
created

GoogleFont   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 108
Duplicated Lines 16.67 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 18
loc 108
ccs 45
cts 55
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 17

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getSizesString() 0 3 1
A fromString() 0 14 3
A getSubsets() 0 3 1
A setSubsets() 7 7 3
A getSizes() 0 3 1
A __toString() 0 8 1
A setName() 0 3 1
A setSizes() 7 7 3
A getName() 0 3 1
A getSubsetsString() 0 3 1
A __construct() 0 5 1

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 ZWF;
4
5
class GoogleFont
6
{
7
8
    protected $name = '';
9
10
    protected $sizes = [];
11
12
    protected $subsets = [];
13
14 5
    public function __construct($name, array $sizes = [], array $subsets = [])
15 5
    {
16 5
        $this->setName($name);
17 5
        $this->setSizes($sizes);
18 5
        $this->setSubsets($subsets);
19 5
    }
20
21 5
    public function setName($name)
22 5
    {
23 5
        $this->name = $name;
24 5
    }
25
26 5
    public function getName()
27 5
    {
28 5
        return $this->name;
29
    }
30
31
    /**
32
     * @param string|array $sizes
33
     */
34 5 View Code Duplication
    public function setSizes($sizes)
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...
35 5
    {
36 5
        if (is_array($sizes)) {
37 5
            $this->sizes = $sizes;
38
        }
39 5
        if (is_string($sizes)) {
40 4
            $this->sizes = array_map('trim', explode(',', $sizes));
41
        }
42 5
    }
43
44 5
    public function getSizes()
45 5
    {
46 5
        return $this->sizes;
47
    }
48
49
    public function getSizesString()
50
    {
51
        return implode(',', $this->getSizes());
52
    }
53
54
    /**
55
     * @param string|array $subsets
56
     */
57 5 View Code Duplication
    public function setSubsets($subsets)
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...
58 5
    {
59 5
        if (is_array($subsets)) {
60 5
            $this->subsets = $subsets;
61
        }
62 5
        if (is_string($subsets)) {
63 4
            $this->subsets = array_map('trim', explode(',', $subsets));
64
        }
65 5
    }
66
67 3
    public function getSubsets()
68 3
    {
69 3
        return $this->subsets;
70
    }
71
72 5
    public function getSubsetsString()
73 5
    {
74 5
        return implode(',', $this->subsets);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function __toString()
81
    {
82
        return implode(
83
            ':',
84
            [
85
                $this->getName(),
86
                $this->getSizesString(),
87
                $this->getSubsetsString()
88
            ]
89
        );
90
    }
91
92
    /**
93
     * Creates a new instance from a given $fontstring.
94
     *
95
     * @param string $fontstring
96
     *
97
     * @return GoogleFont
98
     */
99 5
    public static function fromString($fontstring)
100 5
    {
101 5
        $parts = explode(':', $fontstring);
102 5
        $font  = new self($parts[0]);
103
104 5
        if (isset($parts[1])) {
105 4
            $font->setSizes($parts[1]);
106
        }
107
108 5
        if (isset($parts[2])) {
109 4
            $font->setSubsets($parts[2]);
110
        }
111
112 5
        return $font;
113
    }
114
}
115