• # foreach() receiving null values in getOutput method of Output.php
    
    # Details
    
    I'm getting an exception error reported by sentry saying that a foreach statement in getOutput method of popular posts is receiving null values. The stack trace is:
    
    **"ErrorException: Warning: foreach() argument must be of type array|object, null given"**
    
    The line it is referring to is foreach( $body->childNodes as $node ) { which is line 181 of web/app/plugins/wordpress-popular-posts/src/Output.php, in the getOutput method. Here is the full code in context for the public function getOutput that this line is inside of:
    
    
    
    
    foreach( $body->childNodes as $node ) {
    
    $clean_html .= $dom->saveHTML($node);
    
    }
    
    
    So far there appear to be no problems on the site itself, even on the page sentry is reporting that it is finding the error on. However, I wanted to investigate this issue before it became a larger problem. Wordpress version 6.4.3 PHP version 8.0.22 Plugin version 6.4.2 # Solution I was considering a defensive programming solution where I might check if the array?is_empy??prior to the foreach, but since this is inside of a plugin, the only way can think to do that on my end is to create a patch. However, if a solution is already known for this issue or if patching would be overkill when there is a simpler solution, I would appreciate any assistance.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @manski117sevaa,

    There might be either some broken HTML on your end or an issue related to encoding, those are the only things I can think of right now that could cause this problem.

    I can add some checks to make sure that that foreach loop doesn’t run if $body->childNodes is empty but that won’t likely fix the underlying problem and your popular posts list might not render as expected (or even at all.)

    Thread Starter manski117sevaa

    (@manski117sevaa)

    Thanks for your quick response. I’ve been looking into the issue more on my end and you are probably right. It’s probably something site-wide that is likely an issue with some bad html on some old posts. Since this would take a while to solve, my goal was to go ahead and just make a patch on our end so that sentry would stop pinging us with warnings.

    However, I’ve been having trouble applying our patch. I’ve double checked everything on my end so I don’t think it’s anything simple like the directory or a syntax error in the file. When I try to apply it via composer update, the patch fails. Running –verbose, I can see it gives the error “Hunk #1 FAILED at 177 (different line endings).”

    At first we thought that it might be related to encoding, since Windows and Mac encode text slightly differently, and line endings are apparently a particular pain point. We looked into it and it looks like you are using LF line endings. Is this correct? If so, our current thought is that perhaps WPackagist is doing something to it? I was curious what you thought or if anyone else had experienced an issue like this.

    Plugin Author Hector Cabrera

    (@hcabrera)

    Try logging your HTML and see what the output is, it might give you some clues as to what’s going on. Something like:

    // Put this right before the foreach loop
    error_log( 'HTML' );
    error_log( print_r($html, true) ); 

    Then check the contents of /wp-content/debug.log.

    We looked into it and it looks like you are using LF line endings.

    Honestly I don’t see how line endings would have anything to with this. If that was the case there would be more people reporting this exact same issue. So far you’re the only one. At least with version 6.4.2.

    About the composer stuff, no idea. The plugin is only officially distributed through www.ads-software.com. Anything WPackagist related is not done by me.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘foreach() receiving null values in getOutput method of Output.php’ is closed to new replies.