Completed
Push — master ( a0e7e0...d1cff9 )
by Costin
01:48 queued 12s
created

GluesAttributes   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B glueAttributes() 0 34 11
1
<?php
2
3
namespace SoareCostin\BladeFormComponents\Traits;
4
5
trait GluesAttributes
6
{
7
   public function glueAttributes($attributesList = null)
8
    {
9
        if (is_null($attributesList) && isset($this->attributesList)) {
10
            $attributesList = $this->attributesList;
0 ignored issues
show
Bug introduced by
The property attributesList 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...
11
        }
12
13
        $pairs = [];
14
        foreach ($attributesList as $attr) {
15
            if (isset($this->{$attr})) {
16
17
                // Edge cases
18
                if ($attr == 'autocomplete') {
19
                    $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr} ? 'on' : 'off');
20
                    continue;
21
                }
22
23
                if ($attr == 'class' && is_array($this->{$attr})) {
24
                    $pairs[] = sprintf('%s="%s"', $attr, implode(' ', $this->{$attr}));
25
                    continue;
26
                }
27
28
                // Required, disabled, readonly: true/false
29
                if (is_bool($this->{$attr})) {
30
                    if ($this->{$attr} == true) {
31
                        $pairs[] = $attr;
32
                    }
33
                    continue;
34
                }
35
                
36
                $pairs[] = sprintf('%s="%s"', $attr, $this->{$attr});
37
            }
38
        }
39
        return implode(" ", $pairs);
40
    }
41
}