Test Setup Failed
Push — master ( f52cce...34890a )
by Royall
02:32
created

AttributeSet   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 52
loc 52
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 2
A iterate() 6 6 2
A add() 5 5 1
A merge() 8 8 2
A render() 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
namespace RoyallTheFourth\HtmlDocument\Set;
4
5
use RoyallTheFourth\HtmlDocument\Attribute\AttributeInterface;
6
7 View Code Duplication
final class AttributeSet implements SetInterface
0 ignored issues
show
Duplication introduced by
This class 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...
8
{
9
    private $attributes = [];
10
11
    public function __construct(AttributeInterface $attribute = null)
12
    {
13
        if ($attribute) {
14
            $this->attributes[] = $attribute;
15
        }
16
    }
17
18
    public function iterate(): \Generator
19
    {
20
        foreach ($this->attributes as $attribute) {
21
            yield $attribute;
22
        }
23
    }
24
25
    /**
26
     * @param AttributeInterface $attribute
27
     * @return AttributeSet
28
     */
29
    public function add($attribute): AttributeSet
30
    {
31
        $this->attributes[] = $attribute;
32
        return $this;
33
    }
34
35
    /**
36
     * @param $set AttributeSet
37
     * @return AttributeSet
38
     */
39
    public function merge($set): AttributeSet
40
    {
41
        foreach ($set->iterate() as $attribute) {
42
            $this->add($attribute);
43
        }
44
45
        return $this;
46
    }
47
48
    public function render(): string
49
    {
50
        $output = '';
51
52
        foreach ($this->iterate() as $attribute) {
53
            $output .= ' ' . $attribute->render();
54
        }
55
56
        return $output;
57
    }
58
}
59