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

Common::classes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder\Traits;
4
5
6
use Illuminate\Support\Collection;
7
8
trait Common
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $id;
14
15
    /**
16
     * @var string
17
     */
18
    protected $name;
19
20
    /**
21
     * @var Collection
22
     */
23
    protected $classes;
24
25
    /**
26
     * @var Collection
27
     */
28
    protected $attributes;
29
30
    /**
31
     * @var Collection
32
     */
33
    protected $styles;
34
35
    public function id($value): self
36
    {
37
        $this->id = $value;
38
39
        return $this;
40
    }
41
42
    public function name($value): self
43
    {
44
        $this->name = $value;
45
46
        return $this;
47
    }
48
49 View Code Duplication
    public function class(...$classes): self
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...
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
50
    {
51
        foreach($classes as $class) {
52
            if(!$this->classes->contains($class)) {
53
                $this->classes->push($class);
54
            }
55
        }
56
57
        return $this;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
58
    }
59
60 View Code Duplication
    public function classes(iterable $classes): self
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...
61
    {
62
        foreach($classes as $class) {
63
            if(!$this->classes->contains($class)) {
64
                $this->classes->push($class);
65
            }
66
        }
67
68
        return $this;
69
    }
70
71
    public function style(string $key, string $value = null): self
72
    {
73
        if (empty($value)) {
74
            $pos = strpos($key, ':', 2);
75
            if ($pos !== false) {
76
                $parts = explode(':', strrev($key));
77
                $key = strrev($parts[1]);
78
                $value = strrev($parts[0]);
79
            }
80
        }
81
82
        if ($key && $value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
83
            $this->styles->put($key, $value);
84
        }
85
86
        return $this;
87
    }
88
89
    public function styles(iterable $styles): self
90
    {
91
        foreach($styles as $key => $value) {
92
            $this->styles->put($key, $value);
93
        }
94
95
        return $this;
96
    }
97
98
    public function attr(string $key, string $value = null): self
99
    {
100
        $this->attributes->put($key, $value);
101
102
        return $this;
103
    }
104
105
    public function attrs(iterable $attributes): self
106
    {
107
        foreach($attributes as $key => $value) {
108
            if ($key === 'class') {
109
                $this->class($value);
110
            } else if($key === 'style') {
111
                $this->style($value);
112
            } else {
113
                $this->attributes->put($key, $value);
114
            }
115
        }
116
117
        return $this;
118
    }
119
}