Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Psalm errors #16

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" />
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider the implications of suppressing MissingClassConstType errors.

Suppressing MissingClassConstType errors means that Psalm will no longer report issues when class constants are declared without explicit type annotations. While this allows for more flexibility, it may also introduce potential type-related issues that could go undetected.

It's generally recommended to maintain strict type checking to catch potential issues early and improve code quality. Instead of suppressing these errors, consider refactoring the codebase to use typed class constants.

If you need assistance in refactoring the codebase to use typed class constants, I'd be happy to help. We can work together to identify the affected class constants and add appropriate type annotations to ensure better type safety. Let me know if you'd like me to open a GitHub issue to track this refactoring effort.

</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
Loading