Skip to content

Commit 12ea8ac

Browse files
committed
Address open PHPStan issues from baseline
1 parent 41486bf commit 12ea8ac

File tree

3 files changed

+79
-120
lines changed

3 files changed

+79
-120
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,92 +18,15 @@ parameters:
1818
count: 1
1919
path: src/Capability/Registry/ResourceTemplateReference.php
2020

21-
-
22-
message: '#^PHPDoc tag @return with type array is incompatible with native type object\.$#'
23-
identifier: return.phpDocType
24-
count: 1
25-
path: src/Schema/Result/EmptyResult.php
26-
2721
-
2822
message: '#^Method Mcp\\Schema\\Result\\ReadResourceResult\:\:jsonSerialize\(\) should return array\{contents\: array\<Mcp\\Schema\\Content\\BlobResourceContents\|Mcp\\Schema\\Content\\TextResourceContents\>\} but returns array\{contents\: array\<Mcp\\Schema\\Content\\ResourceContents\>\}\.$#'
2923
identifier: return.type
3024
count: 1
3125
path: src/Schema/Result/ReadResourceResult.php
3226

33-
-
34-
message: '#^Method Mcp\\Server\\Builder\:\:addPrompt\(\) has parameter \$handler with no value type specified in iterable type array\.$#'
35-
identifier: missingType.iterableValue
36-
count: 1
37-
path: src/Server/Builder.php
38-
39-
-
40-
message: '#^Method Mcp\\Server\\Builder\:\:addResource\(\) has parameter \$handler with no value type specified in iterable type array\.$#'
41-
identifier: missingType.iterableValue
42-
count: 1
43-
path: src/Server/Builder.php
44-
45-
-
46-
message: '#^Method Mcp\\Server\\Builder\:\:addResourceTemplate\(\) has parameter \$handler with no value type specified in iterable type array\.$#'
47-
identifier: missingType.iterableValue
48-
count: 1
49-
path: src/Server/Builder.php
50-
51-
-
52-
message: '#^Method Mcp\\Server\\Builder\:\:addTool\(\) has parameter \$handler with no value type specified in iterable type array\.$#'
53-
identifier: missingType.iterableValue
54-
count: 1
55-
path: src/Server/Builder.php
56-
57-
-
58-
message: '#^Method Mcp\\Server\\Builder\:\:addTool\(\) has parameter \$inputSchema with no value type specified in iterable type array\.$#'
59-
identifier: missingType.iterableValue
60-
count: 1
61-
path: src/Server/Builder.php
62-
63-
-
64-
message: '#^Method Mcp\\Server\\Builder\:\:getCompletionProviders\(\) return type has no value type specified in iterable type array\.$#'
65-
identifier: missingType.iterableValue
66-
count: 1
67-
path: src/Server/Builder.php
68-
69-
-
70-
message: '#^Method Mcp\\Server\\Builder\:\:setDiscovery\(\) has parameter \$excludeDirs with no value type specified in iterable type array\.$#'
71-
identifier: missingType.iterableValue
72-
count: 1
73-
path: src/Server/Builder.php
74-
75-
-
76-
message: '#^Method Mcp\\Server\\Builder\:\:setDiscovery\(\) has parameter \$scanDirs with no value type specified in iterable type array\.$#'
77-
identifier: missingType.iterableValue
78-
count: 1
79-
path: src/Server/Builder.php
80-
8127
-
8228
message: '#^Property Mcp\\Server\\Builder\:\:\$instructions is never read, only written\.$#'
8329
identifier: property.onlyWritten
8430
count: 1
8531
path: src/Server/Builder.php
8632

87-
-
88-
message: '#^Property Mcp\\Server\\Builder\:\:\$prompts type has no value type specified in iterable type array\.$#'
89-
identifier: missingType.iterableValue
90-
count: 1
91-
path: src/Server/Builder.php
92-
93-
-
94-
message: '#^Property Mcp\\Server\\Builder\:\:\$resourceTemplates type has no value type specified in iterable type array\.$#'
95-
identifier: missingType.iterableValue
96-
count: 1
97-
path: src/Server/Builder.php
98-
99-
-
100-
message: '#^Property Mcp\\Server\\Builder\:\:\$resources type has no value type specified in iterable type array\.$#'
101-
identifier: missingType.iterableValue
102-
count: 1
103-
path: src/Server/Builder.php
104-
105-
-
106-
message: '#^Property Mcp\\Server\\Builder\:\:\$tools type has no value type specified in iterable type array\.$#'
107-
identifier: missingType.iterableValue
108-
count: 1
109-
path: src/Server/Builder.php

src/Schema/Result/EmptyResult.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public static function fromArray(): self
3232
return new self();
3333
}
3434

35-
/**
36-
* @return array{}
37-
*/
3835
public function jsonSerialize(): object
3936
{
4037
return new \stdClass();

src/Server/Builder.php

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
use Mcp\Capability\Discovery\SchemaGenerator;
2020
use Mcp\Capability\Prompt\Completion\EnumCompletionProvider;
2121
use Mcp\Capability\Prompt\Completion\ListCompletionProvider;
22+
use Mcp\Capability\Prompt\Completion\ProviderInterface;
2223
use Mcp\Capability\Prompt\PromptGetter;
2324
use Mcp\Capability\Prompt\PromptGetterInterface;
2425
use Mcp\Capability\Registry;
2526
use Mcp\Capability\Registry\Container;
27+
use Mcp\Capability\Registry\ElementReference;
2628
use Mcp\Capability\Registry\ReferenceHandler;
2729
use Mcp\Capability\Resource\ResourceReader;
2830
use Mcp\Capability\Resource\ResourceReaderInterface;
@@ -50,6 +52,8 @@
5052
use Psr\SimpleCache\CacheInterface;
5153

5254
/**
55+
* @phpstan-import-type Handler from ElementReference
56+
*
5357
* @author Kyrian Obikwelu <[email protected]>
5458
*/
5559
final class Builder
@@ -80,40 +84,48 @@ final class Builder
8084

8185
private ?string $instructions = null;
8286

83-
/** @var array<
84-
* array{handler: array|string|\Closure,
85-
* name: string|null,
86-
* description: string|null,
87-
* annotations: ToolAnnotations|null}
88-
* > */
87+
/**
88+
* @var array{
89+
* handler: Handler,
90+
* name: ?string,
91+
* description: ?string,
92+
* annotations: ?ToolAnnotations,
93+
* }[]
94+
*/
8995
private array $tools = [];
9096

91-
/** @var array<
92-
* array{handler: array|string|\Closure,
97+
/**
98+
* @var array{
99+
* handler: Handler,
93100
* uri: string,
94-
* name: string|null,
95-
* description: string|null,
96-
* mimeType: string|null,
101+
* name: ?string,
102+
* description: ?string,
103+
* mimeType: ?string,
97104
* size: int|null,
98-
* annotations: Annotations|null}
99-
* > */
105+
* annotations: ?Annotations,
106+
* }[]
107+
*/
100108
private array $resources = [];
101109

102-
/** @var array<
103-
* array{handler: array|string|\Closure,
110+
/**
111+
* @var array{
112+
* handler: Handler,
104113
* uriTemplate: string,
105-
* name: string|null,
106-
* description: string|null,
107-
* mimeType: string|null,
108-
* annotations: Annotations|null}
109-
* > */
114+
* name: ?string,
115+
* description: ?string,
116+
* mimeType: ?string,
117+
* annotations: ?Annotations,
118+
* }[]
119+
*/
110120
private array $resourceTemplates = [];
111121

112-
/** @var array<
113-
* array{handler: array|string|\Closure,
114-
* name: string|null,
115-
* description: string|null}
116-
* > */
122+
/**
123+
* @var array{
124+
* handler: Handler,
125+
* name: ?string,
126+
* description: ?string,
127+
* }[]
128+
*/
117129
private array $prompts = [];
118130

119131
private ?string $discoveryBasePath = null;
@@ -223,6 +235,10 @@ public function setSession(
223235
return $this;
224236
}
225237

238+
/**
239+
* @param string[] $scanDirs
240+
* @param string[] $excludeDirs
241+
*/
226242
public function setDiscovery(
227243
string $basePath,
228244
array $scanDirs = ['.', 'src'],
@@ -239,6 +255,9 @@ public function setDiscovery(
239255

240256
/**
241257
* Manually registers a tool handler.
258+
*
259+
* @param Handler $handler
260+
* @param array<string, mixed>|null $inputSchema
242261
*/
243262
public function addTool(
244263
callable|array|string $handler,
@@ -254,9 +273,11 @@ public function addTool(
254273

255274
/**
256275
* Manually registers a resource handler.
276+
*
277+
* @param Handler $handler
257278
*/
258279
public function addResource(
259-
callable|array|string $handler,
280+
\Closure|array|string $handler,
260281
string $uri,
261282
?string $name = null,
262283
?string $description = null,
@@ -271,9 +292,11 @@ public function addResource(
271292

272293
/**
273294
* Manually registers a resource template handler.
295+
*
296+
* @param Handler $handler
274297
*/
275298
public function addResourceTemplate(
276-
callable|array|string $handler,
299+
\Closure|array|string $handler,
277300
string $uriTemplate,
278301
?string $name = null,
279302
?string $description = null,
@@ -294,8 +317,10 @@ public function addResourceTemplate(
294317

295318
/**
296319
* Manually registers a prompt handler.
320+
*
321+
* @param Handler $handler
297322
*/
298-
public function addPrompt(callable|array|string $handler, ?string $name = null, ?string $description = null): self
323+
public function addPrompt(\Closure|array|string $handler, ?string $name = null, ?string $description = null): self
299324
{
300325
$this->prompts[] = compact('handler', 'name', 'description');
301326

@@ -387,9 +412,7 @@ private function registerCapabilities(
387412
$tool = new Tool($name, $inputSchema, $description, $data['annotations']);
388413
$registry->registerTool($tool, $data['handler'], true);
389414

390-
$handlerDesc = $data['handler'] instanceof \Closure ? 'Closure' : (\is_array(
391-
$data['handler'],
392-
) ? implode('::', $data['handler']) : $data['handler']);
415+
$handlerDesc = $this->getHandlerDescription($data['handler']);
393416
$logger->debug("Registered manual tool {$name} from handler {$handlerDesc}");
394417
} catch (\Throwable $e) {
395418
$logger->error(
@@ -425,9 +448,7 @@ private function registerCapabilities(
425448
$resource = new Resource($uri, $name, $description, $mimeType, $annotations, $size);
426449
$registry->registerResource($resource, $data['handler'], true);
427450

428-
$handlerDesc = $data['handler'] instanceof \Closure ? 'Closure' : (\is_array(
429-
$data['handler'],
430-
) ? implode('::', $data['handler']) : $data['handler']);
451+
$handlerDesc = $this->getHandlerDescription($data['handler']);
431452
$logger->debug("Registered manual resource {$name} from handler {$handlerDesc}");
432453
} catch (\Throwable $e) {
433454
$logger->error(
@@ -463,9 +484,7 @@ private function registerCapabilities(
463484
$completionProviders = $this->getCompletionProviders($reflection);
464485
$registry->registerResourceTemplate($template, $data['handler'], $completionProviders, true);
465486

466-
$handlerDesc = $data['handler'] instanceof \Closure ? 'Closure' : (\is_array(
467-
$data['handler'],
468-
) ? implode('::', $data['handler']) : $data['handler']);
487+
$handlerDesc = $this->getHandlerDescription($data['handler']);
469488
$logger->debug("Registered manual template {$name} from handler {$handlerDesc}");
470489
} catch (\Throwable $e) {
471490
$logger->error(
@@ -517,9 +536,7 @@ private function registerCapabilities(
517536
$completionProviders = $this->getCompletionProviders($reflection);
518537
$registry->registerPrompt($prompt, $data['handler'], $completionProviders, true);
519538

520-
$handlerDesc = $data['handler'] instanceof \Closure ? 'Closure' : (\is_array(
521-
$data['handler'],
522-
) ? implode('::', $data['handler']) : $data['handler']);
539+
$handlerDesc = $this->getHandlerDescription($data['handler']);
523540
$logger->debug("Registered manual prompt {$name} from handler {$handlerDesc}");
524541
} catch (\Throwable $e) {
525542
$logger->error(
@@ -533,6 +550,28 @@ private function registerCapabilities(
533550
$logger->debug('Manual element registration complete.');
534551
}
535552

553+
/**
554+
* @param Handler $handler
555+
*/
556+
private function getHandlerDescription(\Closure|array|string $handler): string
557+
{
558+
if ($handler instanceof \Closure) {
559+
return 'Closure';
560+
}
561+
562+
if (\is_array($handler)) {
563+
return \sprintf('%s::%s',
564+
\is_object($handler[0]) ? $handler[0]::class : $handler[0],
565+
$handler[1],
566+
);
567+
}
568+
569+
return (string) $handler;
570+
}
571+
572+
/**
573+
* @return array<string, ProviderInterface>
574+
*/
536575
private function getCompletionProviders(\ReflectionMethod|\ReflectionFunction $reflection): array
537576
{
538577
$completionProviders = [];

0 commit comments

Comments
 (0)