Skip to content

Commit 08eda80

Browse files
authored
feat: deprecate non-int/float values in Gauge::set() (#15)
* 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 * 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.
1 parent 1ec205d commit 08eda80

4 files changed

Lines changed: 69 additions & 3 deletions

File tree

phpcs.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@
44
<file>./src</file>
55
<file>./tests</file>
66
<arg name="extensions" value="php"/>
7-
<rule ref="Drupal"/>
7+
<rule ref="Drupal">
8+
<!-- Not a Drupal project: deprecation "See" links point to GitHub issues,
9+
not drupal.org change records. -->
10+
<exclude name="Drupal.Semantics.FunctionTriggerError.TriggerErrorSeeUrlFormat"/>
11+
</rule>
812
</ruleset>

src/Gauge.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ public function getType(): string {
2525
* Adds a value for this metric.
2626
*
2727
* @param mixed $value
28-
* The value.
28+
* The value. Non-int/float values are deprecated.
2929
* @param array<string, string|int|float> $labels
3030
* The list of key value label pairs.
3131
*/
3232
public function set(mixed $value, array $labels = []): void {
33+
if (!is_int($value) && !is_float($value)) {
34+
@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);
35+
}
3336
$key = $this->getKey($labels);
3437
$this->labelledValues[$key] = new LabelledValue($this->getName(), $value, $labels);
3538
}

src/Serializer/PrometheusEncoder.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public function supportsEncoding(string $format): bool {
2323
/**
2424
* {@inheritdoc}
2525
*
26-
* @phpstan-ignore missingType.iterableValue
26+
* @param mixed $data
27+
* The data to encode.
28+
* @param string $format
29+
* The encoding format.
30+
* @param array<string, mixed> $context
31+
* The serializer context.
2732
*/
2833
public function encode(mixed $data, string $format, array $context = []): string {
2934
$output = [];

tests/Unit/GaugeTest.php

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

77
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
810
use PHPUnit\Framework\TestCase;
911
use PNX\Prometheus\Gauge;
1012

@@ -45,4 +47,56 @@ public function testGaugeNoValues(): void {
4547
$this->assertEmpty($gauge->getLabelledValues());
4648
}
4749

50+
/**
51+
* Tests that int and float values are accepted without a deprecation notice.
52+
*/
53+
#[DataProvider('numericValueProvider')]
54+
public function testGaugeAcceptsNumericValue(int|float $value): void {
55+
$gauge = new Gauge("foo", "bar", "A test gauge");
56+
$gauge->set($value);
57+
58+
$values = $gauge->getLabelledValues();
59+
$this->assertCount(1, $values);
60+
$this->assertEquals($value, $values[0]->getValue());
61+
}
62+
63+
/**
64+
* Provides int and float values accepted by Gauge::set().
65+
*
66+
* @return array<string, array{int|float}>
67+
* The numeric values to test.
68+
*/
69+
public static function numericValueProvider(): array {
70+
return [
71+
'integer' => [100],
72+
'zero' => [0],
73+
'negative integer' => [-5],
74+
'float' => [1.5],
75+
'negative float' => [-2.75],
76+
];
77+
}
78+
79+
/**
80+
* Tests that a non-int/float value triggers a deprecation notice.
81+
*/
82+
public function testGaugeNonNumericValueIsDeprecated(): void {
83+
$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');
84+
85+
$gauge = new Gauge("foo", "bar", "A test gauge");
86+
$gauge->set("not-a-number");
87+
}
88+
89+
/**
90+
* Tests that a deprecated value is still stored.
91+
*/
92+
#[IgnoreDeprecations]
93+
public function testGaugeNonNumericValueIsStored(): void {
94+
$gauge = new Gauge("foo", "bar", "A test gauge");
95+
@$gauge->set("not-a-number");
96+
97+
$values = $gauge->getLabelledValues();
98+
$this->assertCount(1, $values);
99+
$this->assertEquals("not-a-number", $values[0]->getValue());
100+
}
101+
48102
}

0 commit comments

Comments
 (0)