Skip to content

Commit

Permalink
Take advantage of PHP 8.0 constructor property promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSuisse committed Nov 24, 2021
1 parent 81bbe9a commit bd2676b
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 77 deletions.
5 changes: 1 addition & 4 deletions src/Counter.php
Expand Up @@ -16,12 +16,9 @@
*/
final class Counter extends Metric
{
private CounterStorage $storage;

public function __construct(CounterStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
public function __construct(private CounterStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
{
parent::__construct($name, $help, $labelNames ?? MetricLabelNames::fromNames());
$this->storage = $storage;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Gauge.php
Expand Up @@ -13,12 +13,9 @@
*/
final class Gauge extends Metric
{
private GaugeStorage $storage;

public function __construct(GaugeStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
public function __construct(private GaugeStorage $storage, MetricName $name, string $help, ?MetricLabelNames $labelNames = null)
{
parent::__construct($name, $help, $labelNames ?? MetricLabelNames::fromNames());
$this->storage = $storage;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Histogram.php
Expand Up @@ -36,18 +36,15 @@ final class Histogram extends Metric
10.0,
];

private HistogramStorage $storage;

/** @var float[] */
private array $buckets;

/**
* @param float[] $buckets
*/
public function __construct(HistogramStorage $storage, MetricName $name, string $help, ?HistogramLabelNames $labelNames = null, ?array $buckets = null)
public function __construct(private HistogramStorage $storage, MetricName $name, string $help, ?HistogramLabelNames $labelNames = null, ?array $buckets = null)
{
parent::__construct($name, $help, $labelNames ?? HistogramLabelNames::fromNames());
$this->storage = $storage;

if ($buckets === null) {
$buckets = self::DEFAULT_BUCKETS;
Expand Down
15 changes: 1 addition & 14 deletions src/Metric.php
Expand Up @@ -17,24 +17,11 @@
*/
abstract class Metric
{
/** @var MetricName */
private $name;
/** @var string */
private $help;
/**
* @var LabelNames
* @psalm-var TLabelNames
* */
private $labelNames;

/**
* @psalm-param TLabelNames $labelNames
*/
public function __construct(MetricName $name, string $help, LabelNames $labelNames)
public function __construct(private MetricName $name, private string $help, private LabelNames $labelNames)
{
$this->name = $name;
$this->help = $help;
$this->labelNames = $labelNames;
}

/**
Expand Down
15 changes: 1 addition & 14 deletions src/MetricFamilySamples.php
Expand Up @@ -11,25 +11,12 @@
*/
final class MetricFamilySamples
{
private string $name;
private string $type;
private string $help;
/** @var string[] */
private array $labelNames;
/** @var Sample[] */
private array $samples;

/**
* @param string[] $labelNames
* @param Sample[] $samples
*/
public function __construct(string $name, string $type, string $help, array $labelNames, array $samples)
public function __construct(private string $name, private string $type, private string $help, private array $labelNames, private array $samples)
{
$this->name = $name;
$this->type = $type;
$this->help = $help;
$this->labelNames = $labelNames;
$this->samples = $samples;
}

public function getName(): string
Expand Down
10 changes: 2 additions & 8 deletions src/PushGateway/PSR18Pusher.php
Expand Up @@ -18,20 +18,14 @@
final class PSR18Pusher implements Pusher
{
private string $address;
private ClientInterface $client;
private RequestFactoryInterface $requestFactory;
private StreamFactoryInterface $streamFactory;

public function __construct(string $address, ClientInterface $client, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory)
public function __construct(string $address, private ClientInterface $client, private RequestFactoryInterface $requestFactory, private StreamFactoryInterface $streamFactory)
{
if (strpos($address, '://') === false) {
$address = 'http://' . $address;
}

$this->address = $address . '/metrics/job/';
$this->client = $client;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->address = $address . '/metrics/job/';
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/PushGateway/PSR18UnexpectedPushGatewayResponse.php
Expand Up @@ -10,14 +10,8 @@

final class PSR18UnexpectedPushGatewayResponse extends UnexpectedPushGatewayResponse
{
private RequestInterface $request;
private ?ResponseInterface $response;

private function __construct(RequestInterface $request, ?ResponseInterface $response, ?ClientExceptionInterface $clientException)
private function __construct(private RequestInterface $request, private ?ResponseInterface $response, ?ClientExceptionInterface $clientException)
{
$this->request = $request;
$this->response = $response;

$exceptionCode = 0;

$message = 'Cannot connect PushGateway server to ' . $request->getUri()->__toString() . ':';
Expand Down
4 changes: 1 addition & 3 deletions src/Renderer/IncoherentMetricLabelNamesAndValues.php
Expand Up @@ -12,9 +12,7 @@

final class IncoherentMetricLabelNamesAndValues extends RuntimeException
{
private MetricFamilySamples $metric;

public function __construct(MetricFamilySamples $metric, int $nbLabelNames, int $nbLabelValues)
public function __construct(private MetricFamilySamples $metric, int $nbLabelNames, int $nbLabelValues)
{
if ($nbLabelNames === $nbLabelValues) {
throw new LogicException(
Expand Down
13 changes: 1 addition & 12 deletions src/Sample.php
Expand Up @@ -11,23 +11,12 @@
*/
final class Sample
{
private string $name;
/** @var string[] */
private array $labelNames;
/** @var string[] */
private array $labelValues;
private float $value;

/**
* @param string[] $labelNames
* @param string[] $labelValues
*/
public function __construct(string $name, float $value, array $labelNames, array $labelValues)
public function __construct(private string $name, private float $value, private array $labelNames, private array $labelValues)
{
$this->name = $name;
$this->labelNames = $labelNames;
$this->labelValues = $labelValues;
$this->value = $value;
}

public function getName(): string
Expand Down
5 changes: 1 addition & 4 deletions src/Value/HistogramLabelNames.php
Expand Up @@ -16,11 +16,8 @@ final class HistogramLabelNames implements LabelNames
{
private const RESERVED_LABEL_HISTOGRAM = 'le';

private MetricLabelNames $names;

private function __construct(MetricLabelNames $names)
private function __construct(private MetricLabelNames $names)
{
$this->names = $names;
}

public static function fromNames(string ...$names): self
Expand Down
4 changes: 1 addition & 3 deletions src/Value/MetricName.php
Expand Up @@ -16,9 +16,7 @@ final class MetricName
{
private const METRIC_NAME_REGEX = '/^[a-zA-Z_:][a-zA-Z0-9_:]*$/';

private string $name;

private function __construct(string $name)
private function __construct(private string $name)
{
$this->name = $name;
}
Expand Down

0 comments on commit bd2676b

Please sign in to comment.