Skip to content

Commit 4fb55ef

Browse files
committed
Use ?string instead of false|string
1 parent 413e1cd commit 4fb55ef

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/Storage/EtcdStorage.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,37 @@ public function __construct(?ClientInterface $client = null)
1717
$this->client = $client ?? new Client();
1818
}
1919

20-
public function putIf(string $key, string $value, bool|string $previousValue, bool $returnNewValueOnFail): bool|string
20+
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail): bool|string
2121
{
2222
try {
23-
return $this->client->putIf($key, $value, $previousValue, $returnNewValueOnFail);
23+
return $this->client->putIf($key, $value, $previousValue ?? false, $returnNewValueOnFail);
2424
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
2525
throw new StorageException($e->getMessage(), $e->getCode(), $e);
2626
}
2727
}
2828

29-
public function deleteIf(string $key, $previousValue, bool $returnNewValueOnFail = false): bool|string
29+
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string
3030
{
3131
try {
32-
return $this->client->deleteIf($key, $previousValue, $returnNewValueOnFail);
32+
return $this->client->deleteIf($key, $previousValue ?? false, $returnNewValueOnFail);
3333
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
3434
throw new StorageException($e->getMessage(), $e->getCode(), $e);
3535
}
3636
}
3737

3838

39-
public function get(string $key): bool|string
39+
public function get(string $key): ?string
4040
{
4141
try {
42-
return $this->client->get($key);
42+
$value = $this->client->get($key);
4343
} catch (UnavailableException | DeadlineExceededException | UnknownException $e) {
4444
throw new StorageException($e->getMessage(), $e->getCode(), $e);
4545
}
46+
47+
if ($value === false) {
48+
return null;
49+
}
50+
51+
return $value;
4652
}
4753
}

src/Storage/StorageInterface.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,40 @@
1010
interface StorageInterface
1111
{
1212
/**
13-
* Put `$value` if `$key` value matches `$previousValue` otherwise `$returnNewValueOnFail`
13+
* Put `$value` if `$key` value matches `$previousValue` and return true. If the new value does not match and
14+
* `$returnNewValueOnFail` is true return the new value, otherwise return false.
1415
* @param string $key
1516
* @param string $value The new value to set
16-
* @param bool|string $previousValue The previous value to compare against
17-
* @param bool $returnNewValueOnFail
18-
* @return bool|string
17+
* @param string|null $previousValue The previous value to compare against. If null is provided, the comparison
18+
* should check that the key does not exist yet.
19+
* @param bool $returnNewValueOnFail if true the new value of the key should be returned if the operation fails
20+
* @return bool|string true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is false,
21+
* otherwise the new value of the key
1922
* @throws StorageException a known, retryable error occurred
2023
* @throws Exception an unknown error occurred
2124
*/
22-
public function putIf(string $key, string $value, bool|string $previousValue, bool $returnNewValueOnFail): bool|string;
25+
public function putIf(string $key, string $value, ?string $previousValue, bool $returnNewValueOnFail): bool|string;
2326

2427
/**
2528
* Delete if $key value matches $previous value otherwise $returnNewValueOnFail
2629
*
2730
* @param string $key
28-
* @param bool|string $previousValue The previous value to compare against
31+
* @param string|null $previousValue The previous value to compare against. If null is provided, the comparison
32+
* should check that the key does not exist yet.
2933
* @param bool $returnNewValueOnFail
30-
* @return bool|string
34+
* @return bool|string true if the operation succeeded, false if it failed and `$returnNewValueOnFail` is false,
35+
* otherwise the new value of the key
3136
* @throws StorageException a known, retryable error occurred
3237
* @throws Exception an unknown error occurred
3338
*/
34-
public function deleteIf(string $key, bool|string $previousValue, bool $returnNewValueOnFail = false): bool|string;
39+
public function deleteIf(string $key, ?string $previousValue, bool $returnNewValueOnFail = false): bool|string;
3540

3641
/**
3742
* Get the value of a key
3843
* @param string $key
39-
* @return bool|string
44+
* @return string|null the value of the key or null if the key does not exist
4045
* @throws StorageException a known, retryable error occurred
4146
* @throws Exception an unknown error occurred
4247
*/
43-
public function get(string $key): bool|string;
48+
public function get(string $key): ?string;
4449
}

0 commit comments

Comments
 (0)