Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve arguments validation + Minor refactoring + Psalm 1 #62

Merged
merged 10 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -113,7 +115,7 @@
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$values = $this->iterableToArray($values);
$this->validateKeysOfValues($values);

Check warning on line 118 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function setMultiple(iterable $values, null|int|DateInterval $ttl = null) : bool { $values = $this->iterableToArray($values); - $this->validateKeysOfValues($values); + return $this->cache->setMulti($values, $this->normalizeTtl($ttl)); } public function deleteMultiple(iterable $keys) : bool
return $this->cache->setMulti($values, $this->normalizeTtl($ttl));
}

Expand All @@ -123,12 +125,12 @@
$this->validateKeys($keys);

foreach ($this->cache->deleteMulti($keys) as $result) {
if ($result === false) {

Check warning on line 128 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ $keys = $this->iterableToArray($keys); $this->validateKeys($keys); foreach ($this->cache->deleteMulti($keys) as $result) { - if ($result === false) { + if ($result === true) { return false; } }
return false;
}
}

return true;

Check warning on line 133 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ return false; } } - return true; + return false; } public function has(string $key) : bool {
}

public function has(string $key): bool
Expand Down Expand Up @@ -170,11 +172,14 @@

/**
* 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,14 +201,20 @@

/**
* 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)) {
}

foreach ($servers as $server) {
Expand All @@ -223,18 +234,29 @@
* @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 @@
}
}

/**
* @param string[] $keys
*/
private function validateKeys(array $keys): void
{
foreach ($keys as $key) {
Expand All @@ -259,6 +284,6 @@
private function validateKeysOfValues(array $values): void
{
$keys = array_map('\strval', array_keys($values));
$this->validateKeys($keys);

Check warning on line 287 in src/Memcached.php

View workflow job for this annotation

GitHub Actions / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ private function validateKeysOfValues(array $values) : void { $keys = array_map('\\strval', array_keys($values)); - $this->validateKeys($keys); + } }
}
}
Loading