Fatal Error: Argument #3 ($code) must be of type int, string given
Before submitting an issue please check that you’ve completed the following steps:
- Made sure you’re on the latest version ✅
- Used the search feature to ensure that the bug hasn’t been reported before ✅
Describe the bug The following error is logged sometimes. In both cases we have, it was for version 3.16:
Fatal error: Uncaught TypeError: WP_Rocket\Engine\Common\Database\Queries\AbstractQuery::update_message(): Argument #3 ($code) must be of type int, string given, called in /home/wordpress/wp-content/plugins/wp-rocket/inc/Engine/Common/JobManager/Managers/AbstractManager.php on line 204 and defined in /home/wordpress/wp-content/plugins/wp-rocket/inc/Engine/Common/Database/Queries/AbstractQuery.php:578
Full trace here - https://secure.helpscout.net/conversation/2621067830/496192#thread-7885680013
In the related files, I see the 3rd argument with default set as int in one case:
https://github.com/wp-media/wp-rocket/blob/d4319347ba6f301ab52ebfe6d1154fef7708dc8c/inc/Engine/Common/Database/Queries/AbstractQuery.php#L578
While here I see it as string:
https://github.com/wp-media/wp-rocket/blob/d4319347ba6f301ab52ebfe6d1154fef7708dc8c/inc/Engine/Common/JobManager/Managers/AbstractManager.php#L199
To Reproduce Unfortunately, I'm not completely sure how to reproduce it.
Expected behavior We should protect against this kind of error.
Additional context Ticket1 - https://secure.helpscout.net/conversation/2621067830/496192 Ticket2 - https://secure.helpscout.net/conversation/2606889904/493486/#thread-7840276870 Slack - https://wp-media.slack.com/archives/C43T1AYMQ/p1718027695573349
Acceptance Criteria (for WP Media team use only) Clear instructions for developers, to be added before the grooming
Might be a duplicate of https://github.com/wp-media/wp-rocket/issues/6395
HelpScout similar tickets: https://secure.helpscout.net/conversation/2647601826/501808/ https://secure.helpscout.net/conversation/2627965189/497805/
Related: https://secure.helpscout.net/conversation/2663017874/504691/
related https://secure.helpscout.net/conversation/2677458637/507259/
Related: https://secure.helpscout.net/conversation/2681617659/507933/
related https://secure.helpscout.net/conversation/2681755693/507950
Related: https://secure.helpscout.net/conversation/2682126241/507985
Related: https://secure.helpscout.net/conversation/2683561186/508253/
Related: https://secure.helpscout.net/conversation/2685335823/508532/
Reproduce the problem
Well, there isn't a proper way to reproduce the issue, however, we could hard code a string in $job_details['code'] here https://github.com/wp-media/wp-rocket/blob/2b2243e08aecf0d7618179106c2a133749762e47/inc/Engine/Optimization/RUCSS/Strategy/Strategies/DefaultProcess.php#L108
This would ensure to get the same error as reported.
Identify the root cause
The issue is that a string is given in the update_message method while it's waiting for an int.
Scope a solution
To solve the issue we could force the cast of$job_details['code'] to an int . So in case this is a string with an actual number it would output its int version otherwise it would return 0.
To do that, we can modify this line by :
$this->used_css_query->update_message( $row_details->id, (int) $job_details['code'], $job_details['message'], $row_details->error_message );
Also,
We can modify the parameter $error_code in this function in order to typehint as an int.
Effort estimation:
S
Is a refactor needed in that part of the codebase?
No
@Miraeld Shouldn't there also be some standardization of the typehint for both update_message() methods, so that they both expect a int, instead of one expecting a string and the other a int?
Updated grooming
Draft PR: https://github.com/wp-media/wp-rocket/pull/6992