Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend parameter description pattern to stop for single line comment #145

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => '',
];
}
}