CacheStorage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 91
rs 10
wmc 8
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add() 0 4 1
A forever() 0 4 1
A has() 0 4 1
A destroy() 0 4 1
A flush() 0 4 1
A cache() 0 8 2
1
<?php
2
3
namespace Framgia\Jwt\Storage;
4
5
use Framgia\Jwt\Contracts\Storage;
6
use Illuminate\Contracts\Cache\Repository;
7
8
class CacheStorage implements Storage
9
{
10
    /**
11
     * @var \Illuminate\Contracts\Cache\Repository|\Illuminate\Contracts\Cache\Store
12
     */
13
    protected $cache;
14
15
    /**
16
     * @var string
17
     */
18
    protected $tag;
19
20
    /**
21
     * @param \Illuminate\Cache\CacheManager  $cache
22
     * @param string $tag
23
     */
24
    public function __construct(Repository $cache, $tag = 'jwt')
25
    {
26
        $this->cache = $cache;
27
        $this->tag = $tag;
28
    }
29
30
    /**
31
     * Add a new item into storage.
32
     *
33
     * @param  string  $key
34
     * @param  mixed  $value
35
     * @param  int  $minutes
36
     * @return void
37
     */
38
    public function add($key, $value, $minutes)
39
    {
40
        $this->cache()->put($key, $value, $minutes);
41
    }
42
43
    /**
44
     * @param  string  $key
45
     * @param  mixed  $value
46
     * @return void
47
     */
48
    public function forever($key, $value)
49
    {
50
        $this->cache()->forever($key, $value);
51
    }
52
53
    /**
54
     * Check whether a key exists in storage.
55
     *
56
     * @param  string  $key
57
     * @return bool
58
     */
59
    public function has($key)
60
    {
61
        return $this->cache()->has($key);
0 ignored issues
show
Bug introduced by
The method has does only exist in Illuminate\Contracts\Cache\Repository, but not in Illuminate\Contracts\Cache\Store.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
    }
63
64
    /**
65
     * Remove an item from storage.
66
     *
67
     * @param  string  $key
68
     * @return bool
69
     */
70
    public function destroy($key)
71
    {
72
        return $this->cache()->forget($key);
73
    }
74
75
    /**
76
     * Remove all items associated with the tag.
77
     *
78
     * @return void
79
     */
80
    public function flush()
81
    {
82
        $this->cache()->flush();
0 ignored issues
show
Bug introduced by
The method flush does only exist in Illuminate\Contracts\Cache\Store, but not in Illuminate\Contracts\Cache\Repository.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
83
    }
84
85
    /**
86
     * Return the cache instance with tags attached.
87
     *
88
     * @return \Illuminate\Contracts\Cache\Repository|\Illuminate\Contracts\Cache\Store
89
     */
90
    protected function cache()
91
    {
92
        if (! method_exists($this->cache, 'tags')) {
93
            return $this->cache;
94
        }
95
96
        return $this->cache->tags($this->tag);
97
    }
98
}
99