Skip to content

[MCP SDK] Use foreach instead of array_map because array_map does not accept generators #143

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dorrogeray
Copy link

Q A
Bug fix? yes
New feature? no
Docs? no
Issues N/A
License MIT

Use foreach instead of array_map because array_map does not accept generators and add test coverage.

@dorrogeray dorrogeray force-pushed the fix-array-map-consuming-generators branch from 44750cc to 35c6c5b Compare July 17, 2025 07:12
@welcoMattic welcoMattic changed the title [mcp-sdk] Use foreach instead of array_map because array_map does not accept generators [McpSdk] Use foreach instead of array_map because array_map does not accept generators Jul 17, 2025
@OskarStark OskarStark added the MCP SDK Issues & PRs about the MCP SDK label Jul 17, 2025
@OskarStark OskarStark changed the title [McpSdk] Use foreach instead of array_map because array_map does not accept generators [MCP SDK] Use foreach instead of array_map because array_map does not accept generators Jul 17, 2025
Comment on lines +29 to +33
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
Copy link
Contributor

Choose a reason for hiding this comment

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

For me it would be totally fine to go with a real instance instead:

Suggested change
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
$collection = new PromptChain([]);

Comment on lines +48 to +52
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here:

Suggested change
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
$collection = new PromptChain($metadataList);

Comment on lines +76 to +80
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
Copy link
Contributor

Choose a reason for hiding this comment

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

And here:

Suggested change
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
$collection = new PromptChain([$item, $item]);

Copy link
Contributor

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

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

Looks quite good to me and fixes #123, right?

Thanks for providing the tests as well - I'm more in the corner of avoiding mocks for simple cases, that's why I left some comments there.

@Nyholm looks good?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MCP SDK Issues & PRs about the MCP SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants