Completed
Push — master ( b78aa7...24e372 )
by Denis
06:09
created

AbstractElement::__toString()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 22
nc 48
nop 0
dl 0
loc 39
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder\Elements;
4
5
6
use Illuminate\Support\Collection;
7
use Ngtfkx\Laradeck\FormBuilder\Traits;
8
9
abstract class AbstractElement
10
{
11
    use Traits\Common;
12
13
    /**
14
     * @var string
15
     */
16
    protected $value;
17
18
    /**
19
     * @var string
20
     */
21
    protected $tag;
22
23
    abstract function tag();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
25
    protected $parts = [];
26
27
    public function __construct()
28
    {
29
        $this->classes = new Collection();
30
31
        $this->attributes = new Collection();
32
33
        $this->styles = new Collection();
34
35
        $this->parts = new Collection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Illuminate\Support\Collection() of type object<Illuminate\Support\Collection> is incompatible with the declared type array of property $parts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
37
        $this->tag();
38
    }
39
40
    public function value($value): self
41
    {
42
        $this->value = $value;
43
44
        return $this;
45
    }
46
47
    public function __toString()
48
    {
49
        $this->addAttr('id', 'name');
50
51
        $attributes = '';
52
53
        if ($this->classes->isNotEmpty()) {
54
            $this->parts->put('class', $this->classes->implode(' '));
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->parts (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
55
        }
56
57
        if ($this->styles->isNotEmpty()) {
58
            $this->parts->put('style', $this->styles->pipe(function ($styles) {
0 ignored issues
show
Bug introduced by
The method put cannot be called on $this->parts (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
59
                $styleAttr = '';
60
                foreach ($styles as $key => $value) {
61
                    $styleAttr .= $key . ':' . $value . ';';
62
                }
63
64
                return $styleAttr;
65
            }));
66
        }
67
68
        foreach ($this->parts as $key => $value) {
69
            if (is_bool($value) && $value === false) {
70
                continue;
71
            }
72
            $attributes .= ' ' . $key . '="' . $value . '"';
73
        }
74
75
        foreach ($this->attributes as $key => $value) {
76
            if (is_bool($value) && $value === false) {
77
                continue;
78
            } else if (is_bool($value)) {
79
                $value = $key;
80
            }
81
            $attributes .= ' ' . $key . '="' . $value . '"';
82
        }
83
84
        return '<' . $this->tag . $attributes . '>';
85
    }
86
87
    protected function addAttr(...$names): self
88
    {
89
        foreach ($names as $name) {
90
            if (!empty($this->$name)) {
91
                $this->parts[$name] = $this->$name;
92
            }
93
        }
94
95
        return $this;
96
    }
97
}