Skip to content

Commit bd2676b

Browse files
committed
Take advantage of PHP 8.0 constructor property promotion
1 parent 81bbe9a commit bd2676b

11 files changed

+12
-77
lines changed

src/Counter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616
*/
1717
final class Counter extends Metric
1818
{
19-
private CounterStorage $storage;
20-
21-
public function __construct(CounterStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
19+
public function __construct(private CounterStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
2220
{
2321
parent::__construct($name, $help, $labelNames ?? MetricLabelNames::fromNames());
24-
$this->storage = $storage;
2522
}
2623

2724
/**

src/Gauge.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313
*/
1414
final class Gauge extends Metric
1515
{
16-
private GaugeStorage $storage;
17-
18-
public function __construct(GaugeStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
16+
public function __construct(private GaugeStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
1917
{
2018
parent::__construct($name, $help, $labelNames ?? MetricLabelNames::fromNames());
21-
$this->storage = $storage;
2219
}
2320

2421
/**

src/Histogram.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,15 @@ final class Histogram extends Metric
3636
10.0,
3737
];
3838

39-
private HistogramStorage $storage;
40-
4139
/** @var float[] */
4240
private array $buckets;
4341

4442
/**
4543
* @param float[] $buckets
4644
*/
47-
public function __construct(HistogramStorage $storage, MetricName $name, string $help, ?HistogramLabelNames $labelNames = null, ?array $buckets = null)
45+
public function __construct(private HistogramStorage $storage, MetricName $name, string $help, ?HistogramLabelNames $labelNames = null, ?array $buckets = null)
4846
{
4947
parent::__construct($name, $help, $labelNames ?? HistogramLabelNames::fromNames());
50-
$this->storage = $storage;
5148

5249
if ($buckets === null) {
5350
$buckets = self::DEFAULT_BUCKETS;

src/Metric.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,11 @@
1717
*/
1818
abstract class Metric
1919
{
20-
/** @var MetricName */
21-
private $name;
22-
/** @var string */
23-
private $help;
24-
/**
25-
* @var LabelNames
26-
* @psalm-var TLabelNames
27-
* */
28-
private $labelNames;
29-
3020
/**
3121
* @psalm-param TLabelNames $labelNames
3222
*/
33-
public function __construct(MetricName $name, string $help, LabelNames $labelNames)
23+
public function __construct(private MetricName $name, private string $help, private LabelNames $labelNames)
3424
{
35-
$this->name = $name;
36-
$this->help = $help;
37-
$this->labelNames = $labelNames;
3825
}
3926

4027
/**

src/MetricFamilySamples.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,12 @@
1111
*/
1212
final class MetricFamilySamples
1313
{
14-
private string $name;
15-
private string $type;
16-
private string $help;
17-
/** @var string[] */
18-
private array $labelNames;
19-
/** @var Sample[] */
20-
private array $samples;
21-
2214
/**
2315
* @param string[] $labelNames
2416
* @param Sample[] $samples
2517
*/
26-
public function __construct(string $name, string $type, string $help, array $labelNames, array $samples)
18+
public function __construct(private string $name, private string $type, private string $help, private array $labelNames, private array $samples)
2719
{
28-
$this->name = $name;
29-
$this->type = $type;
30-
$this->help = $help;
31-
$this->labelNames = $labelNames;
32-
$this->samples = $samples;
3320
}
3421

3522
public function getName(): string

src/PushGateway/PSR18Pusher.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@
1818
final class PSR18Pusher implements Pusher
1919
{
2020
private string $address;
21-
private ClientInterface $client;
22-
private RequestFactoryInterface $requestFactory;
23-
private StreamFactoryInterface $streamFactory;
2421

25-
public function __construct(string $address, ClientInterface $client, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory)
22+
public function __construct(string $address, private ClientInterface $client, private RequestFactoryInterface $requestFactory, private StreamFactoryInterface $streamFactory)
2623
{
2724
if (strpos($address, '://') === false) {
2825
$address = 'http://' . $address;
2926
}
3027

31-
$this->address = $address . '/metrics/job/';
32-
$this->client = $client;
33-
$this->requestFactory = $requestFactory;
34-
$this->streamFactory = $streamFactory;
28+
$this->address = $address . '/metrics/job/';
3529
}
3630

3731
/**

src/PushGateway/PSR18UnexpectedPushGatewayResponse.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010

1111
final class PSR18UnexpectedPushGatewayResponse extends UnexpectedPushGatewayResponse
1212
{
13-
private RequestInterface $request;
14-
private ?ResponseInterface $response;
15-
16-
private function __construct(RequestInterface $request, ?ResponseInterface $response, ?ClientExceptionInterface $clientException)
13+
private function __construct(private RequestInterface $request, private ?ResponseInterface $response, ?ClientExceptionInterface $clientException)
1714
{
18-
$this->request = $request;
19-
$this->response = $response;
20-
2115
$exceptionCode = 0;
2216

2317
$message = 'Cannot connect PushGateway server to ' . $request->getUri()->__toString() . ':';

src/Renderer/IncoherentMetricLabelNamesAndValues.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
final class IncoherentMetricLabelNamesAndValues extends RuntimeException
1414
{
15-
private MetricFamilySamples $metric;
16-
17-
public function __construct(MetricFamilySamples $metric, int $nbLabelNames, int $nbLabelValues)
15+
public function __construct(private MetricFamilySamples $metric, int $nbLabelNames, int $nbLabelValues)
1816
{
1917
if ($nbLabelNames === $nbLabelValues) {
2018
throw new LogicException(

src/Sample.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,12 @@
1111
*/
1212
final class Sample
1313
{
14-
private string $name;
15-
/** @var string[] */
16-
private array $labelNames;
17-
/** @var string[] */
18-
private array $labelValues;
19-
private float $value;
20-
2114
/**
2215
* @param string[] $labelNames
2316
* @param string[] $labelValues
2417
*/
25-
public function __construct(string $name, float $value, array $labelNames, array $labelValues)
18+
public function __construct(private string $name, private float $value, private array $labelNames, private array $labelValues)
2619
{
27-
$this->name = $name;
28-
$this->labelNames = $labelNames;
29-
$this->labelValues = $labelValues;
30-
$this->value = $value;
3120
}
3221

3322
public function getName(): string

src/Value/HistogramLabelNames.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ final class HistogramLabelNames implements LabelNames
1616
{
1717
private const RESERVED_LABEL_HISTOGRAM = 'le';
1818

19-
private MetricLabelNames $names;
20-
21-
private function __construct(MetricLabelNames $names)
19+
private function __construct(private MetricLabelNames $names)
2220
{
23-
$this->names = $names;
2421
}
2522

2623
public static function fromNames(string ...$names): self

src/Value/MetricName.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ final class MetricName
1616
{
1717
private const METRIC_NAME_REGEX = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';
1818

19-
private string $name;
20-
21-
private function __construct(string $name)
19+
private function __construct(private string $name)
2220
{
2321
$this->name = $name;
2422
}

0 commit comments

Comments
 (0)