Completed
Push — master ( e1a379...98a721 )
by ARCANEDEV
07:12
created

Label::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelHtml\Helpers;
2
3
/**
4
 * Class     Label
5
 *
6
 * @package  Arcanedev\LaravelHtml\Helpers
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class Label
10
{
11
    /* ------------------------------------------------------------------------------------------------
12
     |  Main Functions
13
     | ------------------------------------------------------------------------------------------------
14
     */
15
    /**
16
     * Create a form label element.
17
     *
18
     * @param  string  $name
19
     * @param  string  $value
20
     * @param  array   $attributes
21
     * @param  bool    $escaped
22
     *
23
     * @return string
24
     */
25 40
    public static function make($name, $value = null, array $attributes = [], $escaped = true)
26
    {
27 40
        $value = static::format($name, $value);
0 ignored issues
show
Bug introduced by
Since format() 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 format() 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...
28
29 40
        return implode('', [
30 40
            '<label for="' . $name . '"' . Attributes::make($attributes) . '>',
31 40
                $escaped ? e($value) : $value,
32 10
            '</label>'
33 30
        ]);
34
    }
35
36
    /* ------------------------------------------------------------------------------------------------
37
     |  Other Functions
38
     | ------------------------------------------------------------------------------------------------
39
     */
40
    /**
41
     * Format the label value.
42
     *
43
     * @param  string       $name
44
     * @param  string|null  $value
45
     *
46
     * @return string
47
     */
48 40
    private static function format($name, $value)
49
    {
50 40
        return $value ?: ucwords(str_replace('_', ' ', $name));
51
    }
52
}
53