diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 80c9666..d94d87a 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -10,6 +10,8 @@ on: - 'phpunit.xml.dist' push: + branches: + - master paths-ignore: - 'docs/**' - 'README.md' @@ -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 @@ -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 }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 5359c24..6c03912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 4f7912d..4a57f31 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/psalm.xml b/psalm.xml index 533bebd..b48c894 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,6 +1,6 @@ - + + + + - - - - - - + + diff --git a/psalm80.xml b/psalm80.xml new file mode 100644 index 0000000..d091d59 --- /dev/null +++ b/psalm80.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + diff --git a/src/Memcached.php b/src/Memcached.php index 837efe7..5ab6de2 100644 --- a/src/Memcached.php +++ b/src/Memcached.php @@ -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 $iterable + * @psalm-return array */ 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 $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; } @@ -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 $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' - . ' 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) {