Passed
Push — master ( f13f78...5c1b24 )
by Ismayil
04:22
created

engine/classes/Elgg/Cache/Pool/InMemory.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elgg\Cache\Pool;
3
4
use Elgg\Cache\Pool;
5
use Stash;
6
use InvalidArgumentException;
7
8
/**
9
 * An in-memory implementation of a cache pool.
10
 *
11
 * NB: Data put into this cache is not persisted between requests.
12
 *
13
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
14
 *
15
 * @since 1.10.0
16
 *
17
 * @access private
18
 */
19
final class InMemory implements Pool {
20
	/**
21
	 * @var array
22
	 */
23
	private $values = [];
24
	
25
	/** @inheritDoc */
26 4
	public function get($key, callable $callback = null, $default = null) {
27 4
		if (!is_string($key) && !is_int($key)) {
28
			throw new InvalidArgumentException('key must be string or integer');
29
		}
30
31 4
		if (!array_key_exists($key, $this->values)) {
32 4
			if (!$callback) {
33
				return $default;
34
			}
35 4
			$this->values[$key] = call_user_func($callback);
36
		}
37 4
		return $this->values[$key];
38
	}
39
	
40
	/** @inheritDoc */
41 1 View Code Duplication
	public function invalidate($key) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42 1
		if (!is_string($key) && !is_int($key)) {
43
			throw new InvalidArgumentException('key must be string or integer');
44
		}
45
46 1
		unset($this->values[$key]);
47 1
	}
48
49
	/** @inheritDoc */
50 1 View Code Duplication
	public function put($key, $value) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51 1
		if (!is_string($key) && !is_int($key)) {
52
			throw new InvalidArgumentException('key must be string or integer');
53
		}
54
55 1
		$this->values[$key] = $value;
56 1
	}
57
}
58