Completed
Push — master ( 2760df...c42cfb )
by ARCANEDEV
13s
created

YesOrNo::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanesoft\Core\Entities;
2
3
/**
4
 * Class     YesOrNo
5
 *
6
 * @package  Arcanesoft\Core\Entities
7
 * @author   ARCANEDEV <[email protected]>
8
 */
9
class YesOrNo
10
{
11
    /* -----------------------------------------------------------------
12
     |  Constants
13
     | -----------------------------------------------------------------
14
     */
15
16
    const YES = 'yes';
17
    const NO  = 'no';
18
19
    /* -----------------------------------------------------------------
20
     |  All
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Get all.
26
     *
27
     * @return \Illuminate\Support\Collection
28
     */
29
    public static function all()
30
    {
31 6
        return static::keys()->mapWithKeys(function ($status) {
32 6
            return [$status => trans("core::statuses.{$status}")];
33 6
        });
34
    }
35
36
    /**
37
     * Get all keys.
38
     *
39
     * @return \Illuminate\Support\Collection
40
     */
41 8
    public static function keys()
42
    {
43 8
        return collect([static::YES, static::NO]);
44
    }
45
46
    /**
47
     * Get a translated YoN status.
48
     *
49
     * @param  string|boolean  $key
50
     * @param  null            $default
51
     *
52
     * @return string|null
53
     */
54 4
    public static function get($key, $default = null)
55
    {
56 4
        return static::all()->get(static::parseKey($key), $default);
0 ignored issues
show
Bug introduced by
Since parseKey() 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 parseKey() 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...
57
    }
58
59
    /* -----------------------------------------------------------------
60
     |  Other Methods
61
     | -----------------------------------------------------------------
62
     */
63
64
    /**
65
     * Parse the key.
66
     *
67
     * @param  string|boolean  $key
68
     *
69
     * @return string
70
     */
71 4
    private static function parseKey($key)
72
    {
73 4
        return is_bool($key) ? ($key ? static::YES : static::NO) : $key;
74
    }
75
}
76