Skip to content

Commit baf06f3

Browse files
authored
[Server] Drop null from DocBlockParser::parseDocBlock() (#633)
* Drop no-op `?? null` on `getDocComment()` flagged by PHPStan 2.3.3 * Narrow `DocBlockParser::parseDocBlock()` to `string|false`
1 parent 39794ee commit baf06f3

7 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to `mcp/sdk` will be documented in this file.
44

5+
0.10.0
6+
------
7+
8+
### Server
9+
10+
* [BC Break] `DocBlockParser::parseDocBlock()` no longer accepts `null`; pass `ReflectionMethod::getDocComment()` as is
11+
512
0.9.0
613
-----
714

‎src/Capability/Discovery/Discoverer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
261261

262262
++$discoveredCount['resources'];
263263
} elseif ($instance instanceof McpPrompt) {
264-
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment() ?? null);
264+
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment());
265265
$name = ElementMetadataResolver::resolveName($method, $instance->name);
266266
$description = ElementMetadataResolver::resolveDescription($method, $instance->description, $this->docBlockParser);
267267
$arguments = [];

‎src/Capability/Discovery/DocBlockParser.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public function __construct(
3737
/**
3838
* Safely parses a DocComment string into a DocBlock object.
3939
*/
40-
public function parseDocBlock(string|false|null $docComment): ?DocBlock
40+
public function parseDocBlock(string|false $docComment): ?DocBlock
4141
{
42-
if (false === $docComment || null === $docComment || empty($docComment)) {
42+
if (false === $docComment || '' === $docComment) {
4343
return null;
4444
}
4545
try {

‎src/Capability/Discovery/ElementMetadataResolver.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ public static function resolveName(\ReflectionMethod $method, ?string $name): st
3939
*/
4040
public static function resolveDescription(\ReflectionMethod $method, ?string $description, DocBlockParser $docBlockParser): ?string
4141
{
42-
return $description ?? $docBlockParser->getDescription($docBlockParser->parseDocBlock($method->getDocComment() ?? null));
42+
return $description ?? $docBlockParser->getDescription($docBlockParser->parseDocBlock($method->getDocComment()));
4343
}
4444
}

‎src/Capability/Discovery/SchemaGenerator.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,7 @@ private function applyArrayConstraints(array $paramSchema, array $paramInfo): ar
514514
*/
515515
private function parseParametersInfo(\ReflectionMethod|\ReflectionFunction $reflection): array
516516
{
517-
$docComment = $reflection->getDocComment() ?: null;
518-
$docBlock = $this->docBlockParser->parseDocBlock($docComment);
517+
$docBlock = $this->docBlockParser->parseDocBlock($reflection->getDocComment());
519518
$paramTags = $this->docBlockParser->getParamTags($docBlock);
520519
$parametersInfo = [];
521520

‎src/Capability/Registry/Loader/ReflectedElementLoader.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function load(RegistryInterface $registry): void
228228

229229
$arguments = [];
230230
$paramTags = $reflection instanceof \ReflectionMethod ? $docBlockParser->getParamTags(
231-
$docBlockParser->parseDocBlock($reflection->getDocComment() ?? null),
231+
$docBlockParser->parseDocBlock($reflection->getDocComment()),
232232
) : [];
233233
foreach ($reflection->getParameters() as $param) {
234234
$reflectionType = $param->getType();

‎tests/Unit/Capability/Discovery/DocBlockParserTest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,21 @@ protected function setUp(): void
3232
public function testGetDescriptionReturnsCorrectDescription(): void
3333
{
3434
$method = new \ReflectionMethod(DocBlockTestFixture::class, 'methodWithSummaryAndDescription');
35-
$docComment = $method->getDocComment() ?: null;
35+
$docComment = $method->getDocComment();
3636
$docBlock = $this->parser->parseDocBlock($docComment);
3737
$expectedDesc = "Summary line here.\n\nThis is a longer description spanning\nmultiple lines.\nIt might contain *markdown* or `code`.";
3838
$this->assertEquals($expectedDesc, $this->parser->getDescription($docBlock));
3939

4040
$method2 = new \ReflectionMethod(DocBlockTestFixture::class, 'methodWithSummaryOnly');
41-
$docComment2 = $method2->getDocComment() ?: null;
41+
$docComment2 = $method2->getDocComment();
4242
$docBlock2 = $this->parser->parseDocBlock($docComment2);
4343
$this->assertEquals('Simple summary line.', $this->parser->getDescription($docBlock2));
4444
}
4545

4646
public function testGetParamTagsReturnsStructuredParamInfo(): void
4747
{
4848
$method = new \ReflectionMethod(DocBlockTestFixture::class, 'methodWithParams');
49-
$docComment = $method->getDocComment() ?: null;
49+
$docComment = $method->getDocComment();
5050
$docBlock = $this->parser->parseDocBlock($docComment);
5151
$params = $this->parser->getParamTags($docBlock);
5252

@@ -97,7 +97,7 @@ public function testGetParamTagsReturnsStructuredParamInfo(): void
9797
public function testGetTagsByNameReturnsSpecificTags(): void
9898
{
9999
$method = new \ReflectionMethod(DocBlockTestFixture::class, 'methodWithMultipleTags');
100-
$docComment = $method->getDocComment() ?: null;
100+
$docComment = $method->getDocComment();
101101
$docBlock = $this->parser->parseDocBlock($docComment);
102102

103103
$this->assertInstanceOf(DocBlock::class, $docBlock);
@@ -121,7 +121,7 @@ public function testGetTagsByNameReturnsSpecificTags(): void
121121
public function testHandlesMethodWithNoDocblockGracefully(): void
122122
{
123123
$method = new \ReflectionMethod(DocBlockTestFixture::class, 'methodWithNoDocBlock');
124-
$docComment = $method->getDocComment() ?: null;
124+
$docComment = $method->getDocComment();
125125
$docBlock = $this->parser->parseDocBlock($docComment);
126126

127127
$this->assertNull($docBlock);

0 commit comments

Comments
 (0)