Skip to content

Commit 5348251

Browse files
authored
feat: deprecate non-int values in Counter::set() (#17)
* feat: deprecate non-int values in Counter::set() Passing a non-int value to Counter::set() now triggers an E_USER_DEPRECATED notice before the existing InvalidArgumentException is thrown. The parameter type will be narrowed to int in the next major release. Documents the value as int<0, max>. Closes #16 * refactor: remove redundant inline phpcs ignore in Counter::set() The TriggerErrorSeeUrlFormat sniff is now excluded globally in phpcs.xml, so the inline phpcs:ignore is no longer needed.
1 parent 08eda80 commit 5348251

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

src/Counter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@ public function getType(): string {
2424
/**
2525
* Adds a value for this metric.
2626
*
27-
* @param mixed $value
28-
* The value.
27+
* @param int<0, max> $value
28+
* The value. Non-int values are deprecated.
2929
* @param array<string, string|int|float> $labels
3030
* The list of key value label pairs.
3131
*
3232
* @throws \InvalidArgumentException
3333
* If the value is not a non-negative integer.
3434
*/
3535
public function set(mixed $value, array $labels = []): void {
36+
// @phpstan-ignore function.alreadyNarrowedType
37+
if (!is_int($value)) {
38+
@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);
39+
}
3640
if (!$this->isValidValue($value)) {
3741
throw new \InvalidArgumentException("A count value must be a positive integer.");
3842
}

tests/Unit/CounterTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PNX\Prometheus\Tests\Unit;
66

77
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
89
use PHPUnit\Framework\TestCase;
910
use PNX\Prometheus\Counter;
1011

@@ -18,18 +19,52 @@ class CounterTest extends TestCase {
1819
* Tests setting counter values.
1920
*/
2021
public function testCounter(): void {
21-
2222
$counter = new Counter('foo', 'bar', 'Example counter help');
2323
$counter->set(89, ['baz' => 'wiz']);
2424

2525
$this->assertEquals("foo_bar", $counter->getName());
2626
$this->assertEquals("counter", $counter->getType());
2727

28+
$values = $counter->getLabelledValues();
29+
$this->assertCount(1, $values);
30+
$this->assertEquals(89, $values[0]->getValue());
31+
}
32+
33+
/**
34+
* Tests that a negative value throws an exception.
35+
*/
36+
public function testNegativeValueThrows(): void {
37+
$counter = new Counter('foo', 'bar', 'Example counter help');
2838
$this->expectException(\InvalidArgumentException::class);
39+
// @phpstan-ignore argument.type
2940
$counter->set(-1, ['baz' => 'wiz']);
41+
}
42+
43+
/**
44+
* Tests that a non-int value triggers a deprecation notice.
45+
*/
46+
public function testNonIntValueIsDeprecated(): void {
47+
$counter = new Counter('foo', 'bar', 'Example counter help');
48+
$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');
49+
50+
try {
51+
// @phpstan-ignore argument.type
52+
$counter->set('bing', ['baz' => 'wiz']);
53+
}
54+
catch (\InvalidArgumentException) {
55+
// The deprecation is triggered before the exception is thrown.
56+
}
57+
}
3058

59+
/**
60+
* Tests that a non-int value still throws an exception.
61+
*/
62+
#[IgnoreDeprecations]
63+
public function testNonIntValueThrows(): void {
64+
$counter = new Counter('foo', 'bar', 'Example counter help');
3165
$this->expectException(\InvalidArgumentException::class);
32-
$counter->set('bing', ['baz' => 'wiz']);
66+
// @phpstan-ignore argument.type
67+
@$counter->set('bing', ['baz' => 'wiz']);
3368
}
3469

3570
/**

0 commit comments

Comments
 (0)