DataCacheTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A keyDataCache() 0 8 2
A getDataCache() 0 12 4
A resetDataCache() 0 8 1
1
<?php
2
3
namespace yiicod\base\traits;
4
5
/**
6
 * Class DataCacheTrait
7
 * Deduplicate data resolver.
8
 *
9
 * @package yiicod\base\traits
10
 */
11
trait DataCacheTrait
12
{
13
    private static $dataCache = [];
14
15
    /**
16
     * @param array $values
17
     *
18
     * @return string
19
     */
20
    public static function keyDataCache($values = [])
21
    {
22
        if (is_array($values)) {
23
            return implode('', array_merge([__CLASS__], $values));
24
        }
25
26
        return __CLASS__ . $values;
27
    }
28
29
    /**
30
     * Get value by key. If value is not set then will be called callback
31
     *
32
     * @param string|array $key
33
     * @param callable $callback
34
     * @param bool $reset
35
     *
36
     * @return mixed
37
     */
38
    public static function getDataCache($key, $callback, $reset = false)
39
    {
40
        if (true === $reset) {
41
            self::resetDataCache($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by parameter $key on line 38 can also be of type array; however, yiicod\base\traits\DataC...Trait::resetDataCache() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
        }
43
        $key = self::keyDataCache($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by self::keyDataCache($key) on line 43 can also be of type string; however, yiicod\base\traits\DataCacheTrait::keyDataCache() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
        if (false === array_key_exists($key, self::$dataCache) || true === $reset) {
45
            self::$dataCache[$key] = $callback();
46
        }
47
48
        return self::$dataCache[$key];
49
    }
50
51
    /**
52
     * Reset data by key
53
     *
54
     * @param string $key
55
     *
56
     * @return bool
57
     */
58
    public static function resetDataCache($key)
59
    {
60
        $key = self::keyDataCache($key);
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61
62
        unset(self::$dataCache[$key]);
63
64
        return true;
65
    }
66
}
67