Passed
Pull Request — master (#14)
by Christoph
05:53
created
src/HtmlMatcher.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -8,107 +8,107 @@
 block discarded – undo
8 8
 
9 9
 class HtmlMatcher extends DiagnosingMatcher {
10 10
 
11
-	/**
12
-	 * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
13
-	 * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257
14
-	 */
15
-	const XML_UNKNOWN_TAG_ERROR_CODE = 801;
16
-
17
-	/**
18
-	 * @var Matcher
19
-	 */
20
-	private $elementMatcher;
21
-
22
-	/**
23
-	 * @param Matcher $elementMatcher
24
-	 *
25
-	 * @return self
26
-	 */
27
-	public static function htmlPiece( Matcher $elementMatcher = null ) {
28
-		return new static( $elementMatcher );
29
-	}
30
-
31
-	private function __construct( Matcher $elementMatcher = null ) {
32
-		$this->elementMatcher = $elementMatcher;
33
-	}
34
-
35
-	public function describeTo( Description $description ) {
36
-		$description->appendText( 'valid html piece ' );
37
-		if ( $this->elementMatcher ) {
38
-			$description->appendDescriptionOf( $this->elementMatcher );
39
-		}
40
-	}
41
-
42
-	/**
43
-	 * @param string $html
44
-	 * @param Description $mismatchDescription
45
-	 *
46
-	 * @return bool
47
-	 */
48
-	protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) {
49
-		$internalErrors = libxml_use_internal_errors( true );
50
-		libxml_clear_errors();
51
-		$document = new \DOMDocument();
52
-
53
-		$html = $this->escapeScriptTagContents( $html );
54
-
55
-		// phpcs:ignore Generic.PHP.NoSilencedErrors
56
-		if ( [email protected]$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
57
-			$mismatchDescription->appendText( 'there was some parsing error' );
58
-			return false;
59
-		}
60
-
61
-		$errors = libxml_get_errors();
62
-		libxml_clear_errors();
63
-		libxml_use_internal_errors( $internalErrors );
64
-
65
-		$result = true;
66
-		/** @var \LibXMLError $error */
67
-		foreach ( $errors as $error ) {
68
-			if ( $this->isUnknownTagError( $error ) ) {
69
-				continue;
70
-			}
71
-
72
-			$mismatchDescription->appendText( 'there was parsing error: ' )
73
-				->appendText( trim( $error->message ) )
74
-				->appendText( ' on line ' )
75
-				->appendText( $error->line );
76
-			$result = false;
77
-		}
78
-
79
-		if ( !$result ) {
80
-			return false;
81
-		}
82
-		$mismatchDescription->appendText( 'valid html piece ' );
83
-
84
-		if ( $this->elementMatcher ) {
85
-			$result = $this->elementMatcher->matches( $document );
86
-			$this->elementMatcher->describeMismatch( $document, $mismatchDescription );
87
-		}
88
-
89
-		$mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
90
-
91
-		return $result;
92
-	}
93
-
94
-	/**
95
-	 * @param \LibXMLError $error
96
-	 *
97
-	 * @return bool
98
-	 */
99
-	private function isUnknownTagError( \LibXMLError $error ) {
100
-		return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
101
-	}
102
-
103
-	/**
104
-	 * @param string $html
105
-	 *
106
-	 * @return string HTML
107
-	 */
108
-	private function escapeScriptTagContents( $html ) {
109
-		return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
110
-			return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
111
-		}, $html );
112
-	}
11
+    /**
12
+     * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
13
+     * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257
14
+     */
15
+    const XML_UNKNOWN_TAG_ERROR_CODE = 801;
16
+
17
+    /**
18
+     * @var Matcher
19
+     */
20
+    private $elementMatcher;
21
+
22
+    /**
23
+     * @param Matcher $elementMatcher
24
+     *
25
+     * @return self
26
+     */
27
+    public static function htmlPiece( Matcher $elementMatcher = null ) {
28
+        return new static( $elementMatcher );
29
+    }
30
+
31
+    private function __construct( Matcher $elementMatcher = null ) {
32
+        $this->elementMatcher = $elementMatcher;
33
+    }
34
+
35
+    public function describeTo( Description $description ) {
36
+        $description->appendText( 'valid html piece ' );
37
+        if ( $this->elementMatcher ) {
38
+            $description->appendDescriptionOf( $this->elementMatcher );
39
+        }
40
+    }
41
+
42
+    /**
43
+     * @param string $html
44
+     * @param Description $mismatchDescription
45
+     *
46
+     * @return bool
47
+     */
48
+    protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) {
49
+        $internalErrors = libxml_use_internal_errors( true );
50
+        libxml_clear_errors();
51
+        $document = new \DOMDocument();
52
+
53
+        $html = $this->escapeScriptTagContents( $html );
54
+
55
+        // phpcs:ignore Generic.PHP.NoSilencedErrors
56
+        if ( [email protected]$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
57
+            $mismatchDescription->appendText( 'there was some parsing error' );
58
+            return false;
59
+        }
60
+
61
+        $errors = libxml_get_errors();
62
+        libxml_clear_errors();
63
+        libxml_use_internal_errors( $internalErrors );
64
+
65
+        $result = true;
66
+        /** @var \LibXMLError $error */
67
+        foreach ( $errors as $error ) {
68
+            if ( $this->isUnknownTagError( $error ) ) {
69
+                continue;
70
+            }
71
+
72
+            $mismatchDescription->appendText( 'there was parsing error: ' )
73
+                ->appendText( trim( $error->message ) )
74
+                ->appendText( ' on line ' )
75
+                ->appendText( $error->line );
76
+            $result = false;
77
+        }
78
+
79
+        if ( !$result ) {
80
+            return false;
81
+        }
82
+        $mismatchDescription->appendText( 'valid html piece ' );
83
+
84
+        if ( $this->elementMatcher ) {
85
+            $result = $this->elementMatcher->matches( $document );
86
+            $this->elementMatcher->describeMismatch( $document, $mismatchDescription );
87
+        }
88
+
89
+        $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
90
+
91
+        return $result;
92
+    }
93
+
94
+    /**
95
+     * @param \LibXMLError $error
96
+     *
97
+     * @return bool
98
+     */
99
+    private function isUnknownTagError( \LibXMLError $error ) {
100
+        return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
101
+    }
102
+
103
+    /**
104
+     * @param string $html
105
+     *
106
+     * @return string HTML
107
+     */
108
+    private function escapeScriptTagContents( $html ) {
109
+        return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
110
+            return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
111
+        }, $html );
112
+    }
113 113
 
114 114
 }
Please login to merge, or discard this patch.