AbstractCache::detectCacheRepository()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect\Interceptor;
21
22
use Illuminate\Contracts\Cache\Repository;
23
use Ray\Aop\MethodInvocation;
24
use Ray\Aop\MethodInterceptor;
25
use Illuminate\Cache\CacheManager;
26
use Illuminate\Contracts\Cache\Factory;
27
use Doctrine\Common\Annotations\Annotation;
28
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait;
29
use Ytake\LaravelAspect\Annotation\Cacheable;
30
use Ytake\LaravelAspect\Annotation\CacheEvict;
31
use Ytake\LaravelAspect\Annotation\CachePut;
32
33
use function in_array;
34
use function is_array;
35
use function is_object;
36
use function is_null;
37
use function count;
38
use function get_class;
39
40
/**
41
 * Class AbstractCache
42
 */
43
abstract class AbstractCache implements MethodInterceptor
44
{
45
    use AnnotationReaderTrait;
46
47
    /** @var string */
48
    protected $join = ":";
49
50
    /** @var Factory|\Illuminate\Cache\CacheManager */
51
    protected static $factory;
52
53
    /**
54
     * @param string|array     $name
55
     * @param MethodInvocation $invocation
56
     *
57
     * @return array
58
     */
59
    protected function generateCacheName($name, MethodInvocation $invocation): array
60
    {
61
        if (is_array($name)) {
62
            throw new \InvalidArgumentException('Invalid argument');
63
        }
64
        if (is_null($name)) {
65
            $name = $invocation->getMethod()->name;
66
        }
67
68
        return [$name];
69
    }
70
71
    /**
72
     * @param MethodInvocation                         $invocation
73
     * @param Annotation|Cacheable|CacheEvict|CachePut $annotation
74
     * @param array                                    $keys
75
     *
76
     * @return array
77
     */
78
    protected function detectCacheKeys(
79
        MethodInvocation $invocation,
80
        Annotation $annotation,
81
        array $keys
82
    ): array {
83
        $arguments = $invocation->getArguments();
84
        foreach ($invocation->getMethod()->getParameters() as $parameter) {
85
            // exclude object
86
            if (in_array('#' . $parameter->name, $annotation->key)) {
87
                if (isset($arguments[$parameter->getPosition()])) {
88
                    if (!is_object($arguments[$parameter->getPosition()])) {
89
                        $keys[] = $arguments[$parameter->getPosition()];
90
                    }
91
                }
92
                if (!isset($arguments[$parameter->getPosition()])) {
93
                    $keys[] = $parameter->getDefaultValue();
94
                }
95
            }
96
        }
97
98
        return $keys;
99
    }
100
101
    /**
102
     * @param $annotation
103
     *
104
     * @return \Illuminate\Contracts\Cache\Repository
105
     */
106
    protected function detectCacheRepository($annotation): Repository
107
    {
108
        /** @var Factory|CacheManager $cacheFactory */
109
        $cacheFactory = self::$factory;
110
        $driver = (is_null($annotation->driver)) ? $cacheFactory->getDefaultDriver() : $annotation->driver;
111
        /** @var \Illuminate\Contracts\Cache\Repository|\Illuminate\Cache\TaggableStore $cache */
112
        $cache = $cacheFactory->store($driver);
113
        if (count($annotation->tags)) {
114
            $cache = $cache->tags($annotation->tags);
0 ignored issues
show
Bug introduced by
The method tags does only exist in Illuminate\Cache\TaggableStore, 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...
115
116
            return $cache;
117
        }
118
119
        return $cache;
120
    }
121
122
    /**
123
     * set cache instance
124
     *
125
     * @param Factory $factory
126
     */
127
    public function setCache(Factory $factory): void
128
    {
129
        static::$factory = $factory;
130
    }
131
132
    /**
133
     * @param string $glue
134
     * @param array  $array
135
     *
136
     * @return string
137
     */
138
    protected function recursiveImplode(string $glue, array $array): string
139
    {
140
        $return = '';
141
        $index = 0;
142
        $count = count($array);
143
        foreach ($array as $row) {
144
            if (is_array($row)) {
145
                $return .= $this->recursiveImplode($glue, $row);
146
            } else {
147
                $return .= (is_object($row)) ? get_class($row) : $row;
148
            }
149
            if ($index < $count - 1) {
150
                $return .= $glue;
151
            }
152
            ++$index;
153
        }
154
155
        return $return;
156
    }
157
}
158