-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve arguments validation + Minor refactoring + Psalm 1 (#62)
* fix * improve * psalm 1 * ci * Improve list of memcached server validation * Check key type when multiple keys passed * fix * fix * fix
- Loading branch information
Showing
6 changed files
with
70 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
errorLevel="2" | ||
errorLevel="1" | ||
findUnusedBaselineEntry="true" | ||
findUnusedCode="false" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://getpsalm.org/schema/config" | ||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
> | ||
<projectFiles> | ||
<directory name="src"/> | ||
<directory name="src" /> | ||
<ignoreFiles> | ||
<directory name="vendor" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
|
||
<issueHandlers> | ||
<UndefinedClass> | ||
<errorLevel type="suppress"> | ||
<referencedClass name="Memcached" /> | ||
</errorLevel> | ||
</UndefinedClass> | ||
<MixedAssignment errorLevel="suppress" /> | ||
<RiskyTruthyFalsyComparison errorLevel="suppress" /> | ||
</issueHandlers> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0"?> | ||
<psalm | ||
errorLevel="1" | ||
findUnusedBaselineEntry="true" | ||
findUnusedCode="false" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://getpsalm.org/schema/config" | ||
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<ignoreFiles> | ||
<directory name="vendor" /> | ||
</ignoreFiles> | ||
</projectFiles> | ||
<issueHandlers> | ||
<MixedAssignment errorLevel="suppress" /> | ||
</issueHandlers> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ | |
* | ||
* Note, there is no security measure to protected data in memcached. | ||
* All data in memcached can be accessed by any process running in the system. | ||
* | ||
* @psalm-type NewServerType=array{0:string,1:int,2?:int} | ||
*/ | ||
final class Memcached implements CacheInterface | ||
{ | ||
|
@@ -170,11 +172,14 @@ private function normalizeTtl(DateInterval|int|string|null $ttl): int | |
|
||
/** | ||
* Converts iterable to array. | ||
* | ||
* @psalm-template T | ||
* @psalm-param iterable<T> $iterable | ||
* @psalm-return array<array-key,T> | ||
*/ | ||
private function iterableToArray(iterable $iterable): array | ||
{ | ||
/** @psalm-suppress RedundantCast */ | ||
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable; | ||
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable; | ||
} | ||
|
||
/** | ||
|
@@ -196,12 +201,18 @@ private function initServers(array $servers, string $persistentId): void | |
|
||
/** | ||
* Returns the list of the servers that are not in the pool. | ||
* | ||
* @psalm-param list<NewServerType> $servers | ||
*/ | ||
private function getNewServers(array $servers): array | ||
{ | ||
$existingServers = []; | ||
$newServers = []; | ||
|
||
/** | ||
* @psalm-var array{host:string,port:int} $existingServer | ||
* @see https://www.php.net/manual/en/memcached.getserverlist.php | ||
*/ | ||
foreach ($this->cache->getServerList() as $existingServer) { | ||
$existingServers["{$existingServer['host']}:{$existingServer['port']}"] = true; | ||
Check warning on line 217 in src/Memcached.php GitHub Actions / PHP 8.3-ubuntu-latest
|
||
} | ||
|
@@ -223,18 +234,29 @@ private function getNewServers(array $servers): array | |
* @throws InvalidArgumentException If the servers format is incorrect. | ||
* | ||
* @return array The normalized servers. | ||
* | ||
* @psalm-return list<NewServerType> $servers | ||
*/ | ||
private function normalizeServers(array $servers): array | ||
{ | ||
$normalized = []; | ||
|
||
foreach ($servers as $server) { | ||
if (!is_array($server) || !isset($server['host'], $server['port'])) { | ||
if ( | ||
!is_array($server) | ||
|| !isset($server['host'], $server['port']) | ||
|| !is_string($server['host']) | ||
|| !is_int($server['port']) | ||
|| (isset($server['weight']) && !is_int($server['weight'])) | ||
) { | ||
throw new InvalidArgumentException( | ||
'Each entry in servers parameter is supposed to be an array' | ||
Check warning on line 253 in src/Memcached.php GitHub Actions / PHP 8.3-ubuntu-latest
Check warning on line 253 in src/Memcached.php GitHub Actions / PHP 8.3-ubuntu-latest
Check warning on line 253 in src/Memcached.php GitHub Actions / PHP 8.3-ubuntu-latest
|
||
. ' containing hostname, port, and, optionally, weight of the server.', | ||
. ' containing hostname (string), port (int), and, optionally, weight (int) of the server.', | ||
); | ||
} | ||
/** | ||
* @psalm-var array{host:string,port:int,weight?:int} $server Need for PHP 8.0 | ||
*/ | ||
|
||
$normalized[] = [$server['host'], $server['port'], $server['weight'] ?? self::DEFAULT_SERVER_WEIGHT]; | ||
} | ||
|
@@ -249,6 +271,9 @@ private function validateKey(string $key): void | |
} | ||
} | ||
|
||
/** | ||
* @param string[] $keys | ||
*/ | ||
private function validateKeys(array $keys): void | ||
{ | ||
foreach ($keys as $key) { | ||
|