Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 67b108b

Browse files
authored
fix: Extend parameter description pattern to stop for single line comment (#145)
* Add test cases to parameter analyzer for getParameterDescription * Extend parameter description pattern to stop for single line comment * Switch parameter test provider cases to nowdoc format for multilines
1 parent 2507ce1 commit 67b108b

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

src/ToolBox/ParameterAnalyzer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private function getParameterDescription(\ReflectionMethod $method, string $para
9595
return '';
9696
}
9797

98-
$pattern = '/@param\s+\S+\s+\$'.preg_quote($paramName, '/').'\s+(.*)/';
98+
$pattern = '/@param\s+\S+\s+\$'.preg_quote($paramName, '/').'\s+((.*)(?=\*)|.*)/';
9999
if (preg_match($pattern, $docComment, $matches)) {
100100
return trim($matches[1]);
101101
}

tests/ToolBox/ParameterAnalyzerTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpLlm\LlmChain\ToolBox\Metadata;
1414
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
1515
use PHPUnit\Framework\Attributes\CoversClass;
16+
use PHPUnit\Framework\Attributes\DataProvider;
1617
use PHPUnit\Framework\Attributes\Test;
1718
use PHPUnit\Framework\Attributes\UsesClass;
1819
use PHPUnit\Framework\TestCase;
@@ -163,4 +164,83 @@ public function detectParameterDefinitionNone(): void
163164

164165
self::assertNull($actual);
165166
}
167+
168+
#[Test]
169+
public function getParameterDescriptionWithoutDocBlock(): void
170+
{
171+
$targetMethod = self::createStub(\ReflectionMethod::class);
172+
$targetMethod->method('getDocComment')->willReturn(false);
173+
174+
$methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription');
175+
176+
self::assertSame(
177+
'',
178+
$methodToTest->invoke(
179+
$this->analyzer,
180+
$targetMethod,
181+
'myParam',
182+
)
183+
);
184+
}
185+
186+
#[Test]
187+
#[DataProvider('provideGetParameterDescriptionCases')]
188+
public function getParameterDescriptionWithDocs(string $docComment, string $expectedResult): void
189+
{
190+
$targetMethod = self::createStub(\ReflectionMethod::class);
191+
$targetMethod->method('getDocComment')->willReturn($docComment);
192+
193+
$methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription');
194+
195+
self::assertSame(
196+
$expectedResult,
197+
$methodToTest->invoke(
198+
$this->analyzer,
199+
$targetMethod,
200+
'myParam',
201+
)
202+
);
203+
}
204+
205+
public static function provideGetParameterDescriptionCases(): \Generator
206+
{
207+
yield 'empty doc block' => [
208+
'docComment' => '',
209+
'expectedResult' => '',
210+
];
211+
212+
yield 'single line doc block with description' => [
213+
'docComment' => '/** @param string $myParam The description */',
214+
'expectedResult' => 'The description',
215+
];
216+
217+
yield 'multi line doc block with description and other tags' => [
218+
'docComment' => <<<'TEXT'
219+
/**
220+
* @param string $myParam The description
221+
* @return void
222+
*/
223+
TEXT,
224+
'expectedResult' => 'The description',
225+
];
226+
227+
yield 'multi line doc block with multiple parameters' => [
228+
'docComment' => <<<'TEXT'
229+
/**
230+
* @param string $myParam The description
231+
* @param string $anotherParam The wrong description
232+
*/
233+
TEXT,
234+
'expectedResult' => 'The description',
235+
];
236+
237+
yield 'multi line doc block with parameter that is not searched for' => [
238+
'docComment' => <<<'TEXT'
239+
/**
240+
* @param string $unknownParam The description
241+
*/
242+
TEXT,
243+
'expectedResult' => '',
244+
];
245+
}
166246
}

0 commit comments

Comments
 (0)