Passed
Push — cache-dir ( a01d8a )
by Akihito
14:39 queued 11s
created

LocalCacheProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 22
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 7 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Context\ProdModule;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use Ray\Di\ProviderInterface;
9
use Ray\PsrCacheModule\Annotation\CacheNamespace;
10
use Ray\PsrCacheModule\ApcuAdapter;
11
use Ray\PsrCacheModule\FilesystemAdapter;
12
use Symfony\Component\Cache\Adapter\AbstractAdapter;
13
14
use const PHP_SAPI;
15
16
/**
17
 * Provide APCu cache adapter if available, otherwise file cache adapter
18
 *
19
 * @implements ProviderInterface<ApcuAdapter|FilesystemAdapter>
20
 */
21
final class LocalCacheProvider implements ProviderInterface
22
{
23
    /** @var string  */
24
    private $cacheDir;
25
26
    /** @var string  */
27
    private $namespace;
28
29
    #[CacheNamespace('namespace')]
30
    public function __construct(AbstractAppMeta $appMeta, string $namespace = '')
31
    {
32
        $this->cacheDir = $appMeta->tmpDir;
33
        $this->namespace = $namespace;
34
    }
35
36
    public function get(): AbstractAdapter
37
    {
38
        return PHP_SAPI !== 'cli' && ApcuAdapter::isSupported() ?
0 ignored issues
show
Bug Best Practice introduced by
The expression return PHP_SAPI !== 'cli...ce, 0, $this->cacheDir) returns the type Ray\PsrCacheModule\ApcuA...odule\FilesystemAdapter which is incompatible with the return type mandated by Ray\Di\ProviderInterface::get() of Ray\Di\T.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
39
            // @codeCoverageIgnoreStart
40
            new ApcuAdapter($this->namespace) :
41
            // @codeCoverageIgnoreEnd
42
            new FilesystemAdapter($this->namespace, 0, $this->cacheDir);
43
    }
44
}
45