Completed
Push — 1.x ( 3cfddd...9b1da2 )
by Akihito
03:05 queued 03:02
created

CacheDirProvider::get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 0
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 Ray\Di\ProviderInterface;
10
11
use function is_writable;
12
use function mkdir;
13
14
/**
15
 * Provide tmp directory
16
 *
17
 * @implements ProviderInterface<string>
18
 */
19
final class CacheDirProvider implements ProviderInterface
20
{
21
    private const CACHE_DIRNAME = '/cache';
22
23
    public function __construct(private AbstractAppMeta $appMeta)
24
    {
25
    }
26
27
    public function get(): string
28
    {
29
        $cacheDir = $this->appMeta->tmpDir . self::CACHE_DIRNAME;
30
        if (! is_writable($cacheDir) && ! @mkdir($cacheDir)) {
31
            // @codeCoverageIgnoreStart
32
            throw new DirectoryNotWritableException($cacheDir);
33
            // @codeCoverageIgnoreEnd
34
        }
35
36
        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...
37
    }
38
}
39