diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml index 58cd2d23..4bd07bf4 100644 --- a/.github/workflows/checks.yml +++ b/.github/workflows/checks.yml @@ -5,8 +5,6 @@ on: [ push, pull_request ] jobs: test: runs-on: ubuntu-latest - strategy: - fail-fast: true name: Code Checks steps: @@ -29,5 +27,36 @@ jobs: run: composer install --prefer-dist --no-interaction --no-suggest - name: Run PHP Code Sniffer run: ./vendor/bin/phpcs + + phpstan: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + php: + - "7.4" + - "8.0" + - "8.1" + - "8.2" + name: PHPStan on PHP ${{ matrix.php }} + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.composer/cache/files + key: dependencies-code-checks + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: none + tools: "cs2pr" + + - name: Install dependencies + run: composer install --prefer-dist --no-interaction --no-suggest - name: Run PHPStan - run: php vendor/bin/phpstan analyse --autoload-file tests/bootstrap.php + run: "vendor/bin/phpstan analyse --autoload-file tests/bootstrap.php --error-format=checkstyle | cs2pr" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index b1fe7a07..8121c416 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,10 +1,8 @@ +includes: + - phpstan/include-by-php-version.php + parameters: level: 8 paths: - src - tests - reportUnmatchedIgnoredErrors: false - ignoreErrors: - - - # REDIS stuff only runs with it available - '#Constant REDIS_HOST not found\.#' diff --git a/phpstan/include-by-php-version.php b/phpstan/include-by-php-version.php new file mode 100644 index 00000000..cdb2c194 --- /dev/null +++ b/phpstan/include-by-php-version.php @@ -0,0 +1,15 @@ += 70400) { + $includes[] = __DIR__ . '/php-74.neon'; +} + +$config = []; +$config['includes'] = $includes; +$config['parameters']['phpVersion'] = PHP_VERSION_ID; + +return $config; diff --git a/phpstan/php-74.neon b/phpstan/php-74.neon new file mode 100644 index 00000000..5a413315 --- /dev/null +++ b/phpstan/php-74.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Strict comparison using === between non\\-empty\\-array\\ and false will always evaluate to false\\.$#" + count: 1 + path: ../src/Prometheus/Storage/RedisNg.php diff --git a/src/Prometheus/RenderTextFormat.php b/src/Prometheus/RenderTextFormat.php index c285bb30..98ee73ea 100644 --- a/src/Prometheus/RenderTextFormat.php +++ b/src/Prometheus/RenderTextFormat.php @@ -67,7 +67,6 @@ private function escapeAllLabels(array $labelNames, Sample $sample): array $labels = array_combine(array_merge($labelNames, $sample->getLabelNames()), $sample->getLabelValues()); - /** @phpstan-ignore-next-line */ if ($labels === false) { throw new RuntimeException('Unable to combine labels.'); } diff --git a/src/Prometheus/Storage/RedisNg.php b/src/Prometheus/Storage/RedisNg.php index 990d0cf8..40061e2c 100644 --- a/src/Prometheus/Storage/RedisNg.php +++ b/src/Prometheus/Storage/RedisNg.php @@ -291,8 +291,7 @@ public function updateSummary(array $data): void if (false === $json) { throw new RuntimeException(json_last_error_msg()); } - $this->redis->setNx($metaKey, $json); - /** @phpstan-ignore-line */ + $this->redis->setnx($metaKey, $json); // store value key $valueKey = $summaryKey . ':' . $this->valueKey($data); @@ -301,8 +300,7 @@ public function updateSummary(array $data): void if (false === $json) { throw new RuntimeException(json_last_error_msg()); } - $this->redis->setNx($valueKey, $json); - /** @phpstan-ignore-line */ + $this->redis->setnx($valueKey, $json); // trick to handle uniqid collision $done = false; @@ -528,10 +526,15 @@ private function collectSummaries(): array } } if (count($samples) === 0) { - $this->redis->del($valueKey); + if (isset($valueKey)) { + $this->redis->del($valueKey); + } + continue; } + assert(isset($decodedLabelValues)); + // Compute quantiles sort($samples); foreach ($data['quantiles'] as $quantile) { @@ -560,11 +563,7 @@ private function collectSummaries(): array ]; - if (count($data['samples']) > 0) { - $summaries[] = $data; - } else { - $this->redis->del($metaKey); - } + $summaries[] = $data; } return $summaries; }