Skip to content

Commit

Permalink
Apply code style
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 9, 2025
1 parent 4e4981c commit c825782
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 164 deletions.
3 changes: 1 addition & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ final class Context implements ContextInterface, \IteratorAggregate, \Countable,
*/
public function __construct(
private array $values,
) {
}
) {}

public function withValue(string $key, mixed $value): ContextInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public static function encode(mixed $payload): string
*/
public static function decode(string $payload): array
{
return (array)\json_decode($payload, true, self::DEFAULT_JSON_DEPTH, self::DEFAULT_JSON_FLAGS);
return (array) \json_decode($payload, true, self::DEFAULT_JSON_DEPTH, self::DEFAULT_JSON_FLAGS);
}
}
1 change: 0 additions & 1 deletion src/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ final class Invoker implements InvokerInterface
private const ERROR_METHOD_RETURN =
'Method %s must return an object that instance of %s, ' .
'but the result provides type of %s';

private const ERROR_METHOD_IN_TYPE =
'Method %s input type must be an instance of %s, ' .
'but the input is type of %s';
Expand Down
83 changes: 38 additions & 45 deletions src/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,18 @@ final class Method
private const ERROR_PARAMS_COUNT =
'The GRPC method %s can only contain 2 parameters (input and output), but ' .
'signature contains an %d parameters';

private const ERROR_PARAM_UNION_TYPE =
'Parameter $%s of the GRPC method %s cannot be declared using union type';

private const ERROR_PARAM_CONTEXT_TYPE =
'The first parameter $%s of the GRPC method %s can only take an instance of %s';

private const ERROR_PARAM_INPUT_TYPE =
'The second (input) parameter $%s of the GRPC method %s can only take ' .
'an instance of %s, but type %s is indicated';

private const ERROR_RETURN_UNION_TYPE =
'Return type of the GRPC method %s cannot be declared using union type';

private const ERROR_RETURN_TYPE =
'Return type of the GRPC method %s must return ' .
'an instance of %s, but type %s is indicated';

private const ERROR_INVALID_GRPC_METHOD = 'Method %s is not valid GRPC method.';

/**
Expand All @@ -45,7 +39,44 @@ private function __construct(
public readonly string $name,
public readonly string $inputType,
public readonly string $outputType,
) {
) {}

/**
* Returns true if method signature matches.
*/
public static function match(\ReflectionMethod $method): bool
{
try {
self::assertMethodSignature($method);
} catch (\Throwable) {
return false;
}

return true;
}

/**
* Creates a new {@see Method} object from a {@see \ReflectionMethod} object.
*/
public static function parse(\ReflectionMethod $method): Method
{
try {
self::assertMethodSignature($method);
} catch (\Throwable $e) {
$message = \sprintf(self::ERROR_INVALID_GRPC_METHOD, $method->getName());
throw GRPCException::create($message, StatusCode::INTERNAL, $e);
}

[, $input] = $method->getParameters();

/** @var \ReflectionNamedType $inputType */
$inputType = $input->getType();

/** @var \ReflectionNamedType $returnType */
$returnType = $method->getReturnType();

/** @psalm-suppress ArgumentTypeCoercion */
return new self($method->getName(), $inputType->getName(), $returnType->getName());
}

/**
Expand Down Expand Up @@ -75,20 +106,6 @@ public function getOutputType(): string
return $this->outputType;
}

/**
* Returns true if method signature matches.
*/
public static function match(\ReflectionMethod $method): bool
{
try {
self::assertMethodSignature($method);
} catch (\Throwable) {
return false;
}

return true;
}

/**
* @throws \ReflectionException
*/
Expand Down Expand Up @@ -231,28 +248,4 @@ private static function assertMethodSignature(\ReflectionMethod $method): void
// The return type must be declared as a Google\Protobuf\Internal\Message class
self::assertOutputReturnType($method);
}

/**
* Creates a new {@see Method} object from a {@see \ReflectionMethod} object.
*/
public static function parse(\ReflectionMethod $method): Method
{
try {
self::assertMethodSignature($method);
} catch (\Throwable $e) {
$message = \sprintf(self::ERROR_INVALID_GRPC_METHOD, $method->getName());
throw GRPCException::create($message, StatusCode::INTERNAL, $e);
}

[, $input] = $method->getParameters();

/** @var \ReflectionNamedType $inputType */
$inputType = $input->getType();

/** @var \ReflectionNamedType $returnType */
$returnType = $method->getReturnType();

/** @psalm-suppress ArgumentTypeCoercion */
return new self($method->getName(), $inputType->getName(), $returnType->getName());
}
}
1 change: 0 additions & 1 deletion src/ResponseHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function set(string $key, string $value): void

/**
* @param THeaderKey $key
* @param string|null $default
* @return THeaderValue|null
*/
public function get(string $key, ?string $default = null): ?string
Expand Down
71 changes: 35 additions & 36 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ final class Server
public function __construct(
private readonly InvokerInterface $invoker = new Invoker(),
private readonly array $options = [],
) {
}
) {}

/**
* Register new GRPC service.
Expand All @@ -63,39 +62,6 @@ public function registerService(string $interface, ServiceInterface $service): v
$this->services[$service->getName()] = $service;
}

/**
* @param ContextResponse $data
* @return array{0: string, 1: string}
* @throws \JsonException
* @throws \Throwable
*/
private function tick(string $body, array $data): array
{
$context = (new Context($data['context']))
->withValue(ResponseHeaders::class, new ResponseHeaders());

$response = $this->invoke($data['service'], $data['method'], $context, $body);

/** @var ResponseHeaders|null $responseHeaders */
$responseHeaders = $context->getValue(ResponseHeaders::class);
$responseHeadersString = $responseHeaders ? $responseHeaders->packHeaders() : '{}';

return [$response, $responseHeadersString];
}

/**
* @psalm-suppress InaccessibleMethod
*/
private function workerSend(WorkerInterface $worker, string $body, string $headers): void
{
$worker->respond(new Payload($body, $headers));
}

private function workerError(WorkerInterface $worker, string $message): void
{
$worker->error($message);
}

/**
* Serve GRPC over given RoadRunner worker.
*/
Expand All @@ -121,7 +87,7 @@ public function serve(?WorkerInterface $worker = null, ?callable $finalize = nul
} catch (GRPCExceptionInterface $e) {
$this->workerGrpcError($worker, $e);
} catch (\Throwable $e) {
$this->workerError($worker, $this->isDebugMode() ? (string)$e : $e->getMessage());
$this->workerError($worker, $this->isDebugMode() ? (string) $e : $e->getMessage());
} finally {
if ($finalize !== null) {
isset($e) ? $finalize($e) : $finalize();
Expand All @@ -146,6 +112,39 @@ protected function invoke(string $service, string $method, ContextInterface $con
return $this->services[$service]->invoke($method, $context, $body);
}

/**
* @param ContextResponse $data
* @return array{0: string, 1: string}
* @throws \JsonException
* @throws \Throwable
*/
private function tick(string $body, array $data): array
{
$context = (new Context($data['context']))
->withValue(ResponseHeaders::class, new ResponseHeaders());

$response = $this->invoke($data['service'], $data['method'], $context, $body);

/** @var ResponseHeaders|null $responseHeaders */
$responseHeaders = $context->getValue(ResponseHeaders::class);
$responseHeadersString = $responseHeaders ? $responseHeaders->packHeaders() : '{}';

return [$response, $responseHeadersString];
}

/**
* @psalm-suppress InaccessibleMethod
*/
private function workerSend(WorkerInterface $worker, string $body, string $headers): void
{
$worker->respond(new Payload($body, $headers));
}

private function workerError(WorkerInterface $worker, string $message): void
{
$worker->error($message);
}

private function workerGrpcError(WorkerInterface $worker, GRPCExceptionInterface $e): void
{
$status = new Status([
Expand Down
4 changes: 1 addition & 3 deletions src/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* Indicates that given class expected to be GRPC service.
*/
interface ServiceInterface
{
}
interface ServiceInterface {}
15 changes: 7 additions & 8 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ContextTest extends TestCase
public function testGetValue(): void
{
$ctx = new Context([
'key' => ['value']
'key' => ['value'],
]);

$this->assertSame(['value'], $ctx->getValue('key'));
Expand All @@ -22,7 +22,7 @@ public function testGetValue(): void
public function testGetNullValue(): void
{
$ctx = new Context([
'key' => ['value']
'key' => ['value'],
]);

$this->assertSame(null, $ctx->getValue('other'));
Expand All @@ -31,19 +31,18 @@ public function testGetNullValue(): void
public function testGetValues(): void
{
$ctx = new Context([
'key' => ['value']
'key' => ['value'],
]);

$this->assertSame([
'key' => ['value']
'key' => ['value'],
], $ctx->getValues());
}


public function testWithValue(): void
{
$ctx = new Context([
'key' => ['value']
'key' => ['value'],
]);

$this->assertSame(['value'], $ctx->getValue('key'));
Expand All @@ -60,7 +59,7 @@ public function testWithValue(): void
public function testGetOutgoingHeader(): void
{
$outgoingHeaders = [
'Set-Cookie' => 'foobar'
'Set-Cookie' => 'foobar',
];
$ctx = new Context([ResponseHeaders::class => new ResponseHeaders($outgoingHeaders)]);

Expand All @@ -71,7 +70,7 @@ public function testGetOutgoingHeader(): void
public function testGetOutgoingHeaders(): void
{
$outgoingHeaders = new ResponseHeaders([
'Set-Cookie' => 'foobar'
'Set-Cookie' => 'foobar',
]);
$ctx = new Context([ResponseHeaders::class => $outgoingHeaders]);
$this->assertSame($outgoingHeaders, $ctx->getValue(ResponseHeaders::class));
Expand Down
4 changes: 1 addition & 3 deletions tests/InvalidInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Spiral\RoadRunner\GRPC\Tests;

interface InvalidInterface
{
}
interface InvalidInterface {}
20 changes: 5 additions & 15 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,13 @@ public function testMethodOutputType(): void
$this->assertSame(Message::class, $m->outputType);
}

public function tM(ContextInterface $context, TestService $input): Message
{
}
public function tM(ContextInterface $context, TestService $input): Message {}

public function tM2(ContextInterface $context, Message $input): TestService
{
}
public function tM2(ContextInterface $context, Message $input): TestService {}

public function tM3(TestService $context, Message $input): TestService
{
}
public function tM3(TestService $context, Message $input): TestService {}

public function tM4(TestService $context, Message $input): Invalid
{
}
public function tM4(TestService $context, Message $input): Invalid {}

public function tM5(TestService $context, Message $input): void
{
}
public function tM5(TestService $context, Message $input): void {}
}
Loading

0 comments on commit c825782

Please sign in to comment.