Completed
Push — master ( 0a3c25...4ed9d0 )
by ARCANEDEV
10s
created

Attributes::makeAttributeElement()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 8
nc 5
nop 2
crap 6
1
<?php namespace Arcanedev\LaravelHtml\Helpers;
2
3
/**
4
 * Class     Attributes
5
 *
6
 * @package  Arcanedev\LaravelHtml\Helpers
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class Attributes
10
{
11
    /* -----------------------------------------------------------------
12
     |  Main Methods
13
     | -----------------------------------------------------------------
14
     */
15
16
    /**
17
     * Build an HTML attribute string from an array.
18
     *
19
     * @param  array  $attributes
20
     *
21
     * @return string
22
     */
23 384
    public static function make(array $attributes)
24
    {
25 384
        $html = [];
26
27 384
        foreach ((array) $attributes as $key => $value) {
28 348
            $element = static::makeAttributeElement($key, $value);
0 ignored issues
show
Bug introduced by
Since makeAttributeElement() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of makeAttributeElement() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
29
30 348
            if ( ! is_null($element))
31 346
                $html[] = $element;
32
        }
33
34 384
        return (count($html) > 0) ? ' '.implode(' ', array_filter($html)) : '';
35
    }
36
37
    /* -----------------------------------------------------------------
38
     |  Other Methods
39
     | -----------------------------------------------------------------
40
     */
41
42
    /**
43
     * Make attribute element.
44
     *
45
     * @param  mixed  $key
46
     * @param  mixed  $value
47
     *
48
     * @return null|string
49
     */
50 348
    private static function makeAttributeElement($key, $value)
51
    {
52 348
        if (is_null($value))
53 261
            return null;
54
55
        // For numeric keys we will assume that the key and the value are the same
56
        // as this will convert HTML attributes such as "required" to a correct
57
        // form like required="required" instead of using incorrect numerics.
58 342
        if (is_numeric($key))
59 9
            return $value;
60
61 342
        if (is_bool($value) && $key !== 'value')
62 3
            return $value ? $key : '';
63
64 342
        return $key.'="'.e($value).'"';
65
    }
66
}
67