GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e9c8b0...012861 )
by Freek
13s
created

Cache::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Once;
4
5
class Cache
6
{
7
    /** @var array */
8
    public static $values = [];
9
10
    /**
11
     * Determine if a value exists for a given object / hash.
12
     *
13
     * @param  mixed  $object
14
     * @param  string  $backtraceHash
15
     *
16
     * @return bool
17
     */
18
    public static function has($object, string $backtraceHash): bool
19
    {
20
        $objectHash = static::objectHash($object);
21
22
        if (! isset(static::$values[$objectHash])) {
23
            return false;
24
        }
25
26
        return array_key_exists($backtraceHash, static::$values[$objectHash]);
27
    }
28
29
    /**
30
     * Retrieve a value for an object / hash.
31
     *
32
     * @param  mixed  $object
33
     * @param  string  $backtraceHash
34
     *
35
     * @return mixed
36
     */
37
    public static function get($object, string $backtraceHash)
38
    {
39
        return static::$values[static::objectHash($object)][$backtraceHash];
40
    }
41
42
    /**
43
     * Set a cached value for an object / hash.
44
     *
45
     * @param  mixed  $object
46
     * @param  string  $backtraceHash
47
     * @param  mixed  $value
48
     */
49
    public static function set($object, string $backtraceHash, $value)
50
    {
51
        static::addDestroyListener($object, $backtraceHash);
0 ignored issues
show
Unused Code introduced by
The call to Cache::addDestroyListener() has too many arguments starting with $backtraceHash.

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.

Loading history...
52
53
        static::$values[static::objectHash($object)][$backtraceHash] = $value;
54
    }
55
56
    /**
57
     * Forget the stored items for the given objectHash.
58
     *
59
     * @param string $objectHash
60
     */
61
    public static function forget(string $objectHash)
62
    {
63
        unset(static::$values[$objectHash]);
64
    }
65
66
    protected static function objectHash($object) : string
67
    {
68
        return is_string($object) ? $object : spl_object_hash($object);
69
    }
70
71
    protected static function addDestroyListener($object)
72
    {
73
        if (is_string($object)) {
74
            return;
75
        }
76
77
        $randomPropertyName = '___once_listener__'.rand(1, 1000000);
78
79
        if (isset($object->$randomPropertyName)) {
80
            return;
81
        }
82
83
        $object->$randomPropertyName = new Listener($object);
84
    }
85
}
86