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 ( 93c765...c24b9a )
by Freek
03:42 queued 02:05
created

Cache::forget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 1
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 = spl_object_hash($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[spl_object_hash($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);
52
53
        static::$values[spl_object_hash($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 addDestroyListener($object)
67
    {
68
        $randomPropertyName = '___once_destroy_listener';
69
70
        if (isset($object->$randomPropertyName)) {
71
            return;
72
        }
73
74
        $object->$randomPropertyName = new class($object) {
75
            public function __construct($object)
76
            {
77
                $this->objectHash = spl_object_hash($object);
0 ignored issues
show
Bug introduced by
The property objectHash does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
            }
79
80
            public function __destruct()
81
            {
82
                Cache::forget($this->objectHash);
83
            }
84
        };
85
    }
86
}
87