Skip to content

refactor: update Console::run() to allow it be called in command() function#10080

Open
paulbalandan wants to merge 4 commits intocodeigniter4:4.8from
paulbalandan:refactor-console
Open

refactor: update Console::run() to allow it be called in command() function#10080
paulbalandan wants to merge 4 commits intocodeigniter4:4.8from
paulbalandan:refactor-console

Conversation

@paulbalandan
Copy link
Copy Markdown
Member

Description
This refactors Console::run() so that it can accept a list of tokens to parse (defaulting to $_SERVER['argv'] if empty) when run() is called. This consequently allows it to be called inside the command() function so that the latter can also enjoy the enhancements to command line parsing.

A behavior break that I am seeing is that command() now receives the $params as a merge of arguments and options, instead of the previous how-it-was-parsed order. However, since getting the params passed to a command by this function is not an intended use, I believe its impact is minimal.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@paulbalandan paulbalandan added refactor Pull requests that refactor code breaking change Pull requests that may break existing functionalities labels Mar 31, 2026
@github-actions github-actions bot added the 4.8 PRs that target the `4.8` branch. label Mar 31, 2026
if (! in_array('--no-header', $tokens, true)) {
// Don't show the header as it is not needed when running commands from code.
$tokens[] = '--no-header';
}
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.

Won't this break calls that use the -- separator? Because --no-header is appended after tokenization, it ends up after the separator and is treated as a positional argument, so the banner is printed and the target command receives an unexpected --no-header argument.

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.

Thanks. Missed that part.

Comment on lines -442 to +446
$exit = $console->run();

return is_int($exit) ? $exit : EXIT_SUCCESS;
return $console->initialize()->run() ?? EXIT_SUCCESS;
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.

Previously, we converted any non-int result to EXIT_SUCCESS. Now, a legacy/custom command returning a bool/string can cause a return-type error.

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.

Is bool|string allowed before as a return type? As I remember it, Console::run() has a PHPDoc of int|void.

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.

Well, yes... bool|string were not part of the "contract", which was effectively int|void, but run() is not natively typed, so non-int values were still accepted at runtime. Previously, we handled that defensively by normalizing any non-int to EXIT_SUCCESS. IMHO, if we want to enforce stricter behavior now, we should make that explicit in the docs (and upgrade notes?) and give users time to adjust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. breaking change Pull requests that may break existing functionalities refactor Pull requests that refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants