1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Once; |
4
|
|
|
|
5
|
|
|
class Cache |
6
|
|
|
{ |
7
|
|
|
/** @var array */ |
8
|
|
|
public static $values = []; |
9
|
|
|
|
10
|
|
|
/** @var bool */ |
11
|
|
|
protected static $enabled = true; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Determine if a value exists for a given object / hash. |
15
|
|
|
* |
16
|
|
|
* @param mixed $object |
17
|
|
|
* @param string $backtraceHash |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public static function has($object, string $backtraceHash): bool |
22
|
|
|
{ |
23
|
|
|
$objectHash = static::objectHash($object); |
24
|
|
|
|
25
|
|
|
if (! isset(static::$values[$objectHash])) { |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return array_key_exists($backtraceHash, static::$values[$objectHash]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Retrieve a value for an object / hash. |
34
|
|
|
* |
35
|
|
|
* @param mixed $object |
36
|
|
|
* @param string $backtraceHash |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public static function get($object, string $backtraceHash) |
41
|
|
|
{ |
42
|
|
|
return static::$values[static::objectHash($object)][$backtraceHash]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set a cached value for an object / hash. |
47
|
|
|
* |
48
|
|
|
* @param mixed $object |
49
|
|
|
* @param string $backtraceHash |
50
|
|
|
* @param mixed $value |
51
|
|
|
*/ |
52
|
|
|
public static function set($object, string $backtraceHash, $value) |
53
|
|
|
{ |
54
|
|
|
static::addDestroyListener($object, $backtraceHash); |
|
|
|
|
55
|
|
|
|
56
|
|
|
static::$values[static::objectHash($object)][$backtraceHash] = $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Forget the stored items for the given objectHash. |
61
|
|
|
* |
62
|
|
|
* @param string $objectHash |
63
|
|
|
*/ |
64
|
|
|
public static function forget(string $objectHash) |
65
|
|
|
{ |
66
|
|
|
unset(static::$values[$objectHash]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Flush the entire cache |
71
|
|
|
*/ |
72
|
|
|
public static function flush() |
73
|
|
|
{ |
74
|
|
|
static::$values = []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected static function objectHash($object) : string |
78
|
|
|
{ |
79
|
|
|
return is_string($object) ? $object : spl_object_hash($object); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected static function addDestroyListener($object) |
83
|
|
|
{ |
84
|
|
|
if (is_string($object)) { |
85
|
|
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$randomPropertyName = '___once_listener__'.rand(1, 1000000); |
89
|
|
|
|
90
|
|
|
if (isset($object->$randomPropertyName)) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$object->$randomPropertyName = new Listener($object); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public static function disable() |
98
|
|
|
{ |
99
|
|
|
static::$enabled = false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public static function enable() |
103
|
|
|
{ |
104
|
|
|
static::$enabled = true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function isEnabled(): bool |
108
|
|
|
{ |
109
|
|
|
return static::$enabled; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.