Possible bug/typo that produces PHP Fatal Error
-
Hello!
Recently in one of my projects I had to create renewal orders for WooCommerce Subscriptions plugin programmatically and in the process I got this error:
PHP-fout Uncaught TypeError: Argument 1 passed to Aelia\WC\EU_VAT_Assistant\Validation\EU_VAT_Number_Validator::format_vies_response() must be of the type array, bool given, called in /public_html/wp-content/plugins/woocommerce-eu-vat-assistant/src/lib/classes/integration/vat_number_validation/validators/eu_vat_number_validator.php on line 196 and defined in /public_html/wp-content/plugins/woocommerce-eu-vat-assistant/src/lib/classes/integration/vat_number_validation/validators/eu_vat_number_validator.php:210
Using EU VAT Assistant 2.0.20.210817 on this site.
I did a little debugging and found this code in mentioned above file eu_vat_number_validator.php:196 :
// Format the response before returning it // @since 2.0.1.201215 if(array($response)) { return self::format_vies_response($response); }
and this code next to it on line 210:
protected static function format_vies_response(array $response) {
The issue I can clearly see here considering the comment above is that method “format_vies_response()” always expects input parameter to be an array.
And I think you assumed it to be that way, when you said in comment: “Format the response before returning it”.
But the issue is that “if(array($response)) {” doesn’t format the $response variable to array type. This “if()” closure will always be TRUE, even if $response will be (bool) false.
Let’s assume “$response = (bool) false;” and see what happens:
var_dump( array( false ) == TRUE ); // this will always be true, because array is equal (bool) false, only when it is empty, and with "false" being a single (bool) element in array, it will not be considered empty, thus this comparison will never be "false". var_dump( array( 0 => false ) == TRUE ); // same way, always true var_dump( array() == TRUE ); // and this will be false, because empty array is type-casted to (bool) false
This way the reason of this PHP Fatal error is obvious. Function expects array input, while you don’t have proper formatting where you are calling it.
If closure is always true, because it is not how you properly type-cast variables in PHP. Which in my case leads to (bool) $response = false; being sent to the function and I get error.
Check out PHP type-casting for arrays ??
Finally, to really format the response in your code you need to do it either this way:
// Format the response before returning it // @since 2.0.1.201215 if($response = array($response)) { return self::format_vies_response($response); }
Or at least that way:
return self::format_vies_response( (array) $response );
Or maybe there’s some other logic that you wanna revisit ??
Cheers,
Bogdan
- The topic ‘Possible bug/typo that produces PHP Fatal Error’ is closed to new replies.