InteractsWithCache::getCache()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 0
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Kaylyu\Alipay\Kernel\Traits;
4
5
use Kaylyu\Alipay\Kernel\ServiceContainer;
6
use Psr\SimpleCache\CacheInterface;
7
use Symfony\Component\Cache\Simple\FilesystemCache;
8
9
/**
10
 * Trait InteractsWithCache.
11
 */
12
trait InteractsWithCache
13
{
14
    /**
15
     * @var \Psr\SimpleCache\CacheInterface
16
     */
17
    protected $cache;
18
19
    /**
20
     * Get cache instance.
21
     *
22
     * @return \Psr\SimpleCache\CacheInterface
23
     */
24
    public function getCache()
25
    {
26
        if ($this->cache) {
27
            return $this->cache;
28
        }
29
30
        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer
31
            && isset($this->app['cache']) && $this->app['cache'] instanceof CacheInterface) {
0 ignored issues
show
Bug introduced by
The class Psr\SimpleCache\CacheInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
32
            return $this->cache = $this->app['cache'];
0 ignored issues
show
Bug introduced by
The property app 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...
33
        }
34
35
        return $this->cache = $this->createDefaultCache();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createDefaultCache() of type object<Symfony\Component...Simple\FilesystemCache> is incompatible with the declared type object<Psr\SimpleCache\CacheInterface> of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    /**
39
     * Set cache instance.
40
     *
41
     * @param \Psr\SimpleCache\CacheInterface $cache
42
     *
43
     * @return $this
44
     */
45
    public function setCache(CacheInterface $cache)
46
    {
47
        $this->cache = $cache;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @return \Symfony\Component\Cache\Simple\FilesystemCache
54
     */
55
    protected function createDefaultCache()
56
    {
57
        return new FilesystemCache();
58
    }
59
}
60