Skip to content

Commit 20e789d

Browse files
authored
Fix return type in abstract methods from self to static (#53)
* Change return type of abstract request methods * Change return type of `fromParams` method to `static` in request classes (cherry picked from commit 94799ab) * Make request classes final and update `getParams` return types for improved type safety (cherry picked from commit d6be496)
1 parent 4b66732 commit 20e789d

17 files changed

+97
-46
lines changed

src/Schema/JsonRpc/Request.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract public static function getMethod(): string;
3636
/**
3737
* @param RequestData $data
3838
*/
39-
public static function fromArray(array $data): self
39+
public static function fromArray(array $data): static
4040
{
4141
if (($data['jsonrpc'] ?? null) !== MessageInterface::JSONRPC_VERSION) {
4242
throw new InvalidArgumentException('Invalid or missing "jsonrpc" version for Request.');
@@ -68,7 +68,7 @@ public static function fromArray(array $data): self
6868
/**
6969
* @param array<string, mixed>|null $params
7070
*/
71-
abstract protected static function fromParams(?array $params): self;
71+
abstract protected static function fromParams(?array $params): static;
7272

7373
public function getId(): string|int
7474
{
@@ -97,7 +97,7 @@ public function jsonSerialize(): array
9797
}
9898

9999
/**
100-
* @return array<string, mixed>|null
100+
* @return array<non-empty-string, mixed>|null
101101
*/
102102
abstract protected function getParams(): ?array;
103103
}

src/Schema/Request/CallToolRequest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Kyrian Obikwelu <[email protected]>
2121
*/
22-
class CallToolRequest extends Request
22+
final class CallToolRequest extends Request
2323
{
2424
/**
2525
* @param string $name the name of the tool to invoke
@@ -36,7 +36,7 @@ public static function getMethod(): string
3636
return 'tools/call';
3737
}
3838

39-
protected static function fromParams(?array $params): Request
39+
protected static function fromParams(?array $params): static
4040
{
4141
if (!isset($params['name']) || !\is_string($params['name'])) {
4242
throw new InvalidArgumentException('Missing or invalid "name" parameter for tools/call.');
@@ -58,7 +58,10 @@ protected static function fromParams(?array $params): Request
5858
);
5959
}
6060

61-
protected function getParams(): ?array
61+
/**
62+
* @return array{name: string, arguments: array<string, mixed>}
63+
*/
64+
protected function getParams(): array
6265
{
6366
return [
6467
'name' => $this->name,

src/Schema/Request/CompletionCompleteRequest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Kyrian Obikwelu <[email protected]>
2323
*/
24-
class CompletionCompleteRequest extends Request
24+
final class CompletionCompleteRequest extends Request
2525
{
2626
/**
2727
* @param PromptReference|ResourceReference $ref the prompt or resource to complete
@@ -38,7 +38,7 @@ public static function getMethod(): string
3838
return 'completion/complete';
3939
}
4040

41-
protected static function fromParams(?array $params): Request
41+
protected static function fromParams(?array $params): static
4242
{
4343
if (!isset($params['ref']) || !\is_array($params['ref'])) {
4444
throw new InvalidArgumentException('Missing or invalid "ref" parameter for completion/complete.');
@@ -57,7 +57,13 @@ protected static function fromParams(?array $params): Request
5757
return new self($ref, $params['argument']);
5858
}
5959

60-
protected function getParams(): ?array
60+
/**
61+
* @return array{
62+
* ref: PromptReference|ResourceReference,
63+
* argument: array{ name: string, value: string }
64+
* }
65+
*/
66+
protected function getParams(): array
6167
{
6268
return [
6369
'ref' => $this->ref,

src/Schema/Request/CreateSamplingMessageRequest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @author Kyrian Obikwelu <[email protected]>
2525
*/
26-
class CreateSamplingMessageRequest extends Request
26+
final class CreateSamplingMessageRequest extends Request
2727
{
2828
/**
2929
* @param SamplingMessage[] $messages the messages to send to the model
@@ -59,7 +59,7 @@ public static function getMethod(): string
5959
return 'sampling/createMessage';
6060
}
6161

62-
protected static function fromParams(?array $params): Request
62+
protected static function fromParams(?array $params): static
6363
{
6464
if (!isset($params['messages']) || !\is_array($params['messages'])) {
6565
throw new InvalidArgumentException('Missing or invalid "messages" parameter for sampling/createMessage.');
@@ -86,7 +86,19 @@ protected static function fromParams(?array $params): Request
8686
);
8787
}
8888

89-
protected function getParams(): ?array
89+
/**
90+
* @return array{
91+
* messages: SamplingMessage[],
92+
* maxTokens: int,
93+
* preferences?: ModelPreferences,
94+
* systemPrompt?: string,
95+
* includeContext?: string,
96+
* temperature?: float,
97+
* stopSequences?: string[],
98+
* metadata?: array<string, mixed>
99+
* }
100+
*/
101+
protected function getParams(): array
90102
{
91103
$params = [
92104
'messages' => $this->messages,

src/Schema/Request/GetPromptRequest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @author Kyrian Obikwelu <[email protected]>
2121
*/
22-
class GetPromptRequest extends Request
22+
final class GetPromptRequest extends Request
2323
{
2424
/**
2525
* @param string $name the name of the prompt to get
@@ -36,7 +36,7 @@ public static function getMethod(): string
3636
return 'prompts/get';
3737
}
3838

39-
protected static function fromParams(?array $params): Request
39+
protected static function fromParams(?array $params): static
4040
{
4141
if (!isset($params['name']) || !\is_string($params['name']) || empty($params['name'])) {
4242
throw new InvalidArgumentException('Missing or invalid "name" parameter for prompts/get.');
@@ -55,7 +55,10 @@ protected static function fromParams(?array $params): Request
5555
return new self($params['name'], $arguments);
5656
}
5757

58-
protected function getParams(): ?array
58+
/**
59+
* @return array{name: string, arguments?: array<string, mixed>}
60+
*/
61+
protected function getParams(): array
5962
{
6063
$params = ['name' => $this->name];
6164

src/Schema/Request/InitializeRequest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Kyrian Obikwelu <[email protected]>
2323
*/
24-
class InitializeRequest extends Request
24+
final class InitializeRequest extends Request
2525
{
2626
/**
2727
* @param string $protocolVersion The latest version of the Model Context Protocol that the client supports. The client MAY decide to support older versions as well.
@@ -40,7 +40,7 @@ public static function getMethod(): string
4040
return 'initialize';
4141
}
4242

43-
protected static function fromParams(?array $params): Request
43+
protected static function fromParams(?array $params): static
4444
{
4545
if (!isset($params['protocolVersion'])) {
4646
throw new InvalidArgumentException('protocolVersion is required');
@@ -59,7 +59,10 @@ protected static function fromParams(?array $params): Request
5959
return new self($params['protocolVersion'], $capabilities, $clientInfo);
6060
}
6161

62-
protected function getParams(): ?array
62+
/**
63+
* @return array{protocolVersion: string, capabilities: ClientCapabilities, clientInfo: Implementation}
64+
*/
65+
protected function getParams(): array
6366
{
6467
return [
6568
'protocolVersion' => $this->protocolVersion,

src/Schema/Request/ListPromptsRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Kyrian Obikwelu <[email protected]>
2020
*/
21-
class ListPromptsRequest extends Request
21+
final class ListPromptsRequest extends Request
2222
{
2323
/**
2424
* If provided, the server should return results starting after this cursor.
@@ -35,11 +35,14 @@ public static function getMethod(): string
3535
return 'prompts/list';
3636
}
3737

38-
protected static function fromParams(?array $params): Request
38+
protected static function fromParams(?array $params): static
3939
{
4040
return new self($params['cursor'] ?? null);
4141
}
4242

43+
/**
44+
* @return array{cursor:string}|null
45+
*/
4346
protected function getParams(): ?array
4447
{
4548
$params = [];

src/Schema/Request/ListResourceTemplatesRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Kyrian Obikwelu <[email protected]>
2020
*/
21-
class ListResourceTemplatesRequest extends Request
21+
final class ListResourceTemplatesRequest extends Request
2222
{
2323
/**
2424
* @param string|null $cursor An opaque token representing the current pagination position.
@@ -35,11 +35,14 @@ public static function getMethod(): string
3535
return 'resources/templates/list';
3636
}
3737

38-
protected static function fromParams(?array $params): Request
38+
protected static function fromParams(?array $params): static
3939
{
4040
return new self($params['cursor'] ?? null);
4141
}
4242

43+
/**
44+
* @return array{cursor:string}|null
45+
*/
4346
protected function getParams(): ?array
4447
{
4548
$params = [];

src/Schema/Request/ListResourcesRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Kyrian Obikwelu <[email protected]>
2020
*/
21-
class ListResourcesRequest extends Request
21+
final class ListResourcesRequest extends Request
2222
{
2323
/**
2424
* @param string|null $cursor An opaque token representing the current pagination position.
@@ -35,11 +35,14 @@ public static function getMethod(): string
3535
return 'resources/list';
3636
}
3737

38-
protected static function fromParams(?array $params): Request
38+
protected static function fromParams(?array $params): static
3939
{
4040
return new self($params['cursor'] ?? null);
4141
}
4242

43+
/**
44+
* @return array{cursor:string}|null
45+
*/
4346
protected function getParams(): ?array
4447
{
4548
$params = [];

src/Schema/Request/ListRootsRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @author Kyrian Obikwelu <[email protected]>
2626
*/
27-
class ListRootsRequest extends Request
27+
final class ListRootsRequest extends Request
2828
{
2929
public function __construct(
3030
) {
@@ -35,7 +35,7 @@ public static function getMethod(): string
3535
return 'roots/list';
3636
}
3737

38-
protected static function fromParams(?array $params): Request
38+
protected static function fromParams(?array $params): static
3939
{
4040
return new self();
4141
}

0 commit comments

Comments
 (0)