GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#12)
by no
03:40
created
src/TagMatcher.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -6,8 +6,8 @@
 block discarded – undo
6 6
 
7 7
 abstract class TagMatcher extends TypeSafeDiagnosingMatcher {
8 8
 
9
-	public function __construct() {
10
-		parent::__construct( self::TYPE_OBJECT, \DOMElement::class );
11
-	}
9
+    public function __construct() {
10
+        parent::__construct( self::TYPE_OBJECT, \DOMElement::class );
11
+    }
12 12
 
13 13
 }
Please login to merge, or discard this patch.
src/ClassMatcher.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -8,41 +8,41 @@
 block discarded – undo
8 8
 
9 9
 class ClassMatcher extends TagMatcher {
10 10
 
11
-	/**
12
-	 * @var Matcher
13
-	 */
14
-	private $classMatcher;
15
-
16
-	public static function withClass( $class ) {
17
-		return new static( Util::wrapValueWithIsEqual( $class ) );
18
-	}
19
-
20
-	public function __construct( Matcher $class ) {
21
-		parent::__construct();
22
-		$this->classMatcher = $class;
23
-	}
24
-
25
-	public function describeTo( Description $description ) {
26
-		$description->appendText( 'with class ' )->appendDescriptionOf( $this->classMatcher );
27
-	}
28
-
29
-	/**
30
-	 * @param \DOMElement $item
31
-	 * @param Description $mismatchDescription
32
-	 *
33
-	 * @return bool
34
-	 */
35
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
36
-		$classAttribute = $item->getAttribute( 'class' );
37
-
38
-		$classes = preg_split( '/\s+/u', $classAttribute );
39
-		foreach ( $classes as $class ) {
40
-			if ( $this->classMatcher->matches( $class ) ) {
41
-				return true;
42
-			}
43
-		}
44
-
45
-		return false;
46
-	}
11
+    /**
12
+     * @var Matcher
13
+     */
14
+    private $classMatcher;
15
+
16
+    public static function withClass( $class ) {
17
+        return new static( Util::wrapValueWithIsEqual( $class ) );
18
+    }
19
+
20
+    public function __construct( Matcher $class ) {
21
+        parent::__construct();
22
+        $this->classMatcher = $class;
23
+    }
24
+
25
+    public function describeTo( Description $description ) {
26
+        $description->appendText( 'with class ' )->appendDescriptionOf( $this->classMatcher );
27
+    }
28
+
29
+    /**
30
+     * @param \DOMElement $item
31
+     * @param Description $mismatchDescription
32
+     *
33
+     * @return bool
34
+     */
35
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
36
+        $classAttribute = $item->getAttribute( 'class' );
37
+
38
+        $classes = preg_split( '/\s+/u', $classAttribute );
39
+        foreach ( $classes as $class ) {
40
+            if ( $this->classMatcher->matches( $class ) ) {
41
+                return true;
42
+            }
43
+        }
44
+
45
+        return false;
46
+    }
47 47
 
48 48
 }
Please login to merge, or discard this patch.
src/AttributeMatcher.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -8,75 +8,75 @@
 block discarded – undo
8 8
 
9 9
 class AttributeMatcher extends TagMatcher {
10 10
 
11
-	/**
12
-	 * @var Matcher
13
-	 */
14
-	private $attributeNameMatcher;
11
+    /**
12
+     * @var Matcher
13
+     */
14
+    private $attributeNameMatcher;
15 15
 
16
-	/**
17
-	 * @var Matcher|null
18
-	 */
19
-	private $valueMatcher;
16
+    /**
17
+     * @var Matcher|null
18
+     */
19
+    private $valueMatcher;
20 20
 
21
-	public static function withAttribute( $attributeName ) {
22
-		return new static( Util::wrapValueWithIsEqual( $attributeName ) );
23
-	}
21
+    public static function withAttribute( $attributeName ) {
22
+        return new static( Util::wrapValueWithIsEqual( $attributeName ) );
23
+    }
24 24
 
25
-	/**
26
-	 * AttributeMatcher constructor.
27
-	 * @param \Hamcrest\Matcher $attributeNameMatcher
28
-	 */
29
-	public function __construct( Matcher $attributeNameMatcher ) {
30
-		parent::__construct();
25
+    /**
26
+     * AttributeMatcher constructor.
27
+     * @param \Hamcrest\Matcher $attributeNameMatcher
28
+     */
29
+    public function __construct( Matcher $attributeNameMatcher ) {
30
+        parent::__construct();
31 31
 
32
-		$this->attributeNameMatcher = $attributeNameMatcher;
33
-	}
32
+        $this->attributeNameMatcher = $attributeNameMatcher;
33
+    }
34 34
 
35
-	/**
36
-	 * @param Matcher|mixed $value
37
-	 * @return AttributeMatcher
38
-	 */
39
-	public function havingValue( $value ) {
40
-		// TODO: Throw exception if value is set
41
-		$result = clone $this;
42
-		$result->valueMatcher = Util::wrapValueWithIsEqual( $value );
35
+    /**
36
+     * @param Matcher|mixed $value
37
+     * @return AttributeMatcher
38
+     */
39
+    public function havingValue( $value ) {
40
+        // TODO: Throw exception if value is set
41
+        $result = clone $this;
42
+        $result->valueMatcher = Util::wrapValueWithIsEqual( $value );
43 43
 
44
-		return $result;
45
-	}
44
+        return $result;
45
+    }
46 46
 
47
-	public function describeTo( Description $description ) {
48
-		$description->appendText( 'with attribute ' )
49
-			->appendDescriptionOf( $this->attributeNameMatcher );
50
-		if ( $this->valueMatcher ) {
51
-			$description->appendText( ' having value ' )
52
-				->appendDescriptionOf( $this->valueMatcher );
53
-		}
54
-	}
47
+    public function describeTo( Description $description ) {
48
+        $description->appendText( 'with attribute ' )
49
+            ->appendDescriptionOf( $this->attributeNameMatcher );
50
+        if ( $this->valueMatcher ) {
51
+            $description->appendText( ' having value ' )
52
+                ->appendDescriptionOf( $this->valueMatcher );
53
+        }
54
+    }
55 55
 
56
-	/**
57
-	 * @param \DOMElement $item
58
-	 * @param Description $mismatchDescription
59
-	 *
60
-	 * @return bool
61
-	 */
62
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
63
-		/** @var \DOMAttr $attribute */
64
-		foreach ( $item->attributes as $attribute ) {
65
-			if ( $this->valueMatcher ) {
66
-				if (
67
-					$this->attributeNameMatcher->matches( $attribute->name )
68
-					&& $this->valueMatcher->matches( $attribute->value )
69
-				) {
70
-					return true;
71
-				}
72
-			} else {
73
-				if ( $this->attributeNameMatcher->matches( $attribute->name ) ) {
74
-					return true;
75
-				}
76
-			}
77
-		}
56
+    /**
57
+     * @param \DOMElement $item
58
+     * @param Description $mismatchDescription
59
+     *
60
+     * @return bool
61
+     */
62
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
63
+        /** @var \DOMAttr $attribute */
64
+        foreach ( $item->attributes as $attribute ) {
65
+            if ( $this->valueMatcher ) {
66
+                if (
67
+                    $this->attributeNameMatcher->matches( $attribute->name )
68
+                    && $this->valueMatcher->matches( $attribute->value )
69
+                ) {
70
+                    return true;
71
+                }
72
+            } else {
73
+                if ( $this->attributeNameMatcher->matches( $attribute->name ) ) {
74
+                    return true;
75
+                }
76
+            }
77
+        }
78 78
 
79
-		return false;
80
-	}
79
+        return false;
80
+    }
81 81
 
82 82
 }
Please login to merge, or discard this patch.
src/functions.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -3,87 +3,87 @@
 block discarded – undo
3 3
 use Hamcrest\Matcher;
4 4
 
5 5
 if ( !function_exists( 'htmlPiece' ) ) {
6
-	/**
7
-	 * @param mixed $elementMatcher
8
-	 *
9
-	 * @return \WMDE\HamcrestHtml\HtmlMatcher
10
-	 */
11
-	function htmlPiece( Matcher $elementMatcher = null ) {
12
-		return \WMDE\HamcrestHtml\HtmlMatcher::htmlPiece( $elementMatcher );
13
-	}
6
+    /**
7
+     * @param mixed $elementMatcher
8
+     *
9
+     * @return \WMDE\HamcrestHtml\HtmlMatcher
10
+     */
11
+    function htmlPiece( Matcher $elementMatcher = null ) {
12
+        return \WMDE\HamcrestHtml\HtmlMatcher::htmlPiece( $elementMatcher );
13
+    }
14 14
 }
15 15
 
16 16
 if ( !function_exists( 'havingRootElement' ) ) {
17
-	function havingRootElement( Matcher $matcher = null ) {
18
-		return \WMDE\HamcrestHtml\RootElementMatcher::havingRootElement( $matcher );
19
-	}
17
+    function havingRootElement( Matcher $matcher = null ) {
18
+        return \WMDE\HamcrestHtml\RootElementMatcher::havingRootElement( $matcher );
19
+    }
20 20
 }
21 21
 
22 22
 if ( !function_exists( 'havingDirectChild' ) ) {
23
-	function havingDirectChild( Matcher $elementMatcher = null ) {
24
-		return \WMDE\HamcrestHtml\DirectChildElementMatcher::havingDirectChild( $elementMatcher );
25
-	}
23
+    function havingDirectChild( Matcher $elementMatcher = null ) {
24
+        return \WMDE\HamcrestHtml\DirectChildElementMatcher::havingDirectChild( $elementMatcher );
25
+    }
26 26
 }
27 27
 
28 28
 if ( !function_exists( 'havingChild' ) ) {
29
-	function havingChild( Matcher $elementMatcher = null ) {
30
-		return \WMDE\HamcrestHtml\ChildElementMatcher::havingChild( $elementMatcher );
31
-	}
29
+    function havingChild( Matcher $elementMatcher = null ) {
30
+        return \WMDE\HamcrestHtml\ChildElementMatcher::havingChild( $elementMatcher );
31
+    }
32 32
 }
33 33
 
34 34
 if ( !function_exists( 'withTagName' ) ) {
35
-	/**
36
-	 * @param Matcher|string $tagName
37
-	 *
38
-	 * @return \WMDE\HamcrestHtml\TagNameMatcher
39
-	 */
40
-	function withTagName( $tagName ) {
41
-		return \WMDE\HamcrestHtml\TagNameMatcher::withTagName( $tagName );
42
-	}
35
+    /**
36
+     * @param Matcher|string $tagName
37
+     *
38
+     * @return \WMDE\HamcrestHtml\TagNameMatcher
39
+     */
40
+    function withTagName( $tagName ) {
41
+        return \WMDE\HamcrestHtml\TagNameMatcher::withTagName( $tagName );
42
+    }
43 43
 }
44 44
 
45 45
 if ( !function_exists( 'withAttribute' ) ) {
46
-	/**
47
-	 * @param Matcher|string $attributeName
48
-	 *
49
-	 * @return \WMDE\HamcrestHtml\AttributeMatcher
50
-	 */
51
-	function withAttribute( $attributeName ) {
52
-		return \WMDE\HamcrestHtml\AttributeMatcher::withAttribute( $attributeName );
53
-	}
46
+    /**
47
+     * @param Matcher|string $attributeName
48
+     *
49
+     * @return \WMDE\HamcrestHtml\AttributeMatcher
50
+     */
51
+    function withAttribute( $attributeName ) {
52
+        return \WMDE\HamcrestHtml\AttributeMatcher::withAttribute( $attributeName );
53
+    }
54 54
 }
55 55
 
56 56
 if ( !function_exists( 'withClass' ) ) {
57
-	/**
58
-	 * @param Matcher|string $class
59
-	 *
60
-	 * @return \WMDE\HamcrestHtml\ClassMatcher
61
-	 */
62
-	function withClass( $class ) {
63
-		// TODO don't allow to call with empty string
57
+    /**
58
+     * @param Matcher|string $class
59
+     *
60
+     * @return \WMDE\HamcrestHtml\ClassMatcher
61
+     */
62
+    function withClass( $class ) {
63
+        // TODO don't allow to call with empty string
64 64
 
65
-		return \WMDE\HamcrestHtml\ClassMatcher::withClass( $class );
66
-	}
65
+        return \WMDE\HamcrestHtml\ClassMatcher::withClass( $class );
66
+    }
67 67
 }
68 68
 
69 69
 if ( !function_exists( 'havingTextContents' ) ) {
70
-	/**
71
-	 * @param Matcher|string $text
72
-	 *
73
-	 * @return \WMDE\HamcrestHtml\TextContentsMatcher
74
-	 */
75
-	function havingTextContents( $text ) {
76
-		return \WMDE\HamcrestHtml\TextContentsMatcher::havingTextContents( $text );
77
-	}
70
+    /**
71
+     * @param Matcher|string $text
72
+     *
73
+     * @return \WMDE\HamcrestHtml\TextContentsMatcher
74
+     */
75
+    function havingTextContents( $text ) {
76
+        return \WMDE\HamcrestHtml\TextContentsMatcher::havingTextContents( $text );
77
+    }
78 78
 }
79 79
 
80 80
 if ( !function_exists( 'tagMatchingOutline' ) ) {
81
-	/**
82
-	 * @param string $htmlOutline
83
-	 *
84
-	 * @return \WMDE\HamcrestHtml\ComplexTagMatcher
85
-	 */
86
-	function tagMatchingOutline( $htmlOutline ) {
87
-		return \WMDE\HamcrestHtml\ComplexTagMatcher::tagMatchingOutline( $htmlOutline );
88
-	}
81
+    /**
82
+     * @param string $htmlOutline
83
+     *
84
+     * @return \WMDE\HamcrestHtml\ComplexTagMatcher
85
+     */
86
+    function tagMatchingOutline( $htmlOutline ) {
87
+        return \WMDE\HamcrestHtml\ComplexTagMatcher::tagMatchingOutline( $htmlOutline );
88
+    }
89 89
 }
Please login to merge, or discard this patch.
src/XmlNodeRecursiveIterator.php 1 patch
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -4,27 +4,27 @@
 block discarded – undo
4 4
 
5 5
 class XmlNodeRecursiveIterator extends \ArrayIterator {
6 6
 
7
-	public function __construct( \DOMNodeList $nodeList ) {
8
-		$queue = $this->addElementsToQueue( [], $nodeList );
9
-		parent::__construct( $queue );
10
-	}
7
+    public function __construct( \DOMNodeList $nodeList ) {
8
+        $queue = $this->addElementsToQueue( [], $nodeList );
9
+        parent::__construct( $queue );
10
+    }
11 11
 
12
-	/**
13
-	 * @param array $queue
14
-	 * @param \DOMNodeList $nodeList
15
-	 *
16
-	 * @return array New queue
17
-	 */
18
-	private function addElementsToQueue( array $queue, \DOMNodeList $nodeList ) {
19
-		/** @var \DOMElement $node */
20
-		foreach ( $nodeList as $node ) {
21
-			$queue[] = $node;
22
-			if ( $node->childNodes !== null ) {
23
-				$queue = $this->addElementsToQueue( $queue, $node->childNodes );
24
-			}
25
-		}
12
+    /**
13
+     * @param array $queue
14
+     * @param \DOMNodeList $nodeList
15
+     *
16
+     * @return array New queue
17
+     */
18
+    private function addElementsToQueue( array $queue, \DOMNodeList $nodeList ) {
19
+        /** @var \DOMElement $node */
20
+        foreach ( $nodeList as $node ) {
21
+            $queue[] = $node;
22
+            if ( $node->childNodes !== null ) {
23
+                $queue = $this->addElementsToQueue( $queue, $node->childNodes );
24
+            }
25
+        }
26 26
 
27
-		return $queue;
28
-	}
27
+        return $queue;
28
+    }
29 29
 
30 30
 }
Please login to merge, or discard this patch.
src/TextContentsMatcher.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -8,32 +8,32 @@
 block discarded – undo
8 8
 
9 9
 class TextContentsMatcher extends TagMatcher {
10 10
 
11
-	/**
12
-	 * @var Matcher
13
-	 */
14
-	private $matcher;
15
-
16
-	public static function havingTextContents( $text ) {
17
-		return new static( Util::wrapValueWithIsEqual( $text ) );
18
-	}
19
-
20
-	public function __construct( Matcher $matcher ) {
21
-		parent::__construct();
22
-		$this->matcher = $matcher;
23
-	}
24
-
25
-	public function describeTo( Description $description ) {
26
-		$description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher );
27
-	}
28
-
29
-	/**
30
-	 * @param \DOMElement $item
31
-	 * @param Description $mismatchDescription
32
-	 *
33
-	 * @return bool
34
-	 */
35
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
36
-		return $this->matcher->matches( $item->textContent );
37
-	}
11
+    /**
12
+     * @var Matcher
13
+     */
14
+    private $matcher;
15
+
16
+    public static function havingTextContents( $text ) {
17
+        return new static( Util::wrapValueWithIsEqual( $text ) );
18
+    }
19
+
20
+    public function __construct( Matcher $matcher ) {
21
+        parent::__construct();
22
+        $this->matcher = $matcher;
23
+    }
24
+
25
+    public function describeTo( Description $description ) {
26
+        $description->appendText( 'having text contents ' )->appendDescriptionOf( $this->matcher );
27
+    }
28
+
29
+    /**
30
+     * @param \DOMElement $item
31
+     * @param Description $mismatchDescription
32
+     *
33
+     * @return bool
34
+     */
35
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
36
+        return $this->matcher->matches( $item->textContent );
37
+    }
38 38
 
39 39
 }
Please login to merge, or discard this patch.
src/TagNameMatcher.php 1 patch
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -8,39 +8,39 @@
 block discarded – undo
8 8
 
9 9
 class TagNameMatcher extends TagMatcher {
10 10
 
11
-	/**
12
-	 * @var Matcher
13
-	 */
14
-	private $tagNameMatcher;
15
-
16
-	public static function withTagName( $tagName ) {
17
-		return new static( Util::wrapValueWithIsEqual( $tagName ) );
18
-	}
19
-
20
-	public function __construct( Matcher $tagNameMatcher ) {
21
-		parent::__construct();
22
-		$this->tagNameMatcher = $tagNameMatcher;
23
-	}
24
-
25
-	public function describeTo( Description $description ) {
26
-		$description->appendText( 'with tag name ' )
27
-			->appendDescriptionOf( $this->tagNameMatcher );
28
-	}
29
-
30
-	/**
31
-	 * @param \DOMElement $item
32
-	 * @param Description $mismatchDescription
33
-	 *
34
-	 * @return bool
35
-	 */
36
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
37
-		if ( $this->tagNameMatcher->matches( $item->tagName ) ) {
38
-			return true;
39
-		}
40
-
41
-		$mismatchDescription->appendText( 'tag name ' );
42
-		$this->tagNameMatcher->describeMismatch( $item->tagName, $mismatchDescription );
43
-		return false;
44
-	}
11
+    /**
12
+     * @var Matcher
13
+     */
14
+    private $tagNameMatcher;
15
+
16
+    public static function withTagName( $tagName ) {
17
+        return new static( Util::wrapValueWithIsEqual( $tagName ) );
18
+    }
19
+
20
+    public function __construct( Matcher $tagNameMatcher ) {
21
+        parent::__construct();
22
+        $this->tagNameMatcher = $tagNameMatcher;
23
+    }
24
+
25
+    public function describeTo( Description $description ) {
26
+        $description->appendText( 'with tag name ' )
27
+            ->appendDescriptionOf( $this->tagNameMatcher );
28
+    }
29
+
30
+    /**
31
+     * @param \DOMElement $item
32
+     * @param Description $mismatchDescription
33
+     *
34
+     * @return bool
35
+     */
36
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
37
+        if ( $this->tagNameMatcher->matches( $item->tagName ) ) {
38
+            return true;
39
+        }
40
+
41
+        $mismatchDescription->appendText( 'tag name ' );
42
+        $this->tagNameMatcher->describeMismatch( $item->tagName, $mismatchDescription );
43
+        return false;
44
+    }
45 45
 
46 46
 }
Please login to merge, or discard this patch.
src/HtmlMatcher.php 1 patch
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -8,94 +8,94 @@
 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 HtmlMatcher
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
-	protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) {
43
-		$internalErrors = libxml_use_internal_errors( true );
44
-		$document = new \DOMDocument();
45
-
46
-		$html = $this->escapeScriptTagContents( $html );
47
-
48
-		if ( !@$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
49
-			$mismatchDescription->appendText( 'there was some parsing error' );
50
-			return false;
51
-		}
52
-
53
-		$errors = libxml_get_errors();
54
-		libxml_clear_errors();
55
-		libxml_use_internal_errors( $internalErrors );
56
-
57
-		$result = true;
58
-		/** @var \LibXMLError $error */
59
-		foreach ( $errors as $error ) {
60
-			if ( $this->isUnknownTagError( $error ) ) {
61
-				continue;
62
-			}
63
-
64
-			$mismatchDescription->appendText( 'there was parsing error: ' )
65
-				->appendText( trim( $error->message ) )
66
-				->appendText( ' on line ' )
67
-				->appendText( $error->line );
68
-			$result = false;
69
-		}
70
-
71
-		if ( !$result ) {
72
-			return false;
73
-		}
74
-		$mismatchDescription->appendText( 'valid html piece ' );
75
-
76
-		if ( $this->elementMatcher ) {
77
-			$result = $this->elementMatcher->matches( $document );
78
-			$this->elementMatcher->describeMismatch( $document, $mismatchDescription );
79
-		}
80
-
81
-		$mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
82
-
83
-		return $result;
84
-	}
85
-
86
-	private function isUnknownTagError( \LibXMLError $error ) {
87
-		return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
88
-	}
89
-
90
-	/**
91
-	 * @param string $html
92
-	 *
93
-	 * @return string HTML
94
-	 */
95
-	private function escapeScriptTagContents( $html ) {
96
-		return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
97
-			return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
98
-		}, $html );
99
-	}
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 HtmlMatcher
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
+    protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) {
43
+        $internalErrors = libxml_use_internal_errors( true );
44
+        $document = new \DOMDocument();
45
+
46
+        $html = $this->escapeScriptTagContents( $html );
47
+
48
+        if ( !@$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
49
+            $mismatchDescription->appendText( 'there was some parsing error' );
50
+            return false;
51
+        }
52
+
53
+        $errors = libxml_get_errors();
54
+        libxml_clear_errors();
55
+        libxml_use_internal_errors( $internalErrors );
56
+
57
+        $result = true;
58
+        /** @var \LibXMLError $error */
59
+        foreach ( $errors as $error ) {
60
+            if ( $this->isUnknownTagError( $error ) ) {
61
+                continue;
62
+            }
63
+
64
+            $mismatchDescription->appendText( 'there was parsing error: ' )
65
+                ->appendText( trim( $error->message ) )
66
+                ->appendText( ' on line ' )
67
+                ->appendText( $error->line );
68
+            $result = false;
69
+        }
70
+
71
+        if ( !$result ) {
72
+            return false;
73
+        }
74
+        $mismatchDescription->appendText( 'valid html piece ' );
75
+
76
+        if ( $this->elementMatcher ) {
77
+            $result = $this->elementMatcher->matches( $document );
78
+            $this->elementMatcher->describeMismatch( $document, $mismatchDescription );
79
+        }
80
+
81
+        $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
82
+
83
+        return $result;
84
+    }
85
+
86
+    private function isUnknownTagError( \LibXMLError $error ) {
87
+        return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
88
+    }
89
+
90
+    /**
91
+     * @param string $html
92
+     *
93
+     * @return string HTML
94
+     */
95
+    private function escapeScriptTagContents( $html ) {
96
+        return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
97
+            return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
98
+        }, $html );
99
+    }
100 100
 
101 101
 }
Please login to merge, or discard this patch.
src/RootElementMatcher.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -8,60 +8,60 @@
 block discarded – undo
8 8
 
9 9
 class RootElementMatcher extends TypeSafeDiagnosingMatcher {
10 10
 
11
-	/**
12
-	 * @var Matcher
13
-	 */
14
-	private $tagMatcher;
11
+    /**
12
+     * @var Matcher
13
+     */
14
+    private $tagMatcher;
15 15
 
16
-	/**
17
-	 * @param Matcher|null $tagMatcher
18
-	 *
19
-	 * @return static
20
-	 */
21
-	public static function havingRootElement( Matcher $tagMatcher = null ) {
22
-		return new static( $tagMatcher );
23
-	}
16
+    /**
17
+     * @param Matcher|null $tagMatcher
18
+     *
19
+     * @return static
20
+     */
21
+    public static function havingRootElement( Matcher $tagMatcher = null ) {
22
+        return new static( $tagMatcher );
23
+    }
24 24
 
25
-	public function __construct( Matcher $tagMatcher = null ) {
26
-		parent::__construct( self::TYPE_OBJECT, \DOMDocument::class );
27
-		$this->tagMatcher = $tagMatcher;
28
-	}
25
+    public function __construct( Matcher $tagMatcher = null ) {
26
+        parent::__construct( self::TYPE_OBJECT, \DOMDocument::class );
27
+        $this->tagMatcher = $tagMatcher;
28
+    }
29 29
 
30
-	public function describeTo( Description $description ) {
31
-		$description->appendText( 'having root element ' );
32
-		if ( $this->tagMatcher ) {
33
-			$description->appendDescriptionOf( $this->tagMatcher );
34
-		}
35
-	}
30
+    public function describeTo( Description $description ) {
31
+        $description->appendText( 'having root element ' );
32
+        if ( $this->tagMatcher ) {
33
+            $description->appendDescriptionOf( $this->tagMatcher );
34
+        }
35
+    }
36 36
 
37
-	/**
38
-	 * @param \DOMDocument $item
39
-	 * @param Description $mismatchDescription
40
-	 *
41
-	 * @return bool
42
-	 */
43
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
44
-		$DOMNodeList = $item->documentElement->childNodes->item( 0 )->childNodes;
45
-		if ( $DOMNodeList->length > 1 ) {
46
-			// TODO Test this description
47
-			$mismatchDescription->appendText( 'having ' . $DOMNodeList->length . ' root elements ' );
48
-			return false;
49
-		}
37
+    /**
38
+     * @param \DOMDocument $item
39
+     * @param Description $mismatchDescription
40
+     *
41
+     * @return bool
42
+     */
43
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
44
+        $DOMNodeList = $item->documentElement->childNodes->item( 0 )->childNodes;
45
+        if ( $DOMNodeList->length > 1 ) {
46
+            // TODO Test this description
47
+            $mismatchDescription->appendText( 'having ' . $DOMNodeList->length . ' root elements ' );
48
+            return false;
49
+        }
50 50
 
51
-		$target = $DOMNodeList->item( 0 );
52
-		if ( !$target ) {
53
-			// TODO Reproduce?
54
-			$mismatchDescription->appendText( 'having no root elements ' );
55
-			return false;
56
-		}
51
+        $target = $DOMNodeList->item( 0 );
52
+        if ( !$target ) {
53
+            // TODO Reproduce?
54
+            $mismatchDescription->appendText( 'having no root elements ' );
55
+            return false;
56
+        }
57 57
 
58
-		if ( $this->tagMatcher ) {
59
-			$mismatchDescription->appendText( 'root element ' );
60
-			$this->tagMatcher->describeMismatch( $target, $mismatchDescription );
61
-			return $this->tagMatcher->matches( $target );
62
-		}
58
+        if ( $this->tagMatcher ) {
59
+            $mismatchDescription->appendText( 'root element ' );
60
+            $this->tagMatcher->describeMismatch( $target, $mismatchDescription );
61
+            return $this->tagMatcher->matches( $target );
62
+        }
63 63
 
64
-		return true;
65
-	}
64
+        return true;
65
+    }
66 66
 
67 67
 }
Please login to merge, or discard this patch.