fixing some psalm 5 complaints (#1110)

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.
This commit is contained in:
Brett McBride 2023-09-05 08:56:24 +10:00 committed by GitHub
parent 73ff5adcb8
commit 1852d514ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 44 additions and 75 deletions

View File

@ -34,6 +34,7 @@ $childSpan2 = $tracer->spanBuilder('bar')->startSpan();
$childSpan2->end();
$childSpan1->end();
$rootSpan->end();
$rootScope->detach();
/** @var SpanDataInterface $span */
foreach ($storage as $span) {

View File

@ -4,9 +4,7 @@ declare(strict_types=1);
namespace OpenTelemetry\API;
use InvalidArgumentException;
class Signals
interface Signals
{
/** @var string */
public const TRACE = 'trace';
@ -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);
}
}
}

View File

@ -7,26 +7,11 @@ namespace OpenTelemetry\API\Trace;
/**
* @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()
{
}
}

View File

@ -7,22 +7,9 @@ namespace OpenTelemetry\API\Trace;
/**
* @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()
{
}
}

View File

@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace OpenTelemetry\Context\Propagation;
use function array_key_first;
use function count;
final class TextMapPropagator
{
public static function composite(TextMapPropagatorInterface ...$propagators): TextMapPropagatorInterface
{
switch (count($propagators)) {
case 0:
return NoopTextMapPropagator::getInstance();
case 1:
/** @psalm-suppress PossiblyNullArrayOffset */
return $propagators[array_key_first($propagators)];
default:
return new MultiTextMapPropagator($propagators);
}
}
private function __construct()
{
}
}

View File

@ -11,6 +11,7 @@ use OpenTelemetry\SDK\Common\Export\TransportFactoryInterface;
class OtlpHttpTransportFactory implements TransportFactoryInterface
{
private const DEFAULT_COMPRESSION = 'none';
public function create(
string $endpoint,
string $contentType,

View File

@ -71,11 +71,6 @@ final class DependencyResolver implements DependencyResolverInterface
return $this->messageFactoryResolver->resolveUriFactory();
}
public function resolveHttpClient(): ClientInterface
{
return $this->psrClientResolver->resolvePsrClient();
}
public function resolveHttpPlugAsyncClient(): HttpAsyncClient
{
return $this->httpPlugClientResolver->resolveHttpPlugAsyncClient();

View File

@ -57,7 +57,7 @@ final class PsrTransportFactory implements TransportFactoryInterface
$endpoint,
$contentType,
$headers,
(array) $compression,
PsrUtils::compression($compression),
$retryDelay,
$maxRetries,
);

View File

@ -104,6 +104,24 @@ final class PsrUtils
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;

View File

@ -116,7 +116,7 @@ class ScopeTest extends TestCase
$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']);

View File

@ -91,4 +91,23 @@ final class PsrUtilsTest extends TestCase
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']],
];
}
}