1212use PHPHtmlParser \Exceptions \ParentNotFoundException ;
1313use PHPHtmlParser \Exceptions \StrictException ;
1414use PHPHtmlParser \Exceptions \UnknownChildTypeException ;
15+ use PHPHtmlParser \Exceptions \LogicalException ;
1516use stringEncode \Encode ;
1617
1718/**
@@ -167,10 +168,15 @@ public function load(string $str, array $options = []): Dom
167168 * @throws ChildNotFoundException
168169 * @throws CircularException
169170 * @throws StrictException
171+ * @throws LogicalException
170172 */
171173 public function loadFromFile (string $ file , array $ options = []): Dom
172174 {
173- return $ this ->loadStr (file_get_contents ($ file ), $ options );
175+ $ content = file_get_contents ($ file );
176+ if ($ content === false ) {
177+ throw new LogicalException ('file_get_contents failed and returned false when trying to read " ' .$ file .'". ' );
178+ }
179+ return $ this ->loadStr ($ content , $ options );
174180 }
175181
176182 /**
@@ -516,48 +522,87 @@ protected function clean(string $str): string
516522 $ is_gzip = 0 === mb_strpos ($ str , "\x1f" . "\x8b" . "\x08" , 0 , "US-ASCII " );
517523 if ($ is_gzip ) {
518524 $ str = gzdecode ($ str );
525+ if ($ str === false ) {
526+ throw new LogicalException ('gzdecode returned false. Error when trying to decode the string. ' );
527+ }
519528 }
520529
521530 // remove white space before closing tags
522531 $ str = mb_eregi_replace ("'\s+> " , "'> " , $ str );
532+ if ($ str === false ) {
533+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to clean single quotes. ' );
534+ }
523535 $ str = mb_eregi_replace ('"\s+> ' , '"> ' , $ str );
536+ if ($ str === false ) {
537+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to clean double quotes. ' );
538+ }
524539
525540 // clean out the \n\r
526541 $ replace = ' ' ;
527542 if ($ this ->options ->get ('preserveLineBreaks ' )) {
528543 $ replace = ' ' ;
529544 }
530545 $ str = str_replace (["\r\n" , "\r" , "\n" ], $ replace , $ str );
546+ if ($ str === false ) {
547+ throw new LogicalException ('str_replace returned false instead of a string. Error when attempting to clean input string. ' );
548+ }
531549
532550 // strip the doctype
533551 $ str = mb_eregi_replace ("<!doctype(.*?)> " , '' , $ str );
552+ if ($ str === false ) {
553+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip the doctype. ' );
554+ }
534555
535556 // strip out comments
536557 $ str = mb_eregi_replace ("<!--(.*?)--> " , '' , $ str );
558+ if ($ str === false ) {
559+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip comments. ' );
560+ }
537561
538562 // strip out cdata
539563 $ str = mb_eregi_replace ("<!\[CDATA\[(.*?)\]\]> " , '' , $ str );
564+ if ($ str === false ) {
565+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip out cdata. ' );
566+ }
540567
541568 // strip out <script> tags
542569 if ($ this ->options ->get ('removeScripts ' )) {
543570 $ str = mb_eregi_replace ("<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*> " , '' , $ str );
571+ if ($ str === false ) {
572+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to remove scripts 1. ' );
573+ }
544574 $ str = mb_eregi_replace ("<\s*script\s*>(.*?)<\s*/\s*script\s*> " , '' , $ str );
575+ if ($ str === false ) {
576+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to remove scripts 2. ' );
577+ }
545578 }
546579
547580 // strip out <style> tags
548581 if ($ this ->options ->get ('removeStyles ' )) {
549582 $ str = mb_eregi_replace ("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*> " , '' , $ str );
583+ if ($ str === false ) {
584+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip out style tags 1. ' );
585+ }
550586 $ str = mb_eregi_replace ("<\s*style\s*>(.*?)<\s*/\s*style\s*> " , '' , $ str );
587+ if ($ str === false ) {
588+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip out style tags 2. ' );
589+ }
551590 }
552591
553592 // strip out server side scripts
554593 if ($ this ->options ->get ('serverSideScripts ' )) {
555594 $ str = mb_eregi_replace ("(<\?)(.*?)(\?>) " , '' , $ str );
595+ if ($ str === false ) {
596+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to strip out service side scripts. ' );
597+ }
556598 }
557599
558600 // strip smarty scripts
559601 if ($ this ->options ->get ('removeSmartyScripts ' )) {
560602 $ str = mb_eregi_replace ("(\{\w)(.*?)(\}) " , '' , $ str );
603+ if ($ str === false ) {
604+ throw new LogicalException ('mb_eregi_replace returned false instead of a string. Error when attempting to remove smarty scripts. ' );
605+ }
561606 }
562607
563608 return $ str ;
@@ -658,7 +703,7 @@ protected function parseTag(): array
658703
659704 // check if this closing tag counts
660705 $ tag = strtolower ($ tag );
661- if (in_array ($ tag , $ this ->selfClosing )) {
706+ if (in_array ($ tag , $ this ->selfClosing , true )) {
662707 $ return ['status ' ] = true ;
663708
664709 return $ return ;
@@ -758,7 +803,7 @@ protected function parseTag(): array
758803 // self closing tag
759804 $ node ->getTag ()->selfClosing ();
760805 $ this ->content ->fastForward (1 );
761- } elseif (in_array ($ tag , $ this ->selfClosing )) {
806+ } elseif (in_array ($ tag , $ this ->selfClosing , true )) {
762807
763808 // Should be a self closing tag, check if we are strict
764809 if ($ this ->options ->strict ) {
@@ -770,7 +815,7 @@ protected function parseTag(): array
770815 $ node ->getTag ()->selfClosing ();
771816
772817 // Should this tag use a trailing slash?
773- if (in_array ($ tag , $ this ->noSlash ))
818+ if (in_array ($ tag , $ this ->noSlash , true ))
774819 {
775820 $ node ->getTag ()->noTrailingSlash ();
776821 }
@@ -798,10 +843,11 @@ protected function detectCharset(): bool
798843 $ encode ->from ($ this ->defaultCharset );
799844 $ encode ->to ($ this ->defaultCharset );
800845
801- if ( ! is_null ($ this ->options ->enforceEncoding )) {
846+ $ enforceEncoding = $ this ->options ->enforceEncoding ;
847+ if ( ! is_null ($ enforceEncoding )) {
802848 // they want to enforce the given encoding
803- $ encode ->from ($ this -> options -> enforceEncoding );
804- $ encode ->to ($ this -> options -> enforceEncoding );
849+ $ encode ->from ($ enforceEncoding );
850+ $ encode ->to ($ enforceEncoding );
805851
806852 return false ;
807853 }
0 commit comments