Passed
Pull Request — main (#3)
by Michael
03:11
created

InteractsWithCache::cacheBindingFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\AutoBinder\Traits;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
10
trait InteractsWithCache
11
{
12
    /**
13
     * Use the bindings from the cache.
14
     *
15
     * @return void
16
     * @throws ContainerExceptionInterface
17
     * @throws NotFoundExceptionInterface
18
     */
19
    protected function fromCache(): void
20
    {
21
        collect(cache()->get($this->cacheClue()))->each(
0 ignored issues
show
Bug introduced by
It seems like cache()->get($this->cacheClue()) can also be of type Illuminate\Contracts\Cache\Repository; however, parameter $value of collect() does only seem to accept Illuminate\Contracts\Support\Arrayable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        collect(/** @scrutinizer ignore-type */ cache()->get($this->cacheClue()))->each(
Loading history...
Bug introduced by
It seems like cacheClue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
        collect(cache()->get($this->/** @scrutinizer ignore-call */ cacheClue()))->each(
Loading history...
22
            fn ($concrete, $interface) => app()->{$this->bindingType}($interface, $concrete)
23
        );
24
    }
25
26
    /**
27
     * Cache the binding.
28
     *
29
     * @param  string  $interface
30
     * @param  \Closure|string  $concrete
31
     *
32
     * @return void
33
     * @throws ContainerExceptionInterface
34
     * @throws NotFoundExceptionInterface
35
     */
36 15
    protected function cacheBindingFor(string $interface, \Closure|string $concrete): void
37
    {
38 15
        $clue = $this->cacheClue();
39
40 15
        $cache = cache()->get($clue);
41
42 15
        $cache[$interface] = $concrete;
43
44 15
        cache()->put($clue, $cache);
45
    }
46
}
47