Completed
Push — master ( 4f68bc...d7053e )
by Artem
02:00
created

StaticDictionary::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2015 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace Dictionary;
13
14
/**
15
 * Abstract static dictionary of key/value pairs.
16
 */
17
abstract class StaticDictionary implements StaticDictionaryInterface
18
{
19
    /** @var array Shared cache of all dictionaries. */
20
    private static $cache = [];
21
22
    /**
23
     * Returns the dictionary as associated array.
24
     *
25
     * @return  array
26
     */
27 5
    private static function getFromCache()
28
    {
29 5
        $dictionary = get_called_class();
30
31 5
        if (!array_key_exists($dictionary, self::$cache)) {
32 2
            self::$cache[$dictionary] = static::all();
33 2
        }
34
35 5
        return self::$cache[$dictionary];
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public static function get($key)
42
    {
43 1
        $dictionary = static::getFromCache();
0 ignored issues
show
Bug introduced by
Since getFromCache() 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 getFromCache() 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...
44
45 1
        if (array_key_exists($key, $dictionary)) {
46 1
            return $dictionary[$key];
47
        }
48
        else {
49 1
            return static::FALLBACK === null ? null : $dictionary[static::FALLBACK];
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public static function has($key)
57
    {
58 1
        $dictionary = static::getFromCache();
0 ignored issues
show
Bug introduced by
Since getFromCache() 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 getFromCache() 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...
59
60 1
        return array_key_exists($key, $dictionary);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public static function find($value)
67
    {
68 1
        $dictionary = static::getFromCache();
0 ignored issues
show
Bug introduced by
Since getFromCache() 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 getFromCache() 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...
69
70 1
        $key = array_search($value, $dictionary);
71
72 1
        return $key === false ? static::FALLBACK : $key;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public static function keys()
79
    {
80 1
        $dictionary = static::getFromCache();
0 ignored issues
show
Bug introduced by
Since getFromCache() 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 getFromCache() 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...
81
82 1
        return array_keys($dictionary);
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public static function values()
89
    {
90 1
        $dictionary = static::getFromCache();
0 ignored issues
show
Bug introduced by
Since getFromCache() 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 getFromCache() 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...
91
92 1
        return array_values($dictionary);
93
    }
94
}
95