Is it possible to debug this PHP

85 views Asked by At

I'm really grasping at straws here. I don't code and my PHP email extension works great EXCEPT for these errors when editing the templates. My vendor is in who-knows what timezone and has been very unresponsive. Besides the obvious - change extensions, change vendors etc., can any of you PHP folk look at this code block and tell me what's wrong. I don't know if taken out of context it will be meaningful. The Errors are PHP Notice: Trying to get property of non-object beginning at line 1344. Here is a block of code from 1344 to 1370. It evidently has to do with switching to plain text when HTML isn't an option. AGAIN, grasping at straws here but stackoverflow is pretty solid when it comes to knowledgeable folk.

I'm re-pasting the code to include a more complete block. I have also added comments in the lines to indicate where the four warnings appear. They look like this: //NEXT LINE IS ERROR ONE - LINE 1344 Notice: Trying to get property of non-object

Thank you

<?php
    public static function isHTML($str) {
    $html = array('A','ABBR','ACRONYM','ADDRESS','APPLET','AREA','B','BASE','BASEFONT','BDO','BIG','BLOCKQUOTE',
        'BODY','BR','BUTTON','CAPTION','CENTER','CITE','CODE','COL','COLGROUP','DD','DEL','DFN','DIR','DIV','DL',
        'DT','EM','FIELDSET','FONT','FORM','FRAME','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HR','HTML',
        'I','IFRAME','IMG','INPUT','INS','ISINDEX','KBD','LABEL','LEGEND','LI','LINK','MAP','MENU','META',
        'NOFRAMES','NOSCRIPT','OBJECT','OL','OPTGROUP','OPTION','P','PARAM','PRE','Q','S','SAMP','SCRIPT',
        'SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT',
        'TH','THEAD','TITLE','TR','TT','U','UL','VAR');

    return preg_match("~(&lt;\/?)\b(".implode('|', $html).")\b([^>]*&gt;)~i", $str, $c);
}

private function _html_to_plain_text($node) {
    if ($node instanceof DOMText) {
        return preg_replace("/\\s+/im", " ", $node->wholeText);
    }
    if ($node instanceof DOMDocumentType) {
        // ignore
        return "";
    }

    // Next
//NEXT LINE IS ERROR ONE - LINE 1344  Notice: Trying to get property of non-object
    $nextNode = $node->nextSibling;
    while ($nextNode != null) {
        if ($nextNode instanceof DOMElement) {
            break;
        }
        $nextNode = $nextNode->nextSibling;
    }
    $nextName = null;
    if ($nextNode instanceof DOMElement && $nextNode != null) {
        $nextName = strtolower($nextNode->nodeName);
    }

    // Previous
//NEXT LINE IS ERROR TWO - LINE 1357  Notice: Trying to get property of non-object
    $nextNode = $node->previousSibling;
    while ($nextNode != null) {
        if ($nextNode instanceof DOMElement) {
            break;
        }
        $nextNode = $nextNode->previousSibling;
    }
    $prevName = null;
    if ($nextNode instanceof DOMElement && $nextNode != null) {
        $prevName = strtolower($nextNode->nodeName);
    }
//NEXT LINE IS ERROR THREE - LINE 1369  Notice: Trying to get property of non-object
    $name = strtolower($node->nodeName);

    // start whitespace
    switch ($name) {
        case "hr":
            return "------\n";

        case "style":
        case "head":
        case "title":
        case "meta":
        case "script":
            // ignore these tags
            return "";

        case "h1":
        case "h2":
        case "h3":
        case "h4":
        case "h5":
        case "h6":
            // add two newlines
            $output = "\n";
            break;

        case "p":
        case "div":
            // add one line
            $output = "\n";
            break;

        default:
            // print out contents of unknown tags
            $output = "";
            break;
    }

    // debug $output .= "[$name,$nextName]";
//NEXT LINE IS ERROR FOUR - LINE 1408  Notice: Trying to get property of non-object
    if($node->childNodes){
        for ($i = 0; $i < $node->childNodes->length; $i++) {
            $n = $node->childNodes->item($i);

            $text = $this->_html_to_plain_text($n);

            $output .= $text;
        }
    }

    // end whitespace
    switch ($name) {
        case "style":
        case "head":
        case "title":
        case "meta":
        case "script":
            // ignore these tags
            return "";

        case "h1":
        case "h2":
        case "h3":
        case "h4":
        case "h5":
        case "h6":
            $output .= "\n";
            break;

        case "p":
        case "br":
            // add one line
            if ($nextName != "div")
                $output .= "\n";
            break;

        case "div":
        // add one line only if the next child isn't a div
            if (($nextName != "div" && $nextName != null) || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateSpacing')))
                $output .= "\n";
            break;

        case "a":
            // links are returned in [text](link) format
            $href = $node->getAttribute("href");
            if ($href == null) {
                // it doesn't link anywhere
                if ($node->getAttribute("name") != null) {
                    $output = "$output";
                }
            } else {
                if ($href == $output || ($node->hasAttribute('class') && strstr($node->getAttribute('class'), 'emailtemplateNoDisplay'))) {
                    // link to the same address: just use link
                    $output;
                } else {
                    // No display
                    $output = $href . "\n" . $output;
                }
            }

            // does the next node require additional whitespace?
            switch ($nextName) {
                case "h1": case "h2": case "h3": case "h4": case "h5": case "h6":
                    $output .= "\n";
                    break;
            }

        default:
            // do nothing
    }

    return $output;
    }
}
?>
1

There are 1 answers

1
RedDragonWebDesign On

Sounds like the $node variable coming into your function from private function _html_to_plain_text($node) isn't the correct variable type. Might be an int, string, array, or null, instead of whatever object/class it's supposed to be. You probably need to look at whatever code is calling that function, rather than the function itself.

Debugging in PHP is a skill in itself.

  • If coding by hand, consider inserting var_export($node); statements before and after areas where you suspect there is a problem so you can examine it.
  • If you have access to a debugger (for example, Eclipse + XAMPP + XDebug), use that and go line by line, examining the contents of the $node variable by hovering over it or looking at your variable list.