Skip to content

Commit

Permalink
Improve arguments validation + Minor refactoring + Psalm 1 (#62)
Browse files Browse the repository at this point in the history
* fix

* improve

* psalm 1

* ci

* Improve list of memcached server validation

* Check key type when multiple keys passed

* fix

* fix

* fix
  • Loading branch information
vjik committed Feb 17, 2024
1 parent edd5a37 commit 415561e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
21 changes: 12 additions & 9 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- 'phpunit.xml.dist'

push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'README.md'
Expand All @@ -22,19 +24,15 @@ on:
name: static analysis

jobs:
mutation:
psalm:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- 8.0
- 8.1
os: ['ubuntu-latest']
php: ['8.0', '8.1', '8.2', '8.3']

steps:
- name: Checkout
Expand Down Expand Up @@ -64,5 +62,10 @@ jobs:
- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Static analysis
run: vendor/bin/psalm --shepherd --stats --output-format=checkstyle | cs2pr --graceful-warnings --colorize
- name: Static analysis PHP 8.0
if: matrix.php == '8.0'
run: vendor/bin/psalm --config=psalm80.xml --shepherd --stats --output-format=github --php-version=${{ matrix.php }}

- name: Static analysis PHP 8.1+
if: matrix.php != '8.0'
run: vendor/bin/psalm --shepherd --stats --output-format=github --php-version=${{ matrix.php }}
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 2.0.1 under development

- no changes in this release.
- Enh #62: Remove unneeded casting to array in private method `Memcached::iterableToArray()` (@vjik)
- Enh #62: Improve list of memcached server validation (@vjik)

## 2.0.0 February 15, 2023

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"rector/rector": "^1.0.1",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.6",
"vimeo/psalm": "^4.30|^5.22",
"yiisoft/di": "^1.2"
},
"autoload": {
Expand Down
15 changes: 7 additions & 8 deletions psalm.xml
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>
19 changes: 19 additions & 0 deletions psalm80.xml
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>
33 changes: 29 additions & 4 deletions src/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * @see https://www.php.net/manual/en/memcached.getserverlist.php */ foreach ($this->cache->getServerList() as $existingServer) { - $existingServers["{$existingServer['host']}:{$existingServer['port']}"] = true; + $existingServers["{$existingServer['host']}:{$existingServer['port']}"] = false; } foreach ($servers as $server) { if (!array_key_exists("{$server[0]}:{$server[1]}", $existingServers)) {
}
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ $normalized = []; foreach ($servers as $server) { 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' . ' containing hostname (string), port (int), and, optionally, weight (int) of the server.'); + throw new InvalidArgumentException(' containing hostname (string), port (int), and, optionally, weight (int) of the server.' . 'Each entry in servers parameter is supposed to be an array'); } /** * @psalm-var array{host:string,port:int,weight?:int} $server Need for PHP 8.0

Check warning on line 253 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $normalized = []; foreach ($servers as $server) { 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' . ' containing hostname (string), port (int), and, optionally, weight (int) of the server.'); + throw new InvalidArgumentException(' 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

Check warning on line 253 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ $normalized = []; foreach ($servers as $server) { 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' . ' containing hostname (string), port (int), and, optionally, weight (int) of the server.'); + throw new InvalidArgumentException('Each entry in servers parameter is supposed to be an array'); } /** * @psalm-var array{host:string,port:int,weight?:int} $server Need for PHP 8.0
. ' 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];
}
Expand All @@ -249,6 +271,9 @@ private function validateKey(string $key): void
}
}

/**
* @param string[] $keys
*/
private function validateKeys(array $keys): void
{
foreach ($keys as $key) {
Expand Down

0 comments on commit 415561e

Please sign in to comment.