Completed
Push — master ( 04fddd...f4fff3 )
by Marcin
06:19
created

ImdbCache::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 18.02.2018
5
 * Time: 20:00
6
 */
7
8
namespace mrcnpdlk\Xmdb;
9
10
11
use Imdb\CacheInterface;
12
13
class ImdbCache implements CacheInterface
14
{
15
    /**
16
     * @var \Psr\SimpleCache\CacheInterface
17
     */
18
    private $oPsrCache;
19
    /**
20
     * @var integer
21
     */
22
    private $ttl;
23
24
    public function __construct(\Psr\SimpleCache\CacheInterface $oCache, integer $ttl = null)
25
    {
26
        $this->oPsrCache = $oCache;
27
        $this->ttl       = $ttl ?? 3600;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ttl ?? 3600 can also be of type object<mrcnpdlk\Xmdb\integer>. However, the property $ttl is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
28
    }
29
30
    public function get($key)
31
    {
32
        return $this->oPsrCache->get($key);
33
    }
34
35
    public function purge()
36
    {
37
        return $this;
38
    }
39
40
    public function set($key, $value)
41
    {
42
        $this->oPsrCache->set($key, $value, $this->ttl);
43
44
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (mrcnpdlk\Xmdb\ImdbCache) is incompatible with the return type declared by the interface Imdb\CacheInterface::set of type boolean.

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...
45
    }
46
}
47