LineGenerator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 73
Duplicated Lines 16.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 12 21 7
A setSeparator() 0 4 1
A clear() 0 4 1
A hasContent() 0 4 1
A generate() 0 4 2
A count() 0 4 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
 * @author sleibelt
4
 * @since 2014-04-25
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
/**
10
 * Class LineGenerator
11
 *
12
 * @package Net\Bazzline\Component\Locator\LocatorGenerator
13
 */
14
class LineGenerator extends AbstractContentGenerator
15
{
16
    /** @var array|string[] */
17
    private $content = [];
18
19
    /** @var string */
20
    private $separator = ' ';
21
22
    /**
23
     * @param string|array|GeneratorInterface[]|AbstractContentGenerator[] $content
24
     * @return $this
25
     * @throws InvalidArgumentException
26
     */
27
    public function add($content)
28
    {
29
        if (is_string($content)) {
30
            $this->content[] = $content;
31
        } else if ($content instanceof LineGenerator) {
32
            $this->content[] = $content->generate();
33 View Code Duplication
        } else if (is_array($content)) {
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...
34
            foreach ($content as $part) {
35
                $this->add($part);
36
            }
37
        } else if ($content instanceof AbstractContentGenerator) {
38
            $content->setIndention($this->getIndention());
39
            if ($content->hasContent()) {
40
                $this->content[] = $content->generate();
41
            }
42
        } else {
43
            throw new InvalidArgumentException('content has to be string, an array or instance of AbstractContentGenerator');
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param $separator
51
     */
52
    public function setSeparator($separator)
53
    {
54
        $this->separator = (string) $separator;
55
    }
56
57
    public function clear()
58
    {
59
        $this->content = [];
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function hasContent()
66
    {
67
        return (!empty($this->content));
68
    }
69
70
    /**
71
     * @throws InvalidArgumentException|RuntimeException
72
     * @return string
73
     */
74
    public function generate()
75
    {
76
        return (implode('', $this->content) !== '') ? $this->getIndention()->toString() . implode($this->separator, $this->content) : '';
77
    }
78
79
    /**
80
     * @return int
81
     */
82
    public function count()
83
    {
84
        return (count($this->content));
85
    }
86
}
87