ElggStaticVariableCache::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * \ElggStaticVariableCache
4
 * Dummy cache which stores values in a static array. Using this makes future
5
 * replacements to other caching back ends (eg memcache) much easier.
6
 *
7
 * @package    Elgg.Core
8
 * @subpackage Cache
9
 */
10
class ElggStaticVariableCache extends \ElggSharedMemoryCache {
11
	/**
12
	 * The cache.
13
	 *
14
	 * @var array
15
	 */
16
	private static $__cache;
17
18
	/**
19
	 * Create the variable cache.
20
	 *
21
	 * This function creates a variable cache in a static variable in
22
	 * memory, optionally with a given namespace (to avoid overlap).
23
	 *
24
	 * @param string $namespace The namespace for this cache to write to.
25
	 * @warning namespaces of the same name are shared!
26
	 */
27 2
	public function __construct($namespace = 'default') {
28 2
		$this->setNamespace($namespace);
29 2
		$this->clear();
30 2
	}
31
32
	/**
33
	 * Save a key
34
	 *
35
	 * @param string $key  Name
36
	 * @param string $data Value
37
	 *
38
	 * @return boolean
39
	 */
40
	public function save($key, $data) {
41
		$namespace = $this->getNamespace();
42
43
		\ElggStaticVariableCache::$__cache[$namespace][$key] = $data;
44
45
		return true;
46
	}
47
48
	/**
49
	 * Load a key
50
	 *
51
	 * @param string $key    Name
52
	 * @param int    $offset Offset
53
	 * @param int    $limit  Limit
54
	 *
55
	 * @return string
56
	 */
57
	public function load($key, $offset = 0, $limit = null) {
58
		$namespace = $this->getNamespace();
59
60
		if (isset(\ElggStaticVariableCache::$__cache[$namespace][$key])) {
61
			return \ElggStaticVariableCache::$__cache[$namespace][$key];
62
		}
63
64
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by ElggStaticVariableCache::load of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
	}
66
67
	/**
68
	 * Invalidate a given key.
69
	 *
70
	 * @param string $key Name
71
	 *
72
	 * @return bool
73
	 */
74
	public function delete($key) {
75
		$namespace = $this->getNamespace();
76
77
		unset(\ElggStaticVariableCache::$__cache[$namespace][$key]);
78
79
		return true;
80
	}
81
82
	/**
83
	 * Clears the cache for a particular namespace
84
	 *
85
	 * @return void
86
	 */
87 4
	public function clear() {
88 4
		$namespace = $this->getNamespace();
89
90 4
		if (!isset(\ElggStaticVariableCache::$__cache)) {
91 1
			\ElggStaticVariableCache::$__cache = array();
92 1
		}
93
94 4
		\ElggStaticVariableCache::$__cache[$namespace] = array();
95 4
	}
96
}
97