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
Pull Request — master (#31)
by
unknown
02:55
created

Cache::objectHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 4
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);
0 ignored issues
show
Bug introduced by
Since objectHash() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of objectHash() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
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];
0 ignored issues
show
Bug introduced by
Since objectHash() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of objectHash() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
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;
0 ignored issues
show
Bug introduced by
Since objectHash() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of objectHash() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
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
    private 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