Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 73 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,76 @@ 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' => '/** @param string $myParam The description
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using php multilingual variable for readability here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Oskar. But, do you mean something like the nowdoc format? Otherwise i would need an explanation for php multilingual variable because i am not aware of this wording for something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preeeetty sure he meant multiline not multilingual :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry 🙈 autocorrection...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe. Yeah. Always hard to teach the smartphone. I have changed the texts to a nowdoc format. This should improve the readability.

* @return void
*/',
'expectedResult' => 'The description',
];

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

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