Skip to content

Fail gracefully when invalid field is supplied to --fields#210

Merged
swissspidy merged 5 commits intomainfrom
copilot/fail-gracefully-invalid-fields
Feb 26, 2026
Merged

Fail gracefully when invalid field is supplied to --fields#210
swissspidy merged 5 commits intomainfrom
copilot/fail-gracefully-invalid-fields

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Passing an invalid field name via --fields (e.g. wp profile stage template --fields=test) causes a PHP 8.1 fatal error due to unsupported operand types (string + null) in Formatter::show_table.

Root cause: When no valid field matches, array_search returns false. Because !is_null(false) is true, $totals[false] (which aliases $totals[0]) is set to the 'total (N)' string. The strict comparison false === 0 then fails to skip that index, and the subsequent $totals[$i] += null blows up in PHP 8.1.

Changes:

  • src/Formatter.php — Added validation in __construct that diffs user-supplied --fields values against the valid fields array. Calls WP_CLI::error() with a clear message (e.g. Error: Invalid field(s): test) before any arithmetic is attempted. Also moved array_map('trim', ...) before the array_search call so trimmed values are used consistently.
  • features/profile-stage.feature — Added a Behat scenario covering the invalid field case:
When I try `wp profile stage template --fields=test`
Then STDERR should contain:
  """
  Invalid field(s): test
  """
And the return code should be 1
Original prompt

This section details on the original issue you should resolve

<issue_title>Fail Gracefully if invalid field is supplied to --fields</issue_title>
<issue_description>## Bug Report

Describe the current, buggy behavior

When running wp profile stage ... and you provide an invalid field to display, it will Fatal Error.

The error is:

PHP Fatal error:  Uncaught TypeError: Unsupported operand types: string + null in [removed]/.wp-cli/packages/vendor/wp-cli/profile-command/src/Formatter.php:134
Stack trace:
wp-cli/profile-command#0 [removed]/.wp-cli/packages/vendor/wp-cli/profile-command/src/Formatter.php(47): WP_CLI\Profile\Formatter->show_table('ASC', NULL, Array, Array, true)
wp-cli/profile-command#1 [removed]/.wp-cli/packages/vendor/wp-cli/profile-command/src/Command.php(187): WP_CLI\Profile\Formatter->display_items(Array, true, 'ASC', NULL)
wp-cli/profile-command#2 [internal function]: WP_CLI\Profile\Command->stage(Array, Array)
wp-cli/profile-command#3 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(100): call_user_func(Array, Array, Array)
wp-cli/profile-command#4 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
wp-cli/profile-command#5 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(497): call_user_func(Object(Closure), Array, Array)
wp-cli/profile-command#6 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(470): WP_CLI\Dispatcher\Subcommand->invoke(Array, Array, Array)
wp-cli/profile-command#7 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(493): WP_CLI\Runner->run_command(Array, Array)
wp-cli/profile-command#8 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(136): WP_CLI\Runner->run_command_and_exit()
wp-cli/profile-command#9 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1228): WP_CLI\Runner->do_early_invoke('before_wp_load')
wp-cli/profile-command#10 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
wp-cli/profile-command#11 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/bootstrap.php(84): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
wp-cli/profile-command#12 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/wp-cli.php(35): WP_CLI\bootstrap()
wp-cli/profile-command#13 phar:///usr/local/bin/wp-cli/php/boot-phar.php(20): include('phar:///usr/loc...')
wp-cli/profile-command#14 /usr/local/bin/wp-cli(4): include('phar:///usr/loc...')
wp-cli/profile-command#15 {main}
  thrown in [removed]/.wp-cli/packages/vendor/wp-cli/profile-command/src/Formatter.php on line 134

Describe how other contributors can replicate this bug

Run any wp profile stage command with an invalid field supplied to --fields: wp profile stage template --fields=test will result in the above error.

Describe what you would expect as the correct outcome

Expected behavior is to either:

  1. Fail gracefully and ignore. in the case of the example command, it should treat it as if no --fields was supplied. In the case that there are valid fields, the invalid field should just be ignored.

OR

  1. Provide a meaningful failure message eg. field 'test' is invalid.

Let us know what environment you are running this on

OS:     Linux 5.15.167.4-microsoft-standard-WSL2 wp-cli/profile-command#1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64
Shell:
PHP binary:     /usr/bin/php8.1
PHP version:    8.1.32
php.ini used:   /etc/php/8.1/cli/php.ini
MySQL binary:   /usr/bin/mariadb
MySQL version:  mariadb  Ver 15.1 Distrib 10.11.11-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:      STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       phar:///usr/local/bin/wp-cli
WP-CLI packages dir:    [removed]/.wp-cli/packages/
WP-CLI cache dir:       [removed]/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.12.0

</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…supplied

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix graceful failure for invalid field in --fields Fail gracefully when invalid field is supplied to --fields Feb 25, 2026
@codecov
Copy link

codecov bot commented Feb 25, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Formatter.php 90.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy requested a review from Copilot February 25, 2026 23:55
@swissspidy
Copy link
Member

/gemini review

gemini-code-assist[bot]

This comment was marked as resolved.

This comment was marked as resolved.

swissspidy and others added 2 commits February 26, 2026 01:00
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@swissspidy swissspidy added this to the 2.1.6 milestone Feb 26, 2026
@swissspidy swissspidy marked this pull request as ready for review February 26, 2026 00:13
@swissspidy swissspidy requested a review from a team as a code owner February 26, 2026 00:13
@swissspidy swissspidy merged commit 6033ffc into main Feb 26, 2026
60 checks passed
@swissspidy swissspidy deleted the copilot/fail-gracefully-invalid-fields branch February 26, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fail Gracefully if invalid field is supplied to --fields

3 participants