Completed
Push — master ( 3b577d...673a4c )
by Sergey
02:52
created

PhpFileCache::getValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4
1
<?php
2
/**
3
 * Yii 2 PHP file cache.
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-php-file-cache
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-php-file-cache/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\caching;
11
12
use yii\caching\FileCache;
13
use yii\helpers\VarDumper;
14
15
/**
16
 * PhpFileCache implements a cache component using PHP files.
17
 */
18
class PhpFileCache extends FileCache
19
{
20
    /**
21
     * @inheritDoc
22
     */
23
    public $cacheFileSuffix = '.php';
24
25
    /**
26
     * @inheritDoc
27
     */
28 18
    public function init()
29
    {
30 18
        parent::init();
31 18
        if ($this->serializer === null) {
32 18
            $this->serializer = [
33
                function ($value) {
34 16
                    if ($value[0] instanceof ValueWithBootstrap) {
35 5
                        $bootstrap = trim($value[0]->bootstrap) . "\n\n";
36 5
                        $value[0] = $value[0]->value;
37 5
                    } else {
38 11
                        $bootstrap = '';
39
                    }
40 16
                    return "<?php\n\n{$bootstrap}return " . VarDumper::export($value) . ";\n";
41 18
                },
42 18
                function ($value) {
43 16
                    return $value;
44 18
                },
45
            ];
46 18
        }
47 18
    }
48
49
    /**
50
     * @inheritDoc
51
     */
52 18
    protected function getValue($key)
53
    {
54 18
        $cacheFile = $this->getCacheFile($key);
55 18
        if (@filemtime($cacheFile) > time()) {
56
            /** @noinspection PhpIncludeInspection */
57 17
            $cacheValue = @include $cacheFile;
58 17
            if (is_array($cacheValue) && array_key_exists(0, $cacheValue)) {
59 16
                return $cacheValue;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $cacheValue; (array) is incompatible with the return type of the parent method yii\caching\FileCache::getValue of type string|false.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
            }
61 1
        }
62
63 2
        return false;
64
    }
65
}
66