Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.

Commit 7d56393

Browse files
committed
AFix psalm errors and improve types
1 parent fdd9e82 commit 7d56393

File tree

6 files changed

+32
-21
lines changed

6 files changed

+32
-21
lines changed

psalm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<issueHandlers>
1717
<RedundantConditionGivenDocblockType errorLevel="suppress" />
1818
<MixedReturnStatement errorLevel="suppress" />
19+
<UnusedPsalmSuppress errorLevel="suppress" />
1920
</issueHandlers>
2021
<projectFiles>
2122
<directory name="src" />

src/Exception/GRPCExceptionInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface GRPCExceptionInterface extends \Throwable
2323
/**
2424
* Returns GRPC exception status code.
2525
*
26+
* @psalm-suppress MissingImmutableAnnotation
2627
* @psalm-return StatusCodeType
2728
* @return int
2829
*/

src/Invoker.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ private function assertResultType(Method $method, $result): bool
8181
* @param string|null $body
8282
* @return Message
8383
* @throws InvokeException
84-
* @psalm-suppress InvalidStringClass
8584
*/
8685
private function makeInput(Method $method, ?string $body): Message
8786
{
@@ -92,7 +91,7 @@ private function makeInput(Method $method, ?string $body): Message
9291
// assertions option ("zend.assertions") is enabled.
9392
assert($this->assertInputType($method, $class));
9493

95-
/** @var Message $in */
94+
/** @psalm-suppress UnsafeInstantiation */
9695
$in = new $class();
9796

9897
if ($body !== null) {

src/Method.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function getOutputType(): string
124124
private static function getReflectionClassByType(?\ReflectionType $type): ?\ReflectionClass
125125
{
126126
if ($type instanceof \ReflectionNamedType && ! $type->isBuiltin()) {
127+
/** @psalm-suppress ArgumentTypeCoercion */
127128
return new \ReflectionClass($type->getName());
128129
}
129130

@@ -319,6 +320,7 @@ public static function parse(\ReflectionMethod $method): Method
319320
/** @var \ReflectionNamedType $returnType */
320321
$returnType = $method->getReturnType();
321322

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

src/Server.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public function __construct(InvokerInterface $invoker = null, array $options = [
6868
* $server->registerService(EchoServiceInterface::class, new EchoService());
6969
* </code>
7070
*
71-
* @param string $interface Generated service interface.
72-
* @param ServiceInterface $service Must implement interface.
71+
* @template T of ServiceInterface
72+
*
73+
* @param class-string<T> $interface Generated service interface.
74+
* @param T $service Must implement interface.
7375
* @throws ServiceException
7476
*/
7577
public function registerService(string $interface, ServiceInterface $service): void
@@ -105,6 +107,7 @@ private function tick(string $body, array $data): array
105107
* @param Worker $worker
106108
* @param string $body
107109
* @param string $headers
110+
* @psalm-suppress InaccessibleMethod
108111
*/
109112
private function workerSend(Worker $worker, string $body, string $headers): void
110113
{
@@ -131,9 +134,13 @@ private function workerError(Worker $worker, string $message): void
131134
/**
132135
* @param Worker $worker
133136
* @return array { 0: string, 1: string } | null
137+
*
138+
* @psalm-suppress UndefinedMethod
139+
* @psalm-suppress PossiblyUndefinedVariable
134140
*/
135141
private function workerReceive(Worker $worker): ?array
136142
{
143+
/** @var string|\Stringable $body */
137144
$body = $worker->receive($ctx);
138145

139146
if (empty($body) && empty($ctx)) {

src/ServiceWrapper.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ final class ServiceWrapper
3434

3535
/**
3636
* @param InvokerInterface $invoker
37-
* @param string $interface Service interface name.
37+
* @param class-string $interface Service interface name.
3838
* @param ServiceInterface $service
39-
*
4039
* @throws ServiceException
4140
*/
42-
public function __construct(
43-
InvokerInterface $invoker,
44-
string $interface,
45-
ServiceInterface $service
46-
) {
41+
public function __construct(InvokerInterface $invoker, string $interface, ServiceInterface $service)
42+
{
4743
$this->invoker = $invoker;
44+
4845
$this->configure($interface, $service);
4946
}
5047

@@ -76,40 +73,44 @@ public function getMethods(): array
7673
* @param string $method
7774
* @param ContextInterface $context
7875
* @param string|null $input
79-
* @param array $metadata
8076
* @return string
81-
*
8277
* @throws NotFoundException
8378
* @throws InvokeException
8479
*/
85-
public function invoke(string $method, ContextInterface $context, ?string $input, array &$metadata = []): string
80+
public function invoke(string $method, ContextInterface $context, ?string $input): string
8681
{
8782
if (! isset($this->methods[$method])) {
8883
throw NotFoundException::create("Method `{$method}` not found in service `{$this->name}`.");
8984
}
9085

91-
return $this->invoker->invoke($this->service, $this->methods[$method], $context, $input, $metadata);
86+
return $this->invoker->invoke($this->service, $this->methods[$method], $context, $input);
9287
}
9388

9489
/**
9590
* Configure service name and methods.
9691
*
97-
* @param string $interface
92+
* @param class-string $interface
9893
* @param ServiceInterface $service
99-
*
10094
* @throws ServiceException
10195
*/
10296
protected function configure(string $interface, ServiceInterface $service): void
10397
{
10498
try {
105-
$r = new \ReflectionClass($interface);
99+
$reflection = new \ReflectionClass($interface);
106100

107-
if (! $r->hasConstant('NAME')) {
101+
if (! $reflection->hasConstant('NAME')) {
108102
$message = "Invalid service interface `{$interface}`, constant `NAME` not found.";
109103
throw ServiceException::create($message);
110104
}
111105

112-
$this->name = $r->getConstant('NAME');
106+
$name = $reflection->getConstant('NAME');
107+
108+
if (! \is_string($name)) {
109+
$message = "Constant `NAME` of service interface `{$interface}` must be a type of string";
110+
throw ServiceException::create($message);
111+
}
112+
113+
$this->name = $name;
113114
} catch (\ReflectionException $e) {
114115
$message = "Invalid service interface `{$interface}`.";
115116
throw ServiceException::create($message, StatusCode::INTERNAL, $e);
@@ -127,7 +128,7 @@ protected function configure(string $interface, ServiceInterface $service): void
127128

128129
/**
129130
* @param ServiceInterface $service
130-
* @return array
131+
* @return array<string, Method>
131132
*/
132133
protected function fetchMethods(ServiceInterface $service): array
133134
{

0 commit comments

Comments
 (0)