Skip to content

Commit

Permalink
chore: upgrade phpstan + fix phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GCalmels committed Jun 12, 2024
1 parent 7d27686 commit b1bc554
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
"require-dev": {
"doctrine/dbal": "^3.0",
"friends-of-phpspec/phpspec-expect": "^4.0",
"open-telemetry/transport-grpc": "^1.0",
"open-telemetry/gen-otlp-protobuf": "^1.0",
"php-http/httplug": "^2.3",
"phpspec/phpspec": "^7.5",
"phpstan/phpstan": "<1.11",
"phpstan/phpstan": "^1.4",
"symfony/framework-bundle": "*",
"symfony/http-client": "*",
"symfony/messenger": "*",
Expand Down
40 changes: 21 additions & 19 deletions src/Bridge/GoogleCloud/Logging/Formatter/StdOutFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,32 @@ protected function normalize(mixed $data, int $depth = 0): mixed
$data['severity'] = $data['level_name'];
unset($data['level_name']);

// Map tracing
if (isset($data['context']['trace'])) {
$data['logging.googleapis.com/trace'] = 'projects/'.$this->project.'/traces/'.$data['context']['trace'];
}
if (isset($data['context']['span'])) {
$data['logging.googleapis.com/spanId'] = $data['context']['span'];
}
if (isset($data['context']['sampled'])) {
$data['logging.googleapis.com/trace_sampled'] = $data['context']['sampled'];
}
if (isset($data['context']['operation'])) {
$data['logging.googleapis.com/operation'] = $data['context']['operation'];
if (\is_array($data['context'])) {
// Map tracing
if (isset($data['context']['trace'])) {
$data['logging.googleapis.com/trace'] = 'projects/'.$this->project.'/traces/'.$data['context']['trace'];
}
if (isset($data['context']['span'])) {
$data['logging.googleapis.com/spanId'] = $data['context']['span'];
}
if (isset($data['context']['sampled'])) {
$data['logging.googleapis.com/trace_sampled'] = $data['context']['sampled'];
}
if (isset($data['context']['operation'])) {
$data['logging.googleapis.com/operation'] = $data['context']['operation'];
}
unset($data['context']['trace'], $data['context']['span'], $data['context']['sampled'], $data['context']['operation']);

if ($exception = $data['context']['exception'] ?? false) {
$data['message'] = $exception['message'];
$data['context'] = array_merge($data['context'], $exception['context']);
unset($data['context']['exception']);
}
}
unset($data['context']['trace'], $data['context']['span'], $data['context']['sampled'], $data['context']['operation']);

// Map channel
$data['logging.googleapis.com/labels'] = ['channel' => $data['channel']];
unset($data['channel']);

if ($exception = $data['context']['exception'] ?? false) {
$data['message'] = $exception['message'];
$data['context'] = array_merge($data['context'], $exception['context']);
unset($data['context']['exception']);
}
}

return $data;
Expand Down
7 changes: 5 additions & 2 deletions src/Metrics/CounterAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class CounterAdapter implements CounterInterface
{
use IterableAttributesTrait;

public function __construct(
private string $name,
private string $description,
Expand All @@ -20,11 +22,12 @@ public function __construct(
}

/**
* @param int $amount
* @param array{labels: array<string,mixed>} $attributes
* @param int $amount
* @param iterable<string,array<string,mixed>> $attributes
*/
public function add($amount, iterable $attributes = [], $context = null): void
{
$attributes = $this->normalizeAttributes($attributes);
/** @var array<string> $labelNames */
$labelNames = array_keys($attributes['labels'] ?? []);
$labelValues = array_values($attributes['labels'] ?? []);
Expand Down
3 changes: 3 additions & 0 deletions src/Metrics/HistogramAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class HistogramAdapter implements HistogramInterface
{
use IterableAttributesTrait;

public function __construct(
private string $name,
private string $description,
Expand All @@ -25,6 +27,7 @@ public function __construct(
*/
public function record($amount, iterable $attributes = [], $context = null): void
{
$attributes = $this->normalizeAttributes($attributes);
/** @var array<string> $labelNames */
$labelNames = array_keys($attributes['labels'] ?? []);
$labelValues = array_values($attributes['labels'] ?? []);
Expand Down
26 changes: 26 additions & 0 deletions src/Metrics/IterableAttributesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the worldia/instrumentation-bundle package.
* (c) Worldia <[email protected]>
*/

namespace Instrumentation\Metrics;

trait IterableAttributesTrait
{
/**
* @param iterable<string,array<string,mixed>> $attributes
*
* @return array<string,array<string,mixed>>
*/
public function normalizeAttributes(iterable $attributes): array
{
$newAttr = [];
foreach ($attributes as $key => $value) {
$newAttr[$key] = $value;
}

return $newAttr;
}
}
3 changes: 3 additions & 0 deletions src/Metrics/UpDownCounterAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class UpDownCounterAdapter implements UpDownCounterInterface
{
use IterableAttributesTrait;

public function __construct(
private string $name,
private string $description,
Expand All @@ -25,6 +27,7 @@ public function __construct(
*/
public function add($amount, iterable $attributes = [], $context = null): void
{
$attributes = $this->normalizeAttributes($attributes);
/** @var array<string> $labelNames */
$labelNames = array_keys($attributes['labels'] ?? []);
$labelValues = array_values($attributes['labels'] ?? []);
Expand Down

0 comments on commit b1bc554

Please sign in to comment.