Skip to content

Commit

Permalink
Fix Psalm errors (#16)
Browse files Browse the repository at this point in the history
* Fix psalm issues, strict_types

* Fix phpunit tests

* Revert the use of the withServicePrefix method

* Improve constant type
  • Loading branch information
msmakouz authored Sep 23, 2024
1 parent 9a7518e commit c9cecf4
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 51 deletions.
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<RedundantCastGivenDocblockType errorLevel="suppress" />
<RedundantCondition errorLevel="suppress" />
<DocblockTypeContradiction errorLevel="suppress" />
<MissingClassConstType errorLevel="suppress" />
</issueHandlers>
<projectFiles>
<directory name="src" />
Expand Down
11 changes: 10 additions & 1 deletion src/AbstractMetrics.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use Spiral\Goridge\RPC\RPCInterface;

abstract class AbstractMetrics implements MetricsInterface
{
protected readonly RPCInterface $rpc;

/**
* @var string
* @var non-empty-string
*/
protected const SERVICE_NAME = 'metrics';

public function __construct(RPCInterface $rpc)
{
$this->rpc = $rpc->withServicePrefix(static::SERVICE_NAME);
}
}
2 changes: 2 additions & 0 deletions src/Collector.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use JetBrains\PhpStorm\Pure;
Expand Down
2 changes: 2 additions & 0 deletions src/Exception/MetricsException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics\Exception;

use Spiral\Goridge\RPC\Exception\ServiceException;
Expand Down
23 changes: 7 additions & 16 deletions src/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,15 @@

namespace Spiral\RoadRunner\Metrics;

use Spiral\Goridge\RPC\AsyncRPCInterface;
use Spiral\Goridge\RPC\Exception\ServiceException;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\Metrics\Exception\MetricsException;
use function compact;
use function str_contains;

class Metrics extends AbstractMetrics
{
public function __construct(
protected readonly RPCInterface $rpc
) {
}

public function add(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->call('metrics.Add', compact('name', 'value', 'labels'));
$this->rpc->call('Add', \compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -30,7 +21,7 @@ public function add(string $name, float $value, array $labels = []): void
public function sub(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->call('metrics.Sub', compact('name', 'value', 'labels'));
$this->rpc->call('Sub', \compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -39,7 +30,7 @@ public function sub(string $name, float $value, array $labels = []): void
public function observe(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->call('metrics.Observe', compact('name', 'value', 'labels'));
$this->rpc->call('Observe', \compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -48,7 +39,7 @@ public function observe(string $name, float $value, array $labels = []): void
public function set(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->call('metrics.Set', compact('name', 'value', 'labels'));
$this->rpc->call('Set', \compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -57,12 +48,12 @@ public function set(string $name, float $value, array $labels = []): void
public function declare(string $name, CollectorInterface $collector): void
{
try {
$this->rpc->call('metrics.Declare', [
$this->rpc->call('Declare', [
'name' => $name,
'collector' => $collector->toArray(),
]);
} catch (ServiceException $e) {
if (str_contains($e->getMessage(), 'tried to register existing collector')) {
if (\str_contains($e->getMessage(), 'tried to register existing collector')) {
// suppress duplicate metric error
return;
}
Expand All @@ -74,7 +65,7 @@ public function declare(string $name, CollectorInterface $collector): void
public function unregister(string $name): void
{
try {
$this->rpc->call('metrics.Unregister', $name);
$this->rpc->call('Unregister', $name);
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/MetricsFactory.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use Psr\Log\LoggerInterface;
Expand Down
20 changes: 13 additions & 7 deletions src/MetricsIgnoreResponse.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use Spiral\Goridge\RPC\AsyncRPCInterface;
use Spiral\Goridge\RPC\Exception\ServiceException;
use Spiral\RoadRunner\Metrics\Exception\MetricsException;

/**
* @property AsyncRPCInterface $rpc
*/
class MetricsIgnoreResponse extends AbstractMetrics
{
public function __construct(
protected readonly AsyncRPCInterface $rpc
AsyncRPCInterface $rpc
) {
parent::__construct($rpc);
}

public function add(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->callIgnoreResponse('metrics.Add', compact('name', 'value', 'labels'));
$this->rpc->callIgnoreResponse('Add', compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -25,7 +31,7 @@ public function add(string $name, float $value, array $labels = []): void
public function sub(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->callIgnoreResponse('metrics.Sub', compact('name', 'value', 'labels'));
$this->rpc->callIgnoreResponse('Sub', compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -34,7 +40,7 @@ public function sub(string $name, float $value, array $labels = []): void
public function observe(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->callIgnoreResponse('metrics.Observe', compact('name', 'value', 'labels'));
$this->rpc->callIgnoreResponse('Observe', compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -43,7 +49,7 @@ public function observe(string $name, float $value, array $labels = []): void
public function set(string $name, float $value, array $labels = []): void
{
try {
$this->rpc->callIgnoreResponse('metrics.Set', compact('name', 'value', 'labels'));
$this->rpc->callIgnoreResponse('Set', compact('name', 'value', 'labels'));
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -52,7 +58,7 @@ public function set(string $name, float $value, array $labels = []): void
public function declare(string $name, CollectorInterface $collector): void
{
try {
$this->rpc->call('metrics.Declare', [
$this->rpc->call('Declare', [
'name' => $name,
'collector' => $collector->toArray(),
]);
Expand All @@ -69,7 +75,7 @@ public function declare(string $name, CollectorInterface $collector): void
public function unregister(string $name): void
{
try {
$this->rpc->call('metrics.Unregister', $name);
$this->rpc->call('Unregister', $name);
} catch (ServiceException $e) {
throw new MetricsException($e->getMessage(), $e->getCode(), $e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/MetricsOptions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

class MetricsOptions
Expand Down
2 changes: 2 additions & 0 deletions src/RetryMetrics.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use Spiral\RoadRunner\Metrics\Exception\MetricsException;
Expand Down
2 changes: 2 additions & 0 deletions src/SuppressExceptionsMetrics.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Metrics;

use Psr\Log\LoggerInterface;
Expand Down
18 changes: 10 additions & 8 deletions tests/Unit/MetricsIgnoreResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
use PHPUnit\Framework\TestCase;
use Spiral\Goridge\RPC\AsyncRPCInterface;
use Spiral\Goridge\RPC\Exception\ServiceException;
use Spiral\Goridge\RPC\RPCInterface;
use Spiral\RoadRunner\Metrics\CollectorInterface;
use Spiral\RoadRunner\Metrics\Exception\MetricsException;
use Spiral\RoadRunner\Metrics\Metrics;
use Spiral\RoadRunner\Metrics\MetricsIgnoreResponse;

final class MetricsIgnoreResponseTest extends TestCase
Expand All @@ -22,14 +20,18 @@ protected function setUp(): void
parent::setUp();

$this->rpc = $this->createMock(AsyncRPCInterface::class);
$this->rpc->expects($this->once())->method('withServicePrefix')
->with('metrics')
->willReturn($this->rpc);

$this->metrics = new MetricsIgnoreResponse($this->rpc);
}

public function testAdd(): void
{
$this->rpc->expects($this->once())
->method('callIgnoreResponse')
->with('metrics.Add', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);
->with('Add', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);

$this->metrics->add('foo', 1.0, ['bar', 'baz']);
}
Expand All @@ -38,7 +40,7 @@ public function testSub(): void
{
$this->rpc->expects($this->once())
->method('callIgnoreResponse')
->with('metrics.Sub', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);
->with('Sub', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);

$this->metrics->sub('foo', 1.0, ['bar', 'baz']);
}
Expand All @@ -47,7 +49,7 @@ public function testObserve(): void
{
$this->rpc->expects($this->once())
->method('callIgnoreResponse')
->with('metrics.Observe', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);
->with('Observe', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);

$this->metrics->observe('foo', 1.0, ['bar', 'baz']);
}
Expand All @@ -56,7 +58,7 @@ public function testSet(): void
{
$this->rpc->expects($this->once())
->method('callIgnoreResponse')
->with('metrics.Set', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);
->with('Set', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']]);

$this->metrics->set('foo', 1.0, ['bar', 'baz']);
}
Expand All @@ -70,7 +72,7 @@ public function testDeclare(): void

$this->rpc->expects($this->once())
->method('call')
->with('metrics.Declare', ['name' => 'foo', 'collector' => $payload])
->with('Declare', ['name' => 'foo', 'collector' => $payload])
->willReturn(null);

$this->metrics->declare('foo', $collector);
Expand Down Expand Up @@ -112,7 +114,7 @@ public function testUnregister(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Unregister', 'foo')
->with('Unregister', 'foo')
->willReturn(null);

$this->metrics->unregister('foo');
Expand Down
16 changes: 10 additions & 6 deletions tests/Unit/MetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ protected function setUp(): void
parent::setUp();

$this->rpc = $this->createMock(RPCInterface::class);
$this->rpc->expects($this->once())->method('withServicePrefix')
->with('metrics')
->willReturn($this->rpc);

$this->metrics = new Metrics($this->rpc);
}

public function testAdd(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Add', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->with('Add', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->willReturn(null);

$this->metrics->add('foo', 1.0, ['bar', 'baz']);
Expand All @@ -53,7 +57,7 @@ public function testSub(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Sub', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->with('Sub', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->willReturn(null);

$this->metrics->sub('foo', 1.0, ['bar', 'baz']);
Expand All @@ -78,7 +82,7 @@ public function testObserve(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Observe', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->with('Observe', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->willReturn(null);

$this->metrics->observe('foo', 1.0, ['bar', 'baz']);
Expand All @@ -103,7 +107,7 @@ public function testSet(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Set', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->with('Set', ['name' => 'foo', 'value' => 1.0, 'labels' => ['bar', 'baz']])
->willReturn(null);

$this->metrics->set('foo', 1.0, ['bar', 'baz']);
Expand Down Expand Up @@ -133,7 +137,7 @@ public function testDeclare(): void

$this->rpc->expects($this->once())
->method('call')
->with('metrics.Declare', ['name' => 'foo', 'collector' => $payload])
->with('Declare', ['name' => 'foo', 'collector' => $payload])
->willReturn(null);

$this->metrics->declare('foo', $collector);
Expand Down Expand Up @@ -175,7 +179,7 @@ public function testUnregister(): void
{
$this->rpc->expects($this->once())
->method('call')
->with('metrics.Unregister', 'foo')
->with('Unregister', 'foo')
->willReturn(null);

$this->metrics->unregister('foo');
Expand Down
Loading

0 comments on commit c9cecf4

Please sign in to comment.