• Resolved BogdanFix

    (@bogdanfix)


    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

    • This topic was modified 3 years, 2 months ago by BogdanFix.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Diego

    (@daigo75)

    Thanks for your feedback. We’re well aware of how typecasting works, but the purpose of the affected line is not to do that.

    The error is much simpler, it’s a typo. The if is supposed to check that the response is actually an array. The correct logic would be the following:

    if(is_array($response)) {
     return self::format_vies_response($response);
    }

    This is done before passing it to a method which, by design, always expects an array. As you will probably notice out, we are going to “type everything” in all of our solutions, replacing implicit types with explicit ones, and arrays with data objects. This will allow us to catch and fix errors sooner, and ensure that all the calls are made with the required data, avoiding dozens of calls to isset(), is_numeric(), typeof and so on.

    I will pass on the information about the incorrect check, so that we can get this glitch patched quickly. Thanks for taking the time to report it.

    Plugin Author Diego

    (@daigo75)

    @bogdanfix I would like to confirm what I wrote earlier. If you change the affected line to if(is_array($response)) {, the check will work as originally intended.

    This will be fixed in version 2.0.21.210910.

    Thread Starter BogdanFix

    (@bogdanfix)

    Thank you @daigo75 for the feedback! Happy to help a fellow WordPress developer. And glad to hear it’s just a typo ??

    P.S. Sorry for that thing about typecasting, didn’t intend to insult you in any way.

    Respectfully,
    Bogdan

    Plugin Author Diego

    (@daigo75)

    Thanks to you for reporting the issue. It was quite tricky to reproduce on a testing server, since it can only occur in some specific cases, and it might have gone unnoticed for a lot longer if you didn’t spot it. Well done. ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Possible bug/typo that produces PHP Fatal Error’ is closed to new replies.