Skip to content

Add declare( strict_types = 1 ) to all PHP files#2424

Merged
westonruter merged 13 commits intoWordPress:trunkfrom
meravi:add-strict-types
Apr 2, 2026
Merged

Add declare( strict_types = 1 ) to all PHP files#2424
westonruter merged 13 commits intoWordPress:trunkfrom
meravi:add-strict-types

Conversation

@meravi
Copy link
Copy Markdown
Contributor

@meravi meravi commented Mar 20, 2026

Adds declare( strict_types = 1 ); to all PHP source files across the monorepo.
Fixes #2348

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 20, 2026

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: meravi <ravikhadka@git.wordpress.org>
Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

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

@meravi
Copy link
Copy Markdown
Contributor Author

meravi commented Mar 24, 2026

@westonruter would appreciate a quick look when you have a moment 🙏

Comment thread performance.php Outdated
@@ -1,4 +1,5 @@
<?php
declare( strict_types = 1 );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should be after the plugin info.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks! Moved it after the plugin header.

@westonruter westonruter added the [Type] Enhancement A suggestion for improvement of an existing feature label Mar 24, 2026
@westonruter westonruter added this to the performance-lab n.e.x.t milestone Mar 24, 2026
Comment thread performance.php Outdated
* @package performance
*/

declare( strict_types = 1 );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
declare( strict_types = 1 );
declare( strict_types = 1 );

Copy link
Copy Markdown
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

In addition to the trivial PHPCS issue which I left a suggestion for, there are unit test failures related to the new strict types. This will need to be fixed.

@westonruter
Copy link
Copy Markdown
Member

@meravi why remove the strict types? The underlying type problem should be fixed so the strict types can exist in all the files.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.33%. Comparing base (404dcd1) to head (849447a).
⚠️ Report is 19 commits behind head on trunk.

Additional details and impacted files
@@           Coverage Diff           @@
##            trunk    #2424   +/-   ##
=======================================
  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% <100.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.

@meravi
Copy link
Copy Markdown
Contributor Author

meravi commented Mar 25, 2026

I reverted it temporarily for testing, but it’s added back now.
@westonruter

@meravi meravi requested a review from westonruter March 25, 2026 08:50
@westonruter
Copy link
Copy Markdown
Member

@meravi ok, you can see that the unit tests are still failing. Once they are passing this will be ready for review.

@westonruter westonruter marked this pull request as draft March 25, 2026 22:54
@meravi
Copy link
Copy Markdown
Contributor Author

meravi commented Mar 26, 2026

@meravi ok, you can see that the unit tests are still failing. Once they are passing this will be ready for review.

I’ve fixed the issues, and the unit tests are now passing 🟢
Ready for review when you have a moment :)

Comment thread plugins/performance-lab/includes/site-health/audit-autoloaded-options/helper.php Outdated
Comment thread plugins/performance-lab/includes/site-health/audit-autoloaded-options/helper.php Outdated
Comment thread plugins/performance-lab/includes/site-health/audit-autoloaded-options/helper.php Outdated
…options/helper.php

Co-authored-by: Weston Ruter <westonruter@gmail.com>
meravi and others added 3 commits March 26, 2026 10:53
…options/helper.php

Co-authored-by: Weston Ruter <westonruter@gmail.com>
…options/helper.php

Co-authored-by: Weston Ruter <westonruter@gmail.com>
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review March 26, 2026 05:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables PHP strict typing across the Performance Lab monorepo by adding declare( strict_types = 1 ); at the top of PHP source files, aligning the codebase with the stricter type-safety expectations already implied by PHPStan level 8 (Issue #2348).

Changes:

  • Added declare( strict_types = 1 ); to PHP plugin entrypoints, helpers, and includes across multiple plugins.
  • Updated the autoloaded-options Site Health helper to ensure option values are stringified before calling strlen() (to avoid type errors with non-string cached values).

Reviewed changes

Copilot reviewed 102 out of 102 changed files in this pull request and generated no comments.

Show a summary per file
File Description
plugins/webp-uploads/uninstall.php Add strict_types declaration.
plugins/webp-uploads/settings.php Add strict_types declaration.
plugins/webp-uploads/rest-api.php Add strict_types declaration.
plugins/webp-uploads/picture-element.php Add strict_types declaration.
plugins/webp-uploads/load.php Add strict_types declaration.
plugins/webp-uploads/image-edit.php Add strict_types declaration.
plugins/webp-uploads/hooks.php Add strict_types declaration.
plugins/webp-uploads/helper.php Add strict_types declaration.
plugins/webp-uploads/deprecated.php Add strict_types declaration.
plugins/web-worker-offloading/third-party/woocommerce.php Add strict_types declaration.
plugins/web-worker-offloading/third-party/seo-by-rank-math.php Add strict_types declaration.
plugins/web-worker-offloading/third-party/google-site-kit.php Add strict_types declaration.
plugins/web-worker-offloading/third-party.php Add strict_types declaration.
plugins/web-worker-offloading/load.php Add strict_types declaration.
plugins/web-worker-offloading/hooks.php Add strict_types declaration.
plugins/web-worker-offloading/helper.php Add strict_types declaration.
plugins/view-transitions/view-transitions.php Add strict_types declaration.
plugins/view-transitions/uninstall.php Add strict_types declaration.
plugins/view-transitions/includes/theme.php Add strict_types declaration.
plugins/view-transitions/includes/settings.php Add strict_types declaration.
plugins/view-transitions/includes/functions.php Add strict_types declaration.
plugins/view-transitions/includes/class-plvt-view-transition-animation.php Add strict_types declaration.
plugins/view-transitions/includes/class-plvt-view-transition-animation-registry.php Add strict_types declaration.
plugins/view-transitions/includes/admin.php Add strict_types declaration.
plugins/view-transitions/hooks.php Add strict_types declaration.
plugins/speculation-rules/wp-core-api.php Add strict_types declaration.
plugins/speculation-rules/uninstall.php Add strict_types declaration.
plugins/speculation-rules/settings.php Add strict_types declaration.
plugins/speculation-rules/plugin-api.php Add strict_types declaration.
plugins/speculation-rules/load.php Add strict_types declaration.
plugins/speculation-rules/hooks.php Add strict_types declaration.
plugins/speculation-rules/class-plsr-url-pattern-prefixer.php Add strict_types declaration.
plugins/performance-lab/load.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/webp-support/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/webp-support/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/load.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/effective-asset-cache-headers/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/effective-asset-cache-headers/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/bfcache-compatibility-headers/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/bfcache-compatibility-headers/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/avif-support/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/avif-support/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/avif-headers/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/avif-headers/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/audit-enqueued-assets/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/audit-enqueued-assets/helper.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/audit-autoloaded-options/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/site-health/audit-autoloaded-options/helper.php Add strict_types declaration and ensure non-string option values are serialized before strlen().
plugins/performance-lab/includes/server-timing/object-cache.copy.php Add strict_types declaration.
plugins/performance-lab/includes/server-timing/load.php Add strict_types declaration.
plugins/performance-lab/includes/server-timing/hooks.php Add strict_types declaration.
plugins/performance-lab/includes/server-timing/defaults.php Add strict_types declaration.
plugins/performance-lab/includes/server-timing/class-perflab-server-timing.php Add strict_types declaration.
plugins/performance-lab/includes/server-timing/class-perflab-server-timing-metric.php Add strict_types declaration.
plugins/performance-lab/includes/admin/server-timing.php Add strict_types declaration.
plugins/performance-lab/includes/admin/rest-api.php Add strict_types declaration.
plugins/performance-lab/includes/admin/plugins.php Add strict_types declaration.
plugins/performance-lab/includes/admin/load.php Add strict_types declaration.
plugins/optimization-detective/uninstall.php Add strict_types declaration.
plugins/optimization-detective/storage/data.php Add strict_types declaration.
plugins/optimization-detective/storage/class-od-url-metrics-post-type.php Add strict_types declaration.
plugins/optimization-detective/storage/class-od-url-metric-store-request-context.php Add strict_types declaration.
plugins/optimization-detective/storage/class-od-storage-lock.php Add strict_types declaration.
plugins/optimization-detective/storage/class-od-rest-url-metrics-store-endpoint.php Add strict_types declaration.
plugins/optimization-detective/site-health.php Add strict_types declaration.
plugins/optimization-detective/optimization.php Add strict_types declaration.
plugins/optimization-detective/load.php Add strict_types declaration.
plugins/optimization-detective/hooks.php Add strict_types declaration.
plugins/optimization-detective/helper.php Add strict_types declaration.
plugins/optimization-detective/detection.php Add strict_types declaration.
plugins/optimization-detective/deprecated.php Add strict_types declaration.
plugins/optimization-detective/class-od-visited-tag-state.php Add strict_types declaration.
plugins/optimization-detective/class-od-url-metric.php Add strict_types declaration.
plugins/optimization-detective/class-od-url-metric-group.php Add strict_types declaration.
plugins/optimization-detective/class-od-url-metric-group-collection.php Add strict_types declaration.
plugins/optimization-detective/class-od-template-optimization-context.php Add strict_types declaration.
plugins/optimization-detective/class-od-tag-visitor-registry.php Add strict_types declaration.
plugins/optimization-detective/class-od-tag-visitor-context.php Add strict_types declaration.
plugins/optimization-detective/class-od-strict-url-metric.php Add strict_types declaration.
plugins/optimization-detective/class-od-link-collection.php Add strict_types declaration.
plugins/optimization-detective/class-od-html-tag-processor.php Add strict_types declaration.
plugins/optimization-detective/class-od-element.php Add strict_types declaration.
plugins/optimization-detective/class-od-data-validation-exception.php Add strict_types declaration.
plugins/image-prioritizer/load.php Add strict_types declaration.
plugins/image-prioritizer/hooks.php Add strict_types declaration.
plugins/image-prioritizer/helper.php Add strict_types declaration.
plugins/image-prioritizer/class-image-prioritizer-video-tag-visitor.php Add strict_types declaration.
plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php Add strict_types declaration.
plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php Add strict_types declaration.
plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php Add strict_types declaration.
plugins/embed-optimizer/load.php Add strict_types declaration.
plugins/embed-optimizer/hooks.php Add strict_types declaration.
plugins/embed-optimizer/helper.php Add strict_types declaration.
plugins/embed-optimizer/class-embed-optimizer-tag-visitor.php Add strict_types declaration.
plugins/dominant-color-images/load.php Add strict_types declaration.
plugins/dominant-color-images/hooks.php Add strict_types declaration.
plugins/dominant-color-images/helper.php Add strict_types declaration.
plugins/dominant-color-images/class-dominant-color-image-editor-imagick.php Add strict_types declaration.
plugins/dominant-color-images/class-dominant-color-image-editor-gd.php Add strict_types declaration.
plugins/auto-sizes/hooks.php Add strict_types declaration.
plugins/auto-sizes/auto-sizes.php Add strict_types declaration.
performance.php Add strict_types declaration.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@westonruter
Copy link
Copy Markdown
Member

westonruter commented Apr 2, 2026

@mukeshpanchal27 Are you having trouble running PHPUnit locally?

I'm trying npm run test-php:auto-sizes and I see:

npm run test-php:auto-sizes
Protected by Socket Firewall

> test-php:auto-sizes
> wp-env run tests-cli --env-cwd=/var/www/html/wp-content/plugins/performance composer test:auto-sizes

ℹ Starting 'composer test:auto-sizes' on the tests-cli container. 

Composer could not detect the root package (wordpress/performance) version, defaulting to '1.0.0'. See https://getcomposer.org/root-version
PHPUnit 10.5.60 by Sebastian Bergmann and contributors.

Unknown option "--verbose"
Script phpunit --strict-coverage handling the test event returned with error code 2
Script @test --verbose --testsuite auto-sizes was called via test:auto-sizes
✖ Command failed with exit code 2

This used to work.


See #2437

@westonruter westonruter merged commit 58c9063 into WordPress:trunk Apr 2, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider declaring strict_types

4 participants