From 91c4079d04c92e95df17956c59ea8b5d8f826966 Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Fri, 10 Jul 2026 09:08:56 +1000 Subject: [PATCH 1/2] feat: deprecate non-int/float values in Gauge::set() Passing a value that is not an int or float to Gauge::set() now triggers an E_USER_DEPRECATED notice. The value is still stored, so behaviour is unchanged. The parameter type will be narrowed to int|float in the next major release. Also documents the encode() parameters in PrometheusEncoder so the missing iterable value-type ignore is no longer needed. Closes #14 --- src/Gauge.php | 6 +++- src/Serializer/PrometheusEncoder.php | 7 +++- tests/Unit/GaugeTest.php | 54 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/Gauge.php b/src/Gauge.php index fc688e6..4f5bf37 100644 --- a/src/Gauge.php +++ b/src/Gauge.php @@ -25,11 +25,15 @@ public function getType(): string { * Adds a value for this metric. * * @param mixed $value - * The value. + * The value. Non-int/float values are deprecated. * @param array $labels * The list of key value label pairs. */ public function set(mixed $value, array $labels = []): void { + if (!is_int($value) && !is_float($value)) { + // phpcs:ignore Drupal.Semantics.FunctionTriggerError.TriggerErrorSeeUrlFormat + @trigger_error(sprintf('Passing a non-int/float value to %s() is deprecated in php_prometheus:1.1.0 and will throw a \TypeError in php_prometheus:2.0.0. See https://github.com/previousnext/php-prometheus/issues/14', __METHOD__), E_USER_DEPRECATED); + } $key = $this->getKey($labels); $this->labelledValues[$key] = new LabelledValue($this->getName(), $value, $labels); } diff --git a/src/Serializer/PrometheusEncoder.php b/src/Serializer/PrometheusEncoder.php index d404a46..2106816 100644 --- a/src/Serializer/PrometheusEncoder.php +++ b/src/Serializer/PrometheusEncoder.php @@ -23,7 +23,12 @@ public function supportsEncoding(string $format): bool { /** * {@inheritdoc} * - * @phpstan-ignore missingType.iterableValue + * @param mixed $data + * The data to encode. + * @param string $format + * The encoding format. + * @param array $context + * The serializer context. */ public function encode(mixed $data, string $format, array $context = []): string { $output = []; diff --git a/tests/Unit/GaugeTest.php b/tests/Unit/GaugeTest.php index 394761a..1d230f9 100644 --- a/tests/Unit/GaugeTest.php +++ b/tests/Unit/GaugeTest.php @@ -5,6 +5,8 @@ namespace PNX\Prometheus\Tests\Unit; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\TestCase; use PNX\Prometheus\Gauge; @@ -45,4 +47,56 @@ public function testGaugeNoValues(): void { $this->assertEmpty($gauge->getLabelledValues()); } + /** + * Tests that int and float values are accepted without a deprecation notice. + */ + #[DataProvider('numericValueProvider')] + public function testGaugeAcceptsNumericValue(int|float $value): void { + $gauge = new Gauge("foo", "bar", "A test gauge"); + $gauge->set($value); + + $values = $gauge->getLabelledValues(); + $this->assertCount(1, $values); + $this->assertEquals($value, $values[0]->getValue()); + } + + /** + * Provides int and float values accepted by Gauge::set(). + * + * @return array + * The numeric values to test. + */ + public static function numericValueProvider(): array { + return [ + 'integer' => [100], + 'zero' => [0], + 'negative integer' => [-5], + 'float' => [1.5], + 'negative float' => [-2.75], + ]; + } + + /** + * Tests that a non-int/float value triggers a deprecation notice. + */ + public function testGaugeNonNumericValueIsDeprecated(): void { + $this->expectUserDeprecationMessage('Passing a non-int/float value to PNX\Prometheus\Gauge::set() is deprecated in php_prometheus:1.1.0 and will throw a \TypeError in php_prometheus:2.0.0. See https://github.com/previousnext/php-prometheus/issues/14'); + + $gauge = new Gauge("foo", "bar", "A test gauge"); + $gauge->set("not-a-number"); + } + + /** + * Tests that a deprecated value is still stored. + */ + #[IgnoreDeprecations] + public function testGaugeNonNumericValueIsStored(): void { + $gauge = new Gauge("foo", "bar", "A test gauge"); + @$gauge->set("not-a-number"); + + $values = $gauge->getLabelledValues(); + $this->assertCount(1, $values); + $this->assertEquals("not-a-number", $values[0]->getValue()); + } + } From eee9cb0df72bae38766789abf4321282fcb4c2eb Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Fri, 10 Jul 2026 11:46:20 +1000 Subject: [PATCH 2/2] refactor: exclude Drupal See-url sniff in phpcs.xml The TriggerErrorSeeUrlFormat sniff requires drupal.org URLs, which does not apply to this GitHub project. Exclude it globally instead of using an inline phpcs:ignore. --- phpcs.xml | 6 +++++- src/Gauge.php | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index c0657de..6bd8a2b 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,5 +4,9 @@ ./src ./tests - + + + + diff --git a/src/Gauge.php b/src/Gauge.php index 4f5bf37..45cf292 100644 --- a/src/Gauge.php +++ b/src/Gauge.php @@ -31,7 +31,6 @@ public function getType(): string { */ public function set(mixed $value, array $labels = []): void { if (!is_int($value) && !is_float($value)) { - // phpcs:ignore Drupal.Semantics.FunctionTriggerError.TriggerErrorSeeUrlFormat @trigger_error(sprintf('Passing a non-int/float value to %s() is deprecated in php_prometheus:1.1.0 and will throw a \TypeError in php_prometheus:2.0.0. See https://github.com/previousnext/php-prometheus/issues/14', __METHOD__), E_USER_DEPRECATED); } $key = $this->getKey($labels);