Guard WP_Router::$request before use#589
Guard WP_Router::$request before use#589arifulhoque7 wants to merge 1 commit intoweDevsOfficial:developfrom
Conversation
Add a check that WP_Router::$request is truthy before calling get_query_params() and accessing the 'with' query param. This prevents fatal errors when the static request property is not initialized; the same guard is applied in both locations where parseIncludes is called.
WalkthroughTwo methods in the Transformer Manager trait now include defensive null checks before accessing the request query parameters. The update guards against potential null reference errors when Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/Common/Traits/Transformer_Manager.php (1)
13-46: Consider extracting duplicate manager setup logic.Both
get_response()andget_json_response()share identical manager initialization and include-parsing logic. Extracting this to a private helper method would reduce duplication and ensure the guard is maintained in one place.♻️ Optional refactor to reduce duplication
+ private function create_manager() { + $manager = new Manager(); + $manager->setSerializer( new DataArraySerializer() ); + + if (WP_Router::$request && isset(WP_Router::$request->get_query_params()['with'])) { + $manager->parseIncludes(sanitize_text_field(wp_unslash(WP_Router::$request->get_query_params()['with']))); + } + + return $manager; + } + public function get_response( $resource, $extra = [] ) { - $manager = new Manager(); - $manager->setSerializer( new DataArraySerializer() ); - - if (WP_Router::$request && isset(WP_Router::$request->get_query_params()['with'])) { - $manager->parseIncludes(sanitize_text_field(wp_unslash(WP_Router::$request->get_query_params()['with']))); - } + $manager = $this->create_manager(); if ($resource) { $response = $manager->createData( $resource )->toArray(); - } else { $response = []; } return array_merge( $extra, $response ); } public function get_json_response( $resource, $extra = [] ) { - $manager = new Manager(); - $manager->setSerializer( new DataArraySerializer() ); - - - if (WP_Router::$request && isset(WP_Router::$request->get_query_params()['with'])) { - $manager->parseIncludes(sanitize_text_field(wp_unslash(WP_Router::$request->get_query_params()['with']))); - } + $manager = $this->create_manager(); if ($resource) { $response = $manager->createData( $resource )->toArray(); } else { $response = []; } return json_encode( array_merge( $extra, $response ) ); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/Common/Traits/Transformer_Manager.php` around lines 13 - 46, Extract the duplicated Manager initialization and include-parsing into a private helper (e.g., init_manager_with_includes()) used by both get_response() and get_json_response(): move creation of new Manager(), setSerializer(new DataArraySerializer()), and the WP_Router::$request guard + parseIncludes(sanitize_text_field(wp_unslash(...))) into that helper and return the configured Manager instance; then call that helper from get_response() and get_json_response() before calling createData($resource)->toArray() so the include-guard logic lives in one place.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/Common/Traits/Transformer_Manager.php`:
- Around line 13-46: Extract the duplicated Manager initialization and
include-parsing into a private helper (e.g., init_manager_with_includes()) used
by both get_response() and get_json_response(): move creation of new Manager(),
setSerializer(new DataArraySerializer()), and the WP_Router::$request guard +
parseIncludes(sanitize_text_field(wp_unslash(...))) into that helper and return
the configured Manager instance; then call that helper from get_response() and
get_json_response() before calling createData($resource)->toArray() so the
include-guard logic lives in one place.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: e4cf2c36-0f18-4240-8cb0-4ca210f57711
📒 Files selected for processing (1)
src/Common/Traits/Transformer_Manager.php
PR Description Close 304
Related PRO PR
Summary
This PR improves the invoice module functionality across the PM Pro plugin by enhancing data transformation, shortcode handling, and frontend script management.
Changes Made
1. Invoice.php (Main Module)
2. Invoice Shortcode (includes/shortcodes/Invoice.php)
3. Invoice Transformer (Src/Transformers/Invoice_Transformer.php)
4. Vue Component Mixin (views/assets/src/helpers/mixin/mixin.js)
5. Transformer Manager Trait (Transformer_Manager.php)
6. Compiled Assets (invoice.js)
Impact
Files Modified
wp-content/plugins/pm-pro/modules/Invoice/Invoice.phpwp-content/plugins/pm-pro/modules/Invoice/includes/shortcodes/Invoice.phpwp-content/plugins/pm-pro/modules/Invoice/Src/Transformers/Invoice_Transformer.phpwp-content/plugins/pm-pro/modules/Invoice/views/assets/src/helpers/mixin/mixin.jswp-content/plugins/pm-pro/modules/Invoice/views/assets/js/invoice.jswp-content/plugins/wedevs-project-manager/src/Common/Traits/Transformer_Manager.phpTesting
Related Issues
Summary by CodeRabbit