wp-rocket icon indicating copy to clipboard operation
wp-rocket copied to clipboard

Fatal Error: Argument #3 ($code) must be of type int, string given

Open joejoe04 opened this issue 1 year ago • 9 comments

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

joejoe04 avatar Jun 10 '24 15:06 joejoe04

Might be a duplicate of https://github.com/wp-media/wp-rocket/issues/6395

MathieuLamiot avatar Jun 21 '24 13:06 MathieuLamiot

HelpScout similar tickets: https://secure.helpscout.net/conversation/2647601826/501808/ https://secure.helpscout.net/conversation/2627965189/497805/

johan-las avatar Jul 09 '24 16:07 johan-las

Related: https://secure.helpscout.net/conversation/2663017874/504691/

joejoe04 avatar Jul 26 '24 16:07 joejoe04

related https://secure.helpscout.net/conversation/2677458637/507259/

alfonso100 avatar Aug 12 '24 21:08 alfonso100

Related: https://secure.helpscout.net/conversation/2681617659/507933/

joejoe04 avatar Aug 16 '24 16:08 joejoe04

related https://secure.helpscout.net/conversation/2681755693/507950

alfonso100 avatar Aug 16 '24 19:08 alfonso100

Related: https://secure.helpscout.net/conversation/2682126241/507985

joejoe04 avatar Aug 19 '24 13:08 joejoe04

Related: https://secure.helpscout.net/conversation/2683561186/508253/

Adrianadla avatar Aug 19 '24 15:08 Adrianadla

Related: https://secure.helpscout.net/conversation/2685335823/508532/

joejoe04 avatar Aug 21 '24 16:08 joejoe04

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 avatar Sep 02 '24 08:09 Miraeld

@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?

remyperona avatar Sep 03 '24 13:09 remyperona

Updated grooming

Miraeld avatar Sep 23 '24 10:09 Miraeld

Draft PR: https://github.com/wp-media/wp-rocket/pull/6992

Miraeld avatar Sep 26 '24 14:09 Miraeld