Skip to content

Commit

Permalink
Merge pull request #1207: Apply Spiral Code Style
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Jan 24, 2025
1 parent 69f74c6 commit 122a44f
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 126 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
],
"require": {
"php": ">=8.1",
"spiral/boot": "^3.14.10",
"spiral/core": "^3.14.10"
"spiral/boot": "^3.15",
"spiral/core": "^3.15"
},
"require-dev": {
"phpunit/phpunit": "^10.1",
Expand Down
45 changes: 36 additions & 9 deletions src/AbstractTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Spiral\Telemetry;

use Spiral\Core\Attribute\Proxy;
use Spiral\Core\BinderInterface;
use Spiral\Core\Container;
use Spiral\Core\ContainerScope;
use Spiral\Core\InvokerInterface;
use Spiral\Core\ScopeInterface;

Expand All @@ -17,19 +18,45 @@
abstract class AbstractTracer implements TracerInterface
{
public function __construct(
#[Proxy] private readonly ?ScopeInterface $scope = new Container(),
) {
}
private readonly ?ScopeInterface $scope = new Container(),
) {}

/**
* @throws \Throwable
*/
final protected function runScope(SpanInterface $span, callable $callback): mixed
{
// TODO: Can we remove this scope?
return $this->scope->runScope([
SpanInterface::class => $span,
TracerInterface::class => $this,
], static fn (InvokerInterface $invoker): mixed => $invoker->invoke($callback));
$container = ContainerScope::getContainer();
if ($container === null) {
return $this->scope->runScope([
SpanInterface::class => $span,
TracerInterface::class => $this,
], static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback));
}

if ($container instanceof Container) {
$invoker = $container;
$binder = $container;
} else {
/** @var InvokerInterface $invoker */
$invoker = $container->get(InvokerInterface::class);
/** @var BinderInterface $binder */
$binder = $container->get(BinderInterface::class);
}

try {
$prevSpan = $container->get(SpanInterface::class);
} catch (\Throwable) {
$prevSpan = null;
}

$binder->bindSingleton(SpanInterface::class, $span);
try {
return $invoker->invoke($callback);
} finally {
$prevSpan === null
? $binder->removeBinding(SpanInterface::class)
: $binder->bindSingleton(SpanInterface::class, $prevSpan);
}
}
}
18 changes: 7 additions & 11 deletions src/Bootloader/TelemetryBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
use Spiral\Telemetry\Config\TelemetryConfig;
use Spiral\Telemetry\ConfigTracerFactoryProvider;
use Spiral\Telemetry\Exception\TracerException;
use Spiral\Telemetry\LogTracer;
use Spiral\Telemetry\LogTracerFactory;
use Spiral\Telemetry\NullTracer;
use Spiral\Telemetry\NullTracerFactory;
use Spiral\Telemetry\TracerFactoryInterface;
use Spiral\Telemetry\TracerInterface;
use Spiral\Telemetry\TracerFactoryProviderInterface;
use Spiral\Telemetry\TracerInterface;

final class TelemetryBootloader extends Bootloader
{
Expand All @@ -29,15 +27,13 @@ final class TelemetryBootloader extends Bootloader
TracerFactoryProviderInterface::class => ConfigTracerFactoryProvider::class,
ClockInterface::class => SystemClock::class,
];

protected const BINDINGS = [
TracerInterface::class => [self::class, 'getTracer'],
];

public function __construct(
private readonly ConfiguratorInterface $config
) {
}
private readonly ConfiguratorInterface $config,
) {}

public function init(EnvironmentInterface $env): void
{
Expand All @@ -51,15 +47,15 @@ public function registerTracer(string $name, string|TracerFactoryInterface|Autow
{
$this->config->modify(
TelemetryConfig::CONFIG,
new Append('drivers', $name, $driver)
new Append('drivers', $name, $driver),
);
}

/**
* @throws TracerException
*/
public function initFactory(
TracerFactoryProviderInterface $tracerProvider
TracerFactoryProviderInterface $tracerProvider,
): TracerFactoryInterface {
return $tracerProvider->getTracerFactory();
}
Expand All @@ -68,7 +64,7 @@ public function initFactory(
* @throws TracerException
*/
public function getTracer(
TracerFactoryInterface $tracerFactory
TracerFactoryInterface $tracerFactory,
): TracerInterface {
return $tracerFactory->make();
}
Expand All @@ -83,7 +79,7 @@ private function initConfig(EnvironmentInterface $env): void
'null' => NullTracerFactory::class,
'log' => LogTracerFactory::class,
],
]
],
);
}
}
4 changes: 2 additions & 2 deletions src/Config/TelemetryConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getDriverConfig(string $name): string|Autowire|TracerFactoryInte
{
if (!isset($this->config['drivers'][$name])) {
throw new InvalidArgumentException(
\sprintf('Config for telemetry driver `%s` is not defined.', $name)
\sprintf('Config for telemetry driver `%s` is not defined.', $name),
);
}

Expand All @@ -53,7 +53,7 @@ public function getDriverConfig(string $name): string|Autowire|TracerFactoryInte

if (!\is_string($driver) && !$driver instanceof Autowire) {
throw new InvalidArgumentException(
\sprintf('Trace type value for `%s` must be a string or %s', $name, Autowire::class)
\sprintf('Trace type value for `%s` must be a string or %s', $name, Autowire::class),
);
}

Expand Down
5 changes: 2 additions & 3 deletions src/ConfigTracerFactoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ final class ConfigTracerFactoryProvider implements TracerFactoryProviderInterfac

public function __construct(
private readonly TelemetryConfig $config,
private readonly FactoryInterface $factory
) {
}
private readonly FactoryInterface $factory,
) {}

public function getTracerFactory(?string $name = null): TracerFactoryInterface
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Telemetry\Exception;

class InvalidArgumentException extends TracerException
{
}
class InvalidArgumentException extends TracerException {}
4 changes: 1 addition & 3 deletions src/Exception/TracerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\Telemetry\Exception;

class TracerException extends \Exception
{
}
class TracerException extends \Exception {}
5 changes: 2 additions & 3 deletions src/LogTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Spiral\Telemetry;

use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactoryInterface;
use Spiral\Core\ScopeInterface;

Expand All @@ -22,7 +21,7 @@ public function __construct(
ScopeInterface $scope,
private readonly ClockInterface $clock,
private readonly LoggerInterface $logger,
private readonly UuidFactoryInterface $uuidFactory
private readonly UuidFactoryInterface $uuidFactory,
) {
parent::__construct($scope);
}
Expand All @@ -33,7 +32,7 @@ public function trace(
array $attributes = [],
bool $scoped = false,
?TraceKind $traceKind = null,
?int $startTime = null
?int $startTime = null,
): mixed {
$span = new Span($name, $attributes);

Expand Down
7 changes: 3 additions & 4 deletions src/LogTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Psr\Log\LoggerInterface;
use Ramsey\Uuid\UuidFactory;
use Spiral\Core\Attribute\Proxy;
use Spiral\Core\ScopeInterface;
use Spiral\Logger\LogsInterface;

Expand All @@ -21,10 +20,10 @@ final class LogTracerFactory implements TracerFactoryInterface
private readonly LoggerInterface $logger;

public function __construct(
#[Proxy] private readonly ScopeInterface $scope,
private readonly ScopeInterface $scope,
private readonly ClockInterface $clock,
LogsInterface $logs,
string $channel = self::LOG_CHANNEL
string $channel = self::LOG_CHANNEL,
) {
$this->logger = $logs->getLogger($channel);
}
Expand All @@ -35,7 +34,7 @@ public function make(array $context = []): TracerInterface
$this->scope,
$this->clock,
$this->logger,
new UuidFactory()
new UuidFactory(),
);
}
}
2 changes: 1 addition & 1 deletion src/NullTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function trace(
array $attributes = [],
bool $scoped = false,
?TraceKind $traceKind = null,
?int $startTime = null
?int $startTime = null,
): mixed {
$span = new Span($name);
$span->setAttributes($attributes);
Expand Down
6 changes: 2 additions & 4 deletions src/NullTracerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\Telemetry;

use Spiral\Core\Attribute\Proxy;
use Spiral\Core\Container;
use Spiral\Core\ScopeInterface;

Expand All @@ -15,9 +14,8 @@
final class NullTracerFactory implements TracerFactoryInterface
{
public function __construct(
#[Proxy] private readonly ?ScopeInterface $scope = new Container(),
) {
}
private readonly ?ScopeInterface $scope = new Container(),
) {}

public function make(array $context = []): TracerInterface
{
Expand Down
6 changes: 2 additions & 4 deletions src/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Spiral\Telemetry;

use Ramsey\Uuid\Uuid;
use Spiral\Telemetry\Span\Status;

/**
Expand All @@ -19,9 +18,8 @@ final class Span implements SpanInterface
*/
public function __construct(
private string $name,
private array $attributes = []
) {
}
private array $attributes = [],
) {}

public function getName(): string
{
Expand Down
5 changes: 2 additions & 3 deletions src/Span/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ final class Status
*/
public function __construct(
public readonly string|int $code,
public readonly ?string $description = null
) {
}
public readonly ?string $description = null,
) {}
}
2 changes: 1 addition & 1 deletion src/TracerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function trace(
array $attributes = [],
bool $scoped = false,
?TraceKind $traceKind = null,
?int $startTime = null
?int $startTime = null,
): mixed;
}
2 changes: 1 addition & 1 deletion tests/Clock/SystemClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function testNow(): void
{
$clock = new SystemClock();

$this->assertIsInt($clock->now());
self::assertIsInt($clock->now());
}
}
14 changes: 7 additions & 7 deletions tests/Config/TelemetryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@ public function testGetsDefaultDriver(): void
{
$config = new TelemetryConfig(['default' => 'foo']);

$this->assertSame('foo', $config->getDefaultDriver());
self::assertSame('foo', $config->getDefaultDriver());
}

public function testGetsDriverConfigAsString(): void
{
$config = new TelemetryConfig(['drivers' => [
'foo' => 'bar'
'foo' => 'bar',
]]);

$this->assertSame('bar', $config->getDriverConfig('foo'));
self::assertSame('bar', $config->getDriverConfig('foo'));
}

public function testGetsDriverConfigAsAutowire(): void
{
$config = new TelemetryConfig(['drivers' => [
'foo' => $driver = new Autowire('bar')
'foo' => $driver = new Autowire('bar'),
]]);

$this->assertSame($driver, $config->getDriverConfig('foo'));
self::assertSame($driver, $config->getDriverConfig('foo'));
}

public function testGetsDriverConfigAsObject(): void
{
$config = new TelemetryConfig(['drivers' => [
'foo' => $driver = m::mock(TracerFactoryInterface::class)
'foo' => $driver = m::mock(TracerFactoryInterface::class),
]]);

$this->assertSame($driver, $config->getDriverConfig('foo'));
self::assertSame($driver, $config->getDriverConfig('foo'));
}
}
Loading

0 comments on commit 122a44f

Please sign in to comment.