Issues (126)

src/Edm/Internal/Cache.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\Edm\Internal;
6
7
/**
8
 * Provides a caching mechanism for semantic properties.
9
 *
10
 * Using a Cache requires a function to compute the cached value at first access and a function
11
 * to create a result when the computation of the value involves a cyclic dependency. (The second
12
 * function can be omitted if a cycle is impossible.)
13
 *
14
 * @package AlgoWeb\ODataMetadata\Edm\Internal
15
 */
16
class Cache
17
{
18
    private $value = null;
19
    private $containerType;
20
    private $propertyType;
21
22
    /**
23
     * Cache constructor.
24
     * @param string $tContainer Type of the element that contains the cached property
25
     * @param string $tProperty  Type of the cached property
26
     */
27
    public function __construct(string $tContainer, string $tProperty)
28
    {
29
        $this->containerType = $tContainer;
30
        $this->propertyType  = $tProperty;
31
    }
32
33
    /**
34
     * In order to detect the boundaries of a cycle, we use two sentinel values. When we encounter the first
35
     * sentinel, we know that a cycle exists and that we are a point on that cycle.
36
     * When we reach an instance of the second sentinel, we know we have made a complete circuit of the cycle and that
37
     * every node in the cycle has been marked with the second sentinel.
38
     * @param  mixed    $container
39
     * @param  callable $compute
40
     * @return mixed;
41
     */
42
    public function getValue($container, callable $compute)
43
    {
44
        if ($this->value === CacheHelper::getUnknown()) {
0 ignored issues
show
Are you sure the usage of AlgoWeb\ODataMetadata\Ed...cheHelper::getUnknown() targeting AlgoWeb\ODataMetadata\Ed...cheHelper::getUnknown() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
            $this->value = $compute($container);
46
        }
47
        return $this->value;
48
    }
49
50
    public function clear(): void
51
    {
52
        if ($this->value !== CacheHelper::getCycleSentinel() && $this->value !== CacheHelper::getSecondPassCycleSentinel()) {
53
            $this->value = CacheHelper::getUnknown();
0 ignored issues
show
Are you sure the assignment to $this->value is correct as AlgoWeb\ODataMetadata\Ed...cheHelper::getUnknown() targeting AlgoWeb\ODataMetadata\Ed...cheHelper::getUnknown() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
        }
55
    }
56
}
57