Completed
Push — master ( b0b946...19b9c5 )
by Costin
02:32
created

GluesAttributes::glueAttributes()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
eloc 18
c 6
b 2
f 1
dl 0
loc 35
rs 9.0444
cc 6
nc 4
nop 1
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)) {
10
            $attributesList = $this->attributesList();
11
        }
12
13
        $pairs = collect($attributesList)->filter(function ($attr, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

13
        $pairs = collect($attributesList)->filter(function ($attr, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
            return isset($this->{$attr});
15
        })
16
        ->filter(function ($attr, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

16
        ->filter(function ($attr, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
            return ! is_bool($this->{$attr}) || $this->{$attr} == true;
18
        })
19
        ->map(function ($attr, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

19
        ->map(function ($attr, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
            if (is_array($this->{$attr})) {
21
                return sprintf('%s="%s"', $attr, implode(' ', $this->{$attr}));
22
            }
23
24
            if (is_bool($this->{$attr})) {
25
                return $attr;
26
            }
27
28
            return sprintf('%s="%s"', $attr, $this->{$attr});
29
        });
30
        
31
        $customAttributes = $this->customAttributes();
32
33
        if (! empty($customAttributes)) {
34
            $pairs = $pairs->merge(
35
                collect($customAttributes)->map(function ($attr, $key) {
36
                    return sprintf('%s="%s"', $key, $attr);
37
                })
38
            );
39
        }
40
41
        return $pairs->implode(' ');
42
    }
43
44
    protected function attributesList()
45
    {
46
        return [];
47
    }
48
49
    protected function customAttributes()
50
    {
51
        return [];
52
    }
53
}
54