Completed
Pull Request — final (#299)
by Georges
03:16
created

Driver::driverClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Drivers\Devtrue;
16
17
use phpFastCache\Core\DriverAbstract;
18
use phpFastCache\Core\StandardPsr6StructureTrait;
19
use phpFastCache\Entities\driverStatistic;
20
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
21
use phpFastCache\Exceptions\phpFastCacheDriverException;
22
use Psr\Cache\CacheItemInterface;
23
24
/**
25
 * Class Driver
26
 * @package phpFastCache\Drivers
27
 */
28 View Code Duplication
class Driver extends DriverAbstract
1 ignored issue
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
{
30
    /**
31
     * Driver constructor.
32
     * @param array $config
33
     * @throws phpFastCacheDriverException
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        $this->setup($config);
38
39
        if (!$this->driverCheck()) {
40
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
41
        }
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function driverCheck()
48
    {
49
        return true;
50
    }
51
52
    /**
53
     * @param \Psr\Cache\CacheItemInterface $item
54
     * @return mixed
55
     * @throws \InvalidArgumentException
56
     */
57
    protected function driverWrite(CacheItemInterface $item)
58
    {
59
        /**
60
         * Check for Cross-Driver type confusion
61
         */
62
        if ($item instanceof Item) {
63
            return false;
64
        } else {
65
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
66
        }
67
    }
68
69
    /**
70
     * @param \Psr\Cache\CacheItemInterface $item
71
     * @return mixed
72
     */
73
    protected function driverRead(CacheItemInterface $item)
74
    {
75
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method phpFastCache\Core\DriverAbstract::driverRead of type array.

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...
76
    }
77
78
    /**
79
     * @param \Psr\Cache\CacheItemInterface $item
80
     * @return bool
81
     * @throws \InvalidArgumentException
82
     */
83
    protected function driverDelete(CacheItemInterface $item)
84
    {
85
        /**
86
         * Check for Cross-Driver type confusion
87
         */
88
        if ($item instanceof Item) {
89
            return false;
90
        } else {
91
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
92
        }
93
    }
94
95
    /**
96
     * @return bool
97
     */
98
    protected function driverClear()
99
    {
100
        return false;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    protected function driverConnect()
107
    {
108
        return false;
109
    }
110
111
    /********************
112
     *
113
     * PSR-6 Extended Methods
114
     *
115
     *******************/
116
117
    /**
118
     * @return driverStatistic
119
     */
120
    public function getStats()
121
    {
122
        $stat = new driverStatistic();
123
        $stat->setInfo('[Devtrue] A void info string')
124
          ->setSize(0)
125
          ->setData(implode(', ', array_keys($this->itemInstances)))
126
          ->setRawData(true);
127
128
        return $stat;
129
    }
130
}