Issues (2)

src/Adapter/PredisAdapter.php (2 issues)

1
<?php
2
3
namespace IQParts\Cache\Adapter;
4
5
use IQParts\Cache\Serializer\SerializerInterface;
6
use Predis\Client;
7
use Predis\ClientInterface;
8
9
final class PredisAdapter implements CacheAdapterInterface
10
{
11
    /**
12
     * @var Client
13
     */
14
    private $client;
15
    /**
16
     * @var SerializerInterface
17
     */
18
    private $serializer;
19
    /**
20
     * @var null
21
     */
22
    private $defaultTtl;
23
24
    /**
25
     * PredisAdapter constructor.
26
     * @param ClientInterface $client
27
     * @param SerializerInterface $serializer
28
     * @param int $defaultTtl
29
     */
30
    public function __construct(ClientInterface $client, SerializerInterface $serializer, $defaultTtl = null)
31
    {
32
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type Predis\ClientInterface, but the property $client was declared to be of type Predis\Client. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
33
        $this->serializer = $serializer;
34
        $this->defaultTtl = $defaultTtl;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultTtl can also be of type integer. However, the property $defaultTtl is declared as type null. 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...
35
    }
36
37
    /**
38
     * @param string $key
39
     * @return mixed
40
     */
41
    public function get(string $key)
42
    {
43
        return $this->serializer->deserialize($this->client->get($key));
44
    }
45
46
    /**
47
     * @param string $key
48
     * @param $value
49
     * @param int|null $ttl TTL in seconds
50
     */
51
    public function set(string $key, $value, int $ttl = null): void
52
    {
53
        if ($ttl !== null && $ttl !== 0) {
54
            $this->client->setex($key, $ttl ?? $this->defaultTtl, $this->serializer->serialize($value));
55
        } else {
56
            $this->client->set($key, $this->serializer->serialize($value));
57
        }
58
    }
59
60
    /**
61
     * @param string $key
62
     * @return integer
63
     */
64
    public function delete(string $key): int
65
    {
66
        return $this->client->del([$key]);
67
    }
68
69
    /**
70
     * @param string $key
71
     * @return mixed
72
     */
73
    public function keys($key = '*')
74
    {
75
        return $this->client->keys($key);
76
    }
77
78
    /**
79
     * @param $key
80
     * @return int
81
     */
82
    public function ttl($key): int
83
    {
84
        return $this->client->ttl($key);
85
    }
86
}