CacheDirProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 11 3
A __construct() 0 2 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Cache;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Package\Exception\DirectoryNotWritableException;
9
use Override;
10
use Ray\Di\ProviderInterface;
11
12
use function is_writable;
13
use function mkdir;
14
15
/**
16
 * Provide tmp directory
17
 *
18
 * @implements ProviderInterface<string>
19
 */
20
final class CacheDirProvider implements ProviderInterface
21
{
22
    private const CACHE_DIRNAME = '/cache';
23
24
    public function __construct(private AbstractAppMeta $appMeta)
25
    {
26
    }
27
28
    #[Override]
29
    public function get(): string
30
    {
31
        $cacheDir = $this->appMeta->tmpDir . self::CACHE_DIRNAME;
32
        if (! is_writable($cacheDir) && ! @mkdir($cacheDir)) {
33
            // @codeCoverageIgnoreStart
34
            throw new DirectoryNotWritableException($cacheDir);
35
            // @codeCoverageIgnoreEnd
36
        }
37
38
        return $cacheDir;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cacheDir returns the type string 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
    }
40
}
41