-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Description
- Laravel Version:
master
- PHP Version: irrelevant
- Database Driver & Version: irrelevant
Description:
Following this trace:
framework/src/Illuminate/Cache/Repository.php
Lines 191 to 199 in 5bd78e1
/** | |
* Store an item in the cache. | |
* | |
* @param string $key | |
* @param mixed $value | |
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes | |
* @return bool | |
*/ | |
public function put($key, $value, $minutes = null) |
Then reaching getMinutes
:
framework/src/Illuminate/Cache/Repository.php
Lines 199 to 207 in 5bd78e1
public function put($key, $value, $minutes = null) | |
{ | |
if (is_array($key)) { | |
$result = $this->putMany($key, $value); | |
return $result; | |
} | |
if (! is_null($minutes = $this->getMinutes($minutes))) { |
Then parseDateInterval
:
framework/src/Illuminate/Cache/Repository.php
Lines 573 to 581 in 5bd78e1
/** | |
* Calculate the number of minutes with the given duration. | |
* | |
* @param \DateTimeInterface|\DateInterval|float|int $duration | |
* @return float|int|null | |
*/ | |
protected function getMinutes($duration) | |
{ | |
$duration = $this->parseDateInterval($duration); |
Which is defined as follows:
framework/src/Illuminate/Support/InteractsWithTime.php
Lines 41 to 53 in 5bd78e1
* If the given value is an interval, convert it to a DateTime instance. | |
* | |
* @param \DateTimeInterface|\DateInterval|int $delay | |
* @return \DateTimeInterface|int | |
*/ | |
protected function parseDateInterval($delay) | |
{ | |
if ($delay instanceof DateInterval) { | |
$delay = Carbon::now()->add($delay); | |
} | |
return $delay; | |
} |
There are some type issues here:
Illuminate\Cache\Repository#put()
has a possiblenull
as TTLIlluminate\Cache\Repository#getMinutes()
does not acceptnull
as TTL, but can returnnull
Illuminate\Support\InteractsWithTime#parseDateInterval()
does not supportnull
parameters
Besides being a type declaration issue, this leads to the weird side-effect of calling Illuminate\Cache\Repository#put('foo', 'bar', null)
being valid, yet not producing a cache entry.
Another (worse) design issue is that Illuminate\Cache\Repository#put('foo', 'bar')
(no third argument) is valid according to specification, but effectively dead/misleading code.
This is a design issue strictly related with #10504
Possible fixes
A possible fix is to:
- introduce a deprecation warning for
null
being passed to the method - add a throwing assertion on the given parameter, so that the 5 supported types of
CacheRepository::put()
are checked - remove support for
null
in a major release (6.0
)
The assertion is completely optional, as a type check in userland would suffice, but the interface is indeed too broad due to the type being too extensive.