Skip to content

Commit

Permalink
fix: Extend parameter description pattern to stop for single line com…
Browse files Browse the repository at this point in the history
…ment (#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
  • Loading branch information
DZunke authored Nov 21, 2024
1 parent 2507ce1 commit 67b108b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ToolBox/ParameterAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function getParameterDescription(\ReflectionMethod $method, string $para
return '';
}

$pattern = '/@param\s+\S+\s+\$'.preg_quote($paramName, '/').'\s+(.*)/';
$pattern = '/@param\s+\S+\s+\$'.preg_quote($paramName, '/').'\s+((.*)(?=\*)|.*)/';
if (preg_match($pattern, $docComment, $matches)) {
return trim($matches[1]);
}
Expand Down
80 changes: 80 additions & 0 deletions tests/ToolBox/ParameterAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpLlm\LlmChain\ToolBox\Metadata;
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -163,4 +164,83 @@ public function detectParameterDefinitionNone(): void

self::assertNull($actual);
}

#[Test]
public function getParameterDescriptionWithoutDocBlock(): void
{
$targetMethod = self::createStub(\ReflectionMethod::class);
$targetMethod->method('getDocComment')->willReturn(false);

$methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription');

self::assertSame(
'',
$methodToTest->invoke(
$this->analyzer,
$targetMethod,
'myParam',
)
);
}

#[Test]
#[DataProvider('provideGetParameterDescriptionCases')]
public function getParameterDescriptionWithDocs(string $docComment, string $expectedResult): void
{
$targetMethod = self::createStub(\ReflectionMethod::class);
$targetMethod->method('getDocComment')->willReturn($docComment);

$methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription');

self::assertSame(
$expectedResult,
$methodToTest->invoke(
$this->analyzer,
$targetMethod,
'myParam',
)
);
}

public static function provideGetParameterDescriptionCases(): \Generator
{
yield 'empty doc block' => [
'docComment' => '',
'expectedResult' => '',
];

yield 'single line doc block with description' => [
'docComment' => '/** @param string $myParam The description */',
'expectedResult' => 'The description',
];

yield 'multi line doc block with description and other tags' => [
'docComment' => <<<'TEXT'
/**
* @param string $myParam The description
* @return void
*/
TEXT,
'expectedResult' => 'The description',
];

yield 'multi line doc block with multiple parameters' => [
'docComment' => <<<'TEXT'
/**
* @param string $myParam The description
* @param string $anotherParam The wrong description
*/
TEXT,
'expectedResult' => 'The description',
];

yield 'multi line doc block with parameter that is not searched for' => [
'docComment' => <<<'TEXT'
/**
* @param string $unknownParam The description
*/
TEXT,
'expectedResult' => '',
];
}
}

0 comments on commit 67b108b

Please sign in to comment.