/** * Shared between increment/decrement: both have mostly the same logic * (decrement just increments a negative value), but need their validation * split up (increment won't accept negative values). * * @param string $key * @param int $offset * @param int $initial * @param int $expire * * @return int|bool */ protected function doIncrement($key, $offset, $initial, $expire) { $value = $this->get($key, $token); if ($value === false) { $this->set($key, $initial, $expire); return $initial; } if (!is_numeric($value) || $value < 0) { return false; } $value += $offset; // value can never be lower than 0 $value = max(0, $value); $success = $this->client->cas($token, $key, $value, $expire); return $success ? $value : false; }