Skip to content

Commit

Permalink
fixing some psalm 5 complaints (#1110)
Browse files Browse the repository at this point in the history
Whilst investigating upgrading to psalm 5, I notice that it generates a lot of new complaints. This fixes the ones that looked legit and
require an API change, or were trivial. Does not upgrade to psalm 5 yet, though. That's a bigger job for another day.
  • Loading branch information
brettmc authored Sep 4, 2023
1 parent 73ff5ad commit 1852d51
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 75 deletions.
1 change: 1 addition & 0 deletions examples/traces/exporters/in_memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
$childSpan2->end();
$childSpan1->end();
$rootSpan->end();
$rootScope->detach();

/** @var SpanDataInterface $span */
foreach ($storage as $span) {
Expand Down
11 changes: 1 addition & 10 deletions src/API/Signals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace OpenTelemetry\API;

use InvalidArgumentException;

class Signals
interface Signals
{
/** @var string */
public const TRACE = 'trace';
Expand All @@ -20,11 +18,4 @@ class Signals
self::METRICS,
self::LOGS,
];

public static function validate(string $signal): void
{
if (!in_array($signal, self::SIGNALS)) {
throw new InvalidArgumentException('Unknown signal: ' . $signal);
}
}
}
17 changes: 1 addition & 16 deletions src/API/Trace/SpanKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,11 @@
/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/trace/api.md#spankind
*/
final class SpanKind
interface SpanKind
{
public const KIND_INTERNAL = 0;
public const KIND_CLIENT = 1;
public const KIND_SERVER = 2;
public const KIND_PRODUCER = 3;
public const KIND_CONSUMER = 4;

public static function getChoices(): array
{
return [
self::KIND_INTERNAL,
self::KIND_CLIENT,
self::KIND_SERVER,
self::KIND_PRODUCER,
self::KIND_CONSUMER,
];
}

private function __construct()
{
}
}
15 changes: 1 addition & 14 deletions src/API/Trace/StatusCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,9 @@
/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.6.1/specification/trace/api.md#set-status
*/
final class StatusCode
interface StatusCode
{
public const STATUS_UNSET = 'Unset';
public const STATUS_OK = 'Ok';
public const STATUS_ERROR = 'Error';

public function getChoices(): array
{
return [
self::STATUS_UNSET,
self::STATUS_OK,
self::STATUS_ERROR,
];
}

private function __construct()
{
}
}
28 changes: 0 additions & 28 deletions src/Context/Propagation/TextMapPropagator.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Contrib/Otlp/OtlpHttpTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class OtlpHttpTransportFactory implements TransportFactoryInterface
{
private const DEFAULT_COMPRESSION = 'none';

public function create(
string $endpoint,
string $contentType,
Expand Down
5 changes: 0 additions & 5 deletions src/SDK/Common/Adapter/HttpDiscovery/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public function resolveUriFactory(): UriFactoryInterface
return $this->messageFactoryResolver->resolveUriFactory();
}

public function resolveHttpClient(): ClientInterface
{
return $this->psrClientResolver->resolvePsrClient();
}

public function resolveHttpPlugAsyncClient(): HttpAsyncClient
{
return $this->httpPlugClientResolver->resolveHttpPlugAsyncClient();
Expand Down
2 changes: 1 addition & 1 deletion src/SDK/Common/Export/Http/PsrTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function create(
$endpoint,
$contentType,
$headers,
(array) $compression,
PsrUtils::compression($compression),
$retryDelay,
$maxRetries,
);
Expand Down
18 changes: 18 additions & 0 deletions src/SDK/Common/Export/Http/PsrUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ public static function decode(string $value, array $encodings): string
return $value;
}

/**
* Resolve an array or CSV of compression types to a list
*/
public static function compression($compression): array
{
if (is_array($compression)) {
return $compression;
}
if (!$compression) {
return [];
}
if (strpos($compression, ',') === false) {
return [$compression];
}

return array_map('trim', explode(',', $compression));
}

private static function encoder(string $encoding): ?callable
{
static $encoders;
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Context/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function test_scope_local_storage_is_preserved_between_attach_and_scope()
$scope['key'] = 'value';
$scope = $storage->scope();
$this->assertNotNull($scope);
$this->assertArrayHasKey('key', $scope); /** @phpstan-ignore-line */
$this->assertArrayHasKey('key', $scope);
$this->assertSame('value', $scope['key']);

unset($scope['key']);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/SDK/Common/Export/Http/PsrUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ public function test_decode_stream_unknown_encoding(): void

PsrUtils::decode('', ['invalid']);
}

/**
* @dataProvider compressionProvider
*/
public function test_resolve_compression($input, $expected): void
{
$this->assertSame($expected, PsrUtils::compression($input));
}

public static function compressionProvider(): array
{
return [
['gzip', ['gzip']],
['', []],
['gzip,br', ['gzip','br']],
['gzip , brotli', ['gzip','brotli']],
[['gzip'], ['gzip']],
];
}
}

0 comments on commit 1852d51

Please sign in to comment.