IQParts /
cache
| 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
|
|||
| 33 | $this->serializer = $serializer; |
||
| 34 | $this->defaultTtl = $defaultTtl; |
||
|
0 ignored issues
–
show
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 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 | } |
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.