Completed
Push — master ( 2106b4...7c92dc )
by Jean-Christophe
01:31
created

ApcuCache::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace micro\cache;
3
4
/**
5
 * This class is responsible for storing Arrays in PHP files.
6
 */
7
class ApcuCache extends AbstractDataCache{
8
	/**
9
	 * Initializes the apcu cache-provider
10
	 */
11
	public function __construct($root,$postfix="") {
12
		parent::__construct($root,$postfix);
13
	}
14
15
	/**
16
	 * Check if annotation-data for the key has been stored.
17
	 * @param string $key cache key
18
	 * @return bool true if data with the given key has been stored; otherwise false
19
	 */
20
	public function exists($key) {
21
		return \apcu_exists($this->getRealKey($key));
1 ignored issue
show
Bug Compatibility introduced by
The expression \apcu_exists($this->getRealKey($key)); of type boolean|string[] adds the type string[] to the return on line 21 which is incompatible with the return type declared by the abstract method micro\cache\AbstractDataCache::exists of type boolean.
Loading history...
22
	}
23
24
	public function store($key, $code, $php=true) {
25
		$this->storeContent($key, $code);
26
	}
27
28
	/**
29
	 * Caches the given data with the given key.
30
	 * @param string $key cache key
31
	 * @param string $code the source-code to be cached
0 ignored issues
show
Bug introduced by
There is no parameter named $code. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
32
	 * @throws AnnotationException if file could not be written
33
	 */
34
	protected function storeContent($key,$content) {
35
		\apcu_store($this->getRealKey($key), $content);
36
	}
37
38
	protected function getRealKey($key){
39
		return \md5($key);
40
	}
41
42
	/**
43
	 * Fetches data stored for the given key.
44
	 * @param string $key cache key
45
	 * @return mixed the cached data
46
	 */
47
	public function fetch($key) {
48
		$result=\apcu_fetch($this->getRealKey($key));
49
		return eval($result);
1 ignored issue
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
50
	}
51
52
	/**
53
	 * return data stored for the given key.
54
	 * @param string $key cache key
55
	 * @return mixed the cached data
56
	 */
57
	public function file_get_contents($key) {
58
		return \apcu_fetch($this->getRealKey($key));
59
	}
60
61
	/**
62
	 * Returns the timestamp of the last cache update for the given key.
63
	 *
64
	 * @param string $key cache key
65
	 * @return int unix timestamp
66
	 */
67
	public function getTimestamp($key) {
68
			$key=$this->getRealKey($key);
69
			$cache = \apc_cache_info();
70
			if (empty($cache['cache_list'])) {
71
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method micro\cache\AbstractDataCache::getTimestamp of type integer.

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...
72
			}
73
			foreach ($cache['cache_list'] as $entry) {
74
				if ($entry['info'] != $key) {
75
					continue;
76
				}
77
				$creationTime = $entry['creation_time'];
78
				return $creationTime;
79
			}
80
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method micro\cache\AbstractDataCache::getTimestamp of type integer.

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...
81
	}
82
83
	public function remove($key) {
84
		\apcu_delete($this->getRealKey($key));
85
	}
86
87
	public function clear() {
88
		\apcu_clear_cache();
89
	}
90
}
91