diff --git a/src/Counter.php b/src/Counter.php index 9c59841..5f25ba6 100644 --- a/src/Counter.php +++ b/src/Counter.php @@ -24,8 +24,8 @@ public function getType(): string { /** * Adds a value for this metric. * - * @param mixed $value - * The value. + * @param int<0, max> $value + * The value. Non-int values are deprecated. * @param array $labels * The list of key value label pairs. * @@ -33,6 +33,10 @@ public function getType(): string { * If the value is not a non-negative integer. */ public function set(mixed $value, array $labels = []): void { + // @phpstan-ignore function.alreadyNarrowedType + if (!is_int($value)) { + @trigger_error(sprintf('Passing a non-int 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/16', __METHOD__), E_USER_DEPRECATED); + } if (!$this->isValidValue($value)) { throw new \InvalidArgumentException("A count value must be a positive integer."); } diff --git a/tests/Unit/CounterTest.php b/tests/Unit/CounterTest.php index 405c762..d60cfac 100644 --- a/tests/Unit/CounterTest.php +++ b/tests/Unit/CounterTest.php @@ -5,6 +5,7 @@ namespace PNX\Prometheus\Tests\Unit; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\IgnoreDeprecations; use PHPUnit\Framework\TestCase; use PNX\Prometheus\Counter; @@ -18,18 +19,52 @@ class CounterTest extends TestCase { * Tests setting counter values. */ public function testCounter(): void { - $counter = new Counter('foo', 'bar', 'Example counter help'); $counter->set(89, ['baz' => 'wiz']); $this->assertEquals("foo_bar", $counter->getName()); $this->assertEquals("counter", $counter->getType()); + $values = $counter->getLabelledValues(); + $this->assertCount(1, $values); + $this->assertEquals(89, $values[0]->getValue()); + } + + /** + * Tests that a negative value throws an exception. + */ + public function testNegativeValueThrows(): void { + $counter = new Counter('foo', 'bar', 'Example counter help'); $this->expectException(\InvalidArgumentException::class); + // @phpstan-ignore argument.type $counter->set(-1, ['baz' => 'wiz']); + } + + /** + * Tests that a non-int value triggers a deprecation notice. + */ + public function testNonIntValueIsDeprecated(): void { + $counter = new Counter('foo', 'bar', 'Example counter help'); + $this->expectUserDeprecationMessage('Passing a non-int value to PNX\Prometheus\Counter::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/16'); + + try { + // @phpstan-ignore argument.type + $counter->set('bing', ['baz' => 'wiz']); + } + catch (\InvalidArgumentException) { + // The deprecation is triggered before the exception is thrown. + } + } + /** + * Tests that a non-int value still throws an exception. + */ + #[IgnoreDeprecations] + public function testNonIntValueThrows(): void { + $counter = new Counter('foo', 'bar', 'Example counter help'); $this->expectException(\InvalidArgumentException::class); - $counter->set('bing', ['baz' => 'wiz']); + // @phpstan-ignore argument.type + @$counter->set('bing', ['baz' => 'wiz']); } /**