Encapsulation and type safety improvements#154
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## trunk #154 +/- ##
============================================
+ Coverage 87.76% 88.04% +0.27%
- Complexity 1234 1236 +2
============================================
Files 54 54
Lines 3996 4022 +26
============================================
+ Hits 3507 3541 +34
+ Misses 489 481 -8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR strengthens internal type safety and encapsulation across the MCP adapter by removing dynamic property assignment, tightening handler access, and adding validation + tests for McpTransportContext.
Changes:
- Added strict typing / native property types in domain + handlers (
declare(strict_types=1), typed static properties). - Made internal handler properties private (
McpServer,HttpRequestHandler) and updated call sites to use getters. - Reworked
McpTransportContextconstruction to validate required/unknown keys and added a new unit test suite for constructor validation.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Unit/Transport/Infrastructure/McpTransportContextTest.php | Adds constructor validation tests for McpTransportContext. |
| tests/Unit/Servers/DefaultServerFactoryTest.php | Updates assertions to use McpServer getters for handlers. |
| tests/Unit/McpServerTest.php | Updates to use get_error_handler() after handler encapsulation. |
| tests/Integration/ErrorHandlingIntegrationTest.php | Updates integration assertions to use get_error_handler(). |
| includes/Transport/Infrastructure/McpTransportContext.php | Replaces dynamic assignment with explicit validation + property assignment. |
| includes/Transport/Infrastructure/HttpRequestHandler.php | Makes transport context private and introduces get_transport_context(). |
| includes/Transport/HttpTransport.php | Updates call sites to use HttpRequestHandler::get_transport_context(). |
| includes/Handlers/Tools/ToolsHandler.php | Switches error logging to McpServer::get_error_handler(). |
| includes/Handlers/Resources/ResourcesHandler.php | Switches error logging to McpServer::get_error_handler(). |
| includes/Handlers/Prompts/PromptsHandler.php | Adds typed static properties; updates error/observability usage to getters. |
| includes/Domain/Prompts/RegisterAbilityAsMcpPrompt.php | Adds missing declare(strict_types=1). |
| includes/Core/McpServer.php | Makes handler properties private; documents get_error_handler(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tests/Unit/Transport/Infrastructure/McpTransportContextTest.php
Outdated
Show resolved
Hide resolved
Add missing declare(strict_types=1) to RegisterAbilityAsMcpPrompt.php — the only file in the codebase without it. Add explicit array and string type declarations to PromptsHandler static properties.
Make McpServer::$error_handler and ::$observability_handler private — these properties are not part of the frozen public API. All ~15 internal call sites updated to use the existing get_error_handler() and get_observability_handler() getters. Make HttpRequestHandler::$transport_context private and add a get_transport_context() getter. Update HttpTransport call sites.
Replace unsafe dynamic property assignment with explicit key validation. Missing required keys and unknown keys now throw InvalidArgumentException with descriptive messages. Prevents silent failures from typos and PHP 8.2+ dynamic property issues.
Default error_handler to the server's handler when not provided, preventing uninitialized typed property access on PHP 8.2+. Remove misleading `/* translators: */` comments from non-i18n strings.
Align McpTransportContextTest method names with the dominant codebase convention (955/986 methods use all-lowercase snake_case).
Cover 5 previously-uncovered diff lines to raise patch coverage from 86.79% to 96.23%. The three handler catch blocks are exercised via result filters that throw, simulating faulty third-party plugins. Two PromptsHandler edge cases (non-array message, missing content type) are covered by abilities returning malformed data.
7e015d8 to
d7519c4
Compare
Why
The v0.5.0 code review identified five internal structure issues that weaken type safety and encapsulation without serving any API purpose:
RegisterAbilityAsMcpPrompt.phpis the only file missingdeclare(strict_types=1), allowing silent type coercion in a domain layer class.McpTransportContextuses dynamic property assignment ($this->$name = $valuein a foreach loop), which silently accepts typos, creates dynamic properties on PHP 8.2+, and bypasses the type system entirely.McpServer::$error_handlerand::$observability_handlerare public despite having getter methods. The properties are not part of the frozen public API (the getters are), but ~15 internal call sites access them directly — inconsistent and mutable from outside.HttpRequestHandler::$transport_contextis public with no getter, forcingHttpTransportto reach into the handler's internals via$this->request_handler->transport_context.PromptsHandlerstatic properties lack typed declarations, relying on PHPDoc@varannotations instead of PHP's native type system.None of these touch the frozen public API surface. All changes are internal to the plugin.
What
Type safety (
RegisterAbilityAsMcpPrompt,PromptsHandler):declare(strict_types=1)to the only file missing itarrayandstringtype declarations toPromptsHandlerstatic propertiesEncapsulation (
McpServer,HttpRequestHandler):McpServer::$error_handlerand::$observability_handlerprivateget_error_handler()/get_observability_handler()HttpRequestHandler::$transport_contextprivate and addedget_transport_context()getterHttpTransportConstructor validation (
McpTransportContext):foreachassignment with explicit key validationInvalidArgumentExceptionlisting missing keysInvalidArgumentExceptionlisting offending keysTest plan
McpTransportContextkey validation