Component   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 80.65%

Importance

Changes 0
Metric Value
wmc 10
lcom 3
cbo 2
dl 0
loc 108
ccs 25
cts 31
cp 0.8065
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getContent() 0 4 1
A getAttributes() 0 4 1
A appendToAttribute() 0 13 2
A setIndentation() 0 4 1
A setTranslator() 0 4 1
A __toString() 0 14 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Foo\Grid\Component;
6
7
use Foo\Grid\Helper\StringHelper;
8
use Foo\Translate\ITranslator;
9
10
class Component implements IComponent
11
{
12
    const ATTRIBUTE_CLASS = 'class';
13
14
    /** @var string|IComponent */
15
    protected $content;
16
17
    /** @var string|null */
18
    protected $tag;
19
20
    /** @var array */
21
    protected $attributes = [];
22
23
    /** @var string */
24
    protected $indentation = '';
25
26
    /** @var ITranslator */
27
    protected $translator;
28
29
    /**
30
     * Component constructor.
31
     *
32
     * @param string           $content
33
     * @param string|null      $tag
34
     * @param array            $attributes
35
     * @param ITranslator|null $translator
36
     */
37 125
    public function __construct(
38
        $content = '',
39
        string $tag = null,
40
        array $attributes = [],
41
        ITranslator $translator = null
42
    ) {
43 125
        $this->content    = $content;
44 125
        $this->tag        = $tag;
45 125
        $this->attributes = $attributes;
46 125
        $this->translator = $translator;
47 125
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getContent(): string
53
    {
54
        return (string)$this->content;
55
    }
56
57
    /**
58
     * @return string
59
     */
60 16
    public function getAttributes(): array
61
    {
62 16
        return $this->attributes;
63
    }
64
65
    /**
66
     * @param string $attribute
67
     * @param string $valueToAppend
68
     */
69 44
    public function appendToAttribute(string $attribute, string $valueToAppend)
70
    {
71 44
        $currentValue = isset($this->attributes[$attribute]) ? $this->attributes[$attribute] : '';
72
73 44
        $classes = explode(' ', $currentValue);
74
75 44
        $classes[] = $valueToAppend;
76
77 44
        $classes = array_unique($classes);
78 44
        $classes = array_filter($classes);
79
80 44
        $this->attributes[$attribute] = implode(' ', $classes);
81 44
    }
82
83
    /**
84
     * @param int    $num
85
     * @param string $whitespace
86
     */
87
    public function setIndentation(int $num, string $whitespace = '    ')
88
    {
89
        $this->intendation = str_repeat($whitespace, $num);
0 ignored issues
show
Bug introduced by
The property intendation does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
90
    }
91
92
    /**
93
     * @param ITranslator $translator
94
     */
95 4
    public function setTranslator(ITranslator $translator)
96
    {
97 4
        $this->translator = $translator;
98 4
    }
99
100
    /**
101
     * @return string
102
     */
103 18
    public function __toString(): string
104
    {
105 18
        $content = $this->content;
106
107 18
        if ($this->translator) {
108 4
            $content = $this->translator->translate($content);
109
110 4
            if (substr($content, 0, 2) === '{{') {
111
                $content = $this->content;
112
            }
113
        }
114
115 18
        return StringHelper::wrapInTag((string)$content, $this->tag, $this->attributes);
116
    }
117
}
118
119