Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix static type error in REST API endpoint and add Generic.PHP.RequireStrictTypes to PHPCS ruleset#2436

Merged
westonruter merged 9 commits into
trunkfrom
add/strict-types-phpcs
Apr 3, 2026
Merged

Fix static type error in REST API endpoint and add Generic.PHP.RequireStrictTypes to PHPCS ruleset#2436
westonruter merged 9 commits into
trunkfrom
add/strict-types-phpcs

Conversation

@westonruter

@westonruter westonruter commented Apr 2, 2026

Copy link
Copy Markdown
Member

Summary

See #2348.

This is a follow-up to #2424.

This PR ensures that new PHP files (aside from tests) will fail PHPCS if declare( strict_types = 1 ); is absent.

This also fixes an error I encountered when attempting to store a new URL Metric via the REST API:

PHP Fatal error:  Uncaught TypeError: od_verify_url_metrics_storage_hmac(): Argument #5 ($cache_purge_post_id) must be of type ?int, string given, called in /var/www/src/wp-content/plugins/performance/plugins/optimization-detective/storage/class-od-rest-url-metrics-store-endpoint.php on line 91 and defined in /var/www/src/wp-content/plugins/performance/plugins/optimization-detective/storage/data.php:295
Stack trace:
#0 /var/www/src/wp-content/plugins/performance/plugins/optimization-detective/storage/class-od-rest-url-metrics-store-endpoint.php(91): od_verify_url_metrics_storage_hmac('74b5101d6b3d321...', '6c9843f1487a1bb...', 'd6d7c1f27cb4884...', 'http://localhos...', '442')
#1 /var/www/src/wp-includes/rest-api/class-wp-rest-request.php(930): OD_REST_URL_Metrics_Store_Endpoint::{closure}('74b5101d6b3d321...', Object(WP_REST_Request), 'hmac')
#2 /var/www/src/wp-includes/rest-api/class-wp-rest-server.php(1109): WP_REST_Request->has_valid_params()
#3 /var/www/src/wp-includes/rest-api/class-wp-rest-server.php(435): WP_REST_Server->dispatch(Object(WP_REST_Request))
#4 /var/www/src/wp-includes/rest-api.php(478): WP_REST_Server->serve_request('/optimization-d...')
#5 /var/www/src/wp-includes/class-wp-hook.php(341): rest_api_loaded(Object(WP))
#6 /var/www/src/wp-includes/class-wp-hook.php(365): WP_Hook->apply_filters('', Array)
#7 /var/www/src/wp-includes/plugin.php(570): WP_Hook->do_action(Array)
#8 /var/www/src/wp-includes/class-wp.php(418): do_action_ref_array('parse_request', Array)
#9 /var/www/src/wp-includes/class-wp.php(821): WP->parse_request('')
#10 /var/www/src/wp-includes/functions.php(1343): WP->main('')
#11 /var/www/src/wp-blog-header.php(16): wp()
#12 /var/www/src/_index.php(17): require('/var/www/src/wp...')
#13 /var/www/src/index.php(19): require_once('/var/www/src/_i...')
#14 {main}
  thrown in /var/www/src/wp-content/plugins/performance/plugins/optimization-detective/storage/data.php on line 295

This is fixed in 563764d. The issue is that when a validate_callback obtains the REST API params, no validation or sanitization is applied. So the string value is passed through. But it needs to be enforced as int<1, max>|null. So this is now fixed.

AI Use

I prompted Claude to help me debug issues with wp-env, PHPUnit, and tests.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature Infrastructure Issues for the overall performance plugin infrastructure no milestone PRs that do not have a defined milestone for release skip changelog PRs that should not be mentioned in changelogs labels Apr 2, 2026
@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: b1ink0 <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

westonruter and others added 3 commits April 2, 2026 13:04
Cast query params to strings in create_request() since real HTTP
requests always deliver parameter values as strings. Also add test
cases for invalid cache_purge_post_id values (negative, zero,
non-numeric string).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…st scripts

The wp-env Docker container has PHPUnit 10.5 installed globally (at
~/.composer/vendor/bin/phpunit), while the project requires PHPUnit 8.5
(in vendor/bin/phpunit). When Composer runs the `test` script, it prepends
vendor/bin to PATH, so the local phpunit should take precedence. However,
Docker Desktop on macOS does not preserve the execute bit on files mapped
into the container via the wp-env `.wp-env.json` mappings config. This
causes vendor/bin/phpunit to be non-executable inside the container
(-rw-r--r-- instead of -rwxr-xr-x), so Composer silently falls back to
the global PHPUnit 10.5 which does not support the --verbose flag and has
other incompatibilities with the project's test configuration.

By using `php vendor/bin/phpunit` explicitly, we bypass the execute
permission issue entirely and ensure the correct project-local PHPUnit
version is always used regardless of Docker's permission mapping behavior.

Note: the `.wp-env.json` "mappings" for the tests container copies files
into a Docker volume rather than creating a live bind mount. After changing
composer.json (or any mapped file), you must run `npx wp-env start --update`
for the container to pick up the changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The manual rest_validate_value_from_schema() call fails when
cache_purge_post_id is not provided because $request['...'] returns
null, which doesn't satisfy type 'integer'. The REST API's own
validation skips absent optional params, but this manual call doesn't
know the param is absent vs explicitly null. Add a null guard to match
the REST API's behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@westonruter westonruter added the [Plugin] Optimization Detective Issues for the Optimization Detective plugin label Apr 2, 2026
@westonruter westonruter changed the title Add Generic.PHP.RequireStrictTypes to PHPCS ruleset Fix static type error in Optimization Detective and add Generic.PHP.RequireStrictTypes to PHPCS ruleset Apr 2, 2026
@codecov

codecov Bot commented Apr 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.33%. Comparing base (58c9063) to head (aa8c2e4).
⚠️ Report is 10 commits behind head on trunk.

Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #2436   +/-   ##
=======================================
  Coverage   69.33%   69.33%           
=======================================
  Files          90       90           
  Lines        7749     7749           
=======================================
  Hits         5373     5373           
  Misses       2376     2376           
Flag Coverage Δ
multisite 69.33% <100.00%> (ø)
single 35.73% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@westonruter westonruter requested a review from b1ink0 April 2, 2026 21:50
Comment thread composer.json
Comment on lines -99 to +101
"test": "phpunit --strict-coverage",
"test": "php vendor/bin/phpunit --strict-coverage",
"test-multisite": [
"WP_MULTISITE=1 phpunit --exclude-group=ms-excluded"
"WP_MULTISITE=1 php vendor/bin/phpunit --exclude-group=ms-excluded"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See 654d69b

@westonruter westonruter changed the title Fix static type error in Optimization Detective and add Generic.PHP.RequireStrictTypes to PHPCS ruleset Fix static type error in REST API endpoint and add Generic.PHP.RequireStrictTypes to PHPCS ruleset Apr 3, 2026
@westonruter westonruter added [Type] Bug An existing feature is broken and removed no milestone PRs that do not have a defined milestone for release skip changelog PRs that should not be mentioned in changelogs Infrastructure Issues for the overall performance plugin infrastructure [Type] Enhancement A suggestion for improvement of an existing feature labels Apr 3, 2026

@b1ink0 b1ink0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@westonruter westonruter merged commit 625a764 into trunk Apr 3, 2026
45 checks passed
@westonruter westonruter deleted the add/strict-types-phpcs branch April 3, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Bug An existing feature is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants