Passed
Push — master ( 6c6503...771255 )
by Marius
47s
created
src/ComplexTagMatcher.php 2 patches
Indentation   +212 added lines, -212 removed lines patch added patch discarded remove patch
@@ -10,217 +10,217 @@
 block discarded – undo
10 10
 
11 11
 class ComplexTagMatcher extends TagMatcher {
12 12
 
13
-	/**
14
-	 * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
15
-	 * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257
16
-	 */
17
-	const XML_UNKNOWN_TAG_ERROR_CODE = 801;
18
-
19
-	/**
20
-	 * @var string
21
-	 */
22
-	private $tagHtmlOutline;
23
-
24
-	/**
25
-	 * @var Matcher
26
-	 */
27
-	private $matcher;
28
-
29
-	/**
30
-	 * @param string $htmlOutline
31
-	 *
32
-	 * @return self
33
-	 */
34
-	public static function tagMatchingOutline( $htmlOutline ) {
35
-		return new self( $htmlOutline );
36
-	}
37
-
38
-	/**
39
-	 * @param string $tagHtmlRepresentation
40
-	 */
41
-	public function __construct( $tagHtmlRepresentation ) {
42
-		parent::__construct();
43
-
44
-		$this->tagHtmlOutline = $tagHtmlRepresentation;
45
-		$this->matcher = $this->createMatcherFromHtml( $tagHtmlRepresentation );
46
-	}
47
-
48
-	public function describeTo( Description $description ) {
49
-		$description->appendText( 'tag matching outline `' )
50
-			->appendText( $this->tagHtmlOutline )
51
-			->appendText( '` ' );
52
-	}
53
-
54
-	/**
55
-	 * @param \DOMElement $item
56
-	 * @param Description $mismatchDescription
57
-	 *
58
-	 * @return bool
59
-	 */
60
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
61
-		$result = $this->matcher->matches( $item );
62
-		if ( !$result ) {
63
-			$mismatchDescription->appendText( 'was `' )
64
-				->appendText( $this->elementToString( $item ) )
65
-				->appendText( '`' );
66
-		}
67
-		return $result;
68
-	}
69
-
70
-	/**
71
-	 * @param string $htmlOutline
72
-	 *
73
-	 * @return Matcher
74
-	 */
75
-	private function createMatcherFromHtml( $htmlOutline ) {
76
-		$document = $this->parseHtml( $htmlOutline );
77
-		$targetTag = $this->getSingleTagFromThe( $document );
78
-
79
-		$this->assertTagDoesNotContainChildren( $targetTag );
80
-
81
-		$attributeMatchers = $this->createAttributeMatchers( $htmlOutline, $targetTag );
82
-		$classMatchers = $this->createClassMatchers( $targetTag );
83
-
84
-		return AllOf::allOf(
85
-			new TagNameMatcher( IsEqual::equalTo( $targetTag->tagName ) ),
86
-			call_user_func_array( [ AllOf::class, 'allOf' ], $attributeMatchers ),
87
-			call_user_func_array( [ AllOf::class, 'allOf' ], $classMatchers )
88
-		);
89
-	}
90
-
91
-	/**
92
-	 * @param \LibXMLError $error
93
-	 *
94
-	 * @return bool
95
-	 */
96
-	private function isUnknownTagError( \LibXMLError $error ) {
97
-		return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
98
-	}
99
-
100
-	/**
101
-	 * @param string $inputHtml
102
-	 * @param string $attributeName
103
-	 *
104
-	 * @return bool
105
-	 */
106
-	private function isBooleanAttribute( $inputHtml, $attributeName ) {
107
-		$quotedName = preg_quote( $attributeName, '/' );
108
-
109
-		$attributeHasValueAssigned = preg_match( "/\b{$quotedName}\s*=/ui", $inputHtml );
110
-		return !$attributeHasValueAssigned;
111
-	}
112
-
113
-	/**
114
-	 * @param string $html
115
-	 *
116
-	 * @return \DOMDocument
117
-	 * @throws \InvalidArgumentException
118
-	 */
119
-	private function parseHtml( $html ) {
120
-		$internalErrors = libxml_use_internal_errors( true );
121
-		$document = new \DOMDocument();
122
-
123
-		// phpcs:ignore Generic.PHP.NoSilencedErrors
124
-		if ( [email protected]$document->loadHTML( $html ) ) {
125
-			throw new \InvalidArgumentException( "There was some parsing error of `$html`" );
126
-		}
127
-
128
-		$errors = libxml_get_errors();
129
-		libxml_clear_errors();
130
-		libxml_use_internal_errors( $internalErrors );
131
-
132
-		/** @var \LibXMLError $error */
133
-		foreach ( $errors as $error ) {
134
-			if ( $this->isUnknownTagError( $error ) ) {
135
-				continue;
136
-			}
137
-
138
-			throw new \InvalidArgumentException(
139
-				'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line
140
-			);
141
-		}
142
-
143
-		return $document;
144
-	}
145
-
146
-	/**
147
-	 * @param \DOMDocument $document
148
-	 *
149
-	 * @return \DOMElement
150
-	 * @throws \InvalidArgumentException
151
-	 */
152
-	private function getSingleTagFromThe( \DOMDocument $document ) {
153
-		$directChildren = iterator_to_array( $document->documentElement->childNodes );
154
-
155
-		$body = array_shift( $directChildren );
156
-		$directChildren = iterator_to_array( $body->childNodes );
157
-
158
-		if ( count( $directChildren ) !== 1 ) {
159
-			throw new InvalidArgumentException(
160
-				'Expected exactly 1 tag description, got ' . count( $directChildren )
161
-			);
162
-		}
163
-
164
-		return $directChildren[0];
165
-	}
166
-
167
-	private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) {
168
-		if ( $targetTag->childNodes->length > 0 ) {
169
-			throw new InvalidArgumentException( 'Nested elements are not allowed' );
170
-		}
171
-	}
172
-
173
-	/**
174
-	 * @param string $inputHtml
175
-	 * @param \DOMElement $targetTag
176
-	 *
177
-	 * @return AttributeMatcher[]
178
-	 */
179
-	private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) {
180
-		$attributeMatchers = [];
181
-		/** @var \DOMAttr $attribute */
182
-		foreach ( $targetTag->attributes as $attribute ) {
183
-			if ( $attribute->name === 'class' ) {
184
-				continue;
185
-			}
186
-
187
-			$attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) );
188
-			if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) {
189
-				$attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) );
190
-			}
191
-
192
-			$attributeMatchers[] = $attributeMatcher;
193
-		}
194
-		return $attributeMatchers;
195
-	}
196
-
197
-	/**
198
-	 * @param \DOMElement $targetTag
199
-	 *
200
-	 * @return ClassMatcher[]
201
-	 */
202
-	private function createClassMatchers( \DOMElement $targetTag ) {
203
-		$classMatchers = [];
204
-		$classValue = $targetTag->getAttribute( 'class' );
205
-		foreach ( explode( ' ', $classValue ) as $expectedClass ) {
206
-			if ( $expectedClass === '' ) {
207
-				continue;
208
-			}
209
-			$classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) );
210
-		}
211
-		return $classMatchers;
212
-	}
213
-
214
-	/**
215
-	 * @param \DOMElement $element
216
-	 *
217
-	 * @return string
218
-	 */
219
-	private function elementToString( \DOMElement $element ) {
220
-		$newDocument = new \DOMDocument();
221
-		$cloned = $element->cloneNode( true );
222
-		$newDocument->appendChild( $newDocument->importNode( $cloned, true ) );
223
-		return trim( $newDocument->saveHTML() );
224
-	}
13
+    /**
14
+     * @link http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
15
+     * @link https://github.com/Chronic-Dev/libxml2/blob/683f296a905710ff285c28b8644ef3a3d8be9486/include/libxml/xmlerror.h#L257
16
+     */
17
+    const XML_UNKNOWN_TAG_ERROR_CODE = 801;
18
+
19
+    /**
20
+     * @var string
21
+     */
22
+    private $tagHtmlOutline;
23
+
24
+    /**
25
+     * @var Matcher
26
+     */
27
+    private $matcher;
28
+
29
+    /**
30
+     * @param string $htmlOutline
31
+     *
32
+     * @return self
33
+     */
34
+    public static function tagMatchingOutline( $htmlOutline ) {
35
+        return new self( $htmlOutline );
36
+    }
37
+
38
+    /**
39
+     * @param string $tagHtmlRepresentation
40
+     */
41
+    public function __construct( $tagHtmlRepresentation ) {
42
+        parent::__construct();
43
+
44
+        $this->tagHtmlOutline = $tagHtmlRepresentation;
45
+        $this->matcher = $this->createMatcherFromHtml( $tagHtmlRepresentation );
46
+    }
47
+
48
+    public function describeTo( Description $description ) {
49
+        $description->appendText( 'tag matching outline `' )
50
+            ->appendText( $this->tagHtmlOutline )
51
+            ->appendText( '` ' );
52
+    }
53
+
54
+    /**
55
+     * @param \DOMElement $item
56
+     * @param Description $mismatchDescription
57
+     *
58
+     * @return bool
59
+     */
60
+    protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
61
+        $result = $this->matcher->matches( $item );
62
+        if ( !$result ) {
63
+            $mismatchDescription->appendText( 'was `' )
64
+                ->appendText( $this->elementToString( $item ) )
65
+                ->appendText( '`' );
66
+        }
67
+        return $result;
68
+    }
69
+
70
+    /**
71
+     * @param string $htmlOutline
72
+     *
73
+     * @return Matcher
74
+     */
75
+    private function createMatcherFromHtml( $htmlOutline ) {
76
+        $document = $this->parseHtml( $htmlOutline );
77
+        $targetTag = $this->getSingleTagFromThe( $document );
78
+
79
+        $this->assertTagDoesNotContainChildren( $targetTag );
80
+
81
+        $attributeMatchers = $this->createAttributeMatchers( $htmlOutline, $targetTag );
82
+        $classMatchers = $this->createClassMatchers( $targetTag );
83
+
84
+        return AllOf::allOf(
85
+            new TagNameMatcher( IsEqual::equalTo( $targetTag->tagName ) ),
86
+            call_user_func_array( [ AllOf::class, 'allOf' ], $attributeMatchers ),
87
+            call_user_func_array( [ AllOf::class, 'allOf' ], $classMatchers )
88
+        );
89
+    }
90
+
91
+    /**
92
+     * @param \LibXMLError $error
93
+     *
94
+     * @return bool
95
+     */
96
+    private function isUnknownTagError( \LibXMLError $error ) {
97
+        return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
98
+    }
99
+
100
+    /**
101
+     * @param string $inputHtml
102
+     * @param string $attributeName
103
+     *
104
+     * @return bool
105
+     */
106
+    private function isBooleanAttribute( $inputHtml, $attributeName ) {
107
+        $quotedName = preg_quote( $attributeName, '/' );
108
+
109
+        $attributeHasValueAssigned = preg_match( "/\b{$quotedName}\s*=/ui", $inputHtml );
110
+        return !$attributeHasValueAssigned;
111
+    }
112
+
113
+    /**
114
+     * @param string $html
115
+     *
116
+     * @return \DOMDocument
117
+     * @throws \InvalidArgumentException
118
+     */
119
+    private function parseHtml( $html ) {
120
+        $internalErrors = libxml_use_internal_errors( true );
121
+        $document = new \DOMDocument();
122
+
123
+        // phpcs:ignore Generic.PHP.NoSilencedErrors
124
+        if ( [email protected]$document->loadHTML( $html ) ) {
125
+            throw new \InvalidArgumentException( "There was some parsing error of `$html`" );
126
+        }
127
+
128
+        $errors = libxml_get_errors();
129
+        libxml_clear_errors();
130
+        libxml_use_internal_errors( $internalErrors );
131
+
132
+        /** @var \LibXMLError $error */
133
+        foreach ( $errors as $error ) {
134
+            if ( $this->isUnknownTagError( $error ) ) {
135
+                continue;
136
+            }
137
+
138
+            throw new \InvalidArgumentException(
139
+                'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line
140
+            );
141
+        }
142
+
143
+        return $document;
144
+    }
145
+
146
+    /**
147
+     * @param \DOMDocument $document
148
+     *
149
+     * @return \DOMElement
150
+     * @throws \InvalidArgumentException
151
+     */
152
+    private function getSingleTagFromThe( \DOMDocument $document ) {
153
+        $directChildren = iterator_to_array( $document->documentElement->childNodes );
154
+
155
+        $body = array_shift( $directChildren );
156
+        $directChildren = iterator_to_array( $body->childNodes );
157
+
158
+        if ( count( $directChildren ) !== 1 ) {
159
+            throw new InvalidArgumentException(
160
+                'Expected exactly 1 tag description, got ' . count( $directChildren )
161
+            );
162
+        }
163
+
164
+        return $directChildren[0];
165
+    }
166
+
167
+    private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) {
168
+        if ( $targetTag->childNodes->length > 0 ) {
169
+            throw new InvalidArgumentException( 'Nested elements are not allowed' );
170
+        }
171
+    }
172
+
173
+    /**
174
+     * @param string $inputHtml
175
+     * @param \DOMElement $targetTag
176
+     *
177
+     * @return AttributeMatcher[]
178
+     */
179
+    private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) {
180
+        $attributeMatchers = [];
181
+        /** @var \DOMAttr $attribute */
182
+        foreach ( $targetTag->attributes as $attribute ) {
183
+            if ( $attribute->name === 'class' ) {
184
+                continue;
185
+            }
186
+
187
+            $attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) );
188
+            if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) {
189
+                $attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) );
190
+            }
191
+
192
+            $attributeMatchers[] = $attributeMatcher;
193
+        }
194
+        return $attributeMatchers;
195
+    }
196
+
197
+    /**
198
+     * @param \DOMElement $targetTag
199
+     *
200
+     * @return ClassMatcher[]
201
+     */
202
+    private function createClassMatchers( \DOMElement $targetTag ) {
203
+        $classMatchers = [];
204
+        $classValue = $targetTag->getAttribute( 'class' );
205
+        foreach ( explode( ' ', $classValue ) as $expectedClass ) {
206
+            if ( $expectedClass === '' ) {
207
+                continue;
208
+            }
209
+            $classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) );
210
+        }
211
+        return $classMatchers;
212
+    }
213
+
214
+    /**
215
+     * @param \DOMElement $element
216
+     *
217
+     * @return string
218
+     */
219
+    private function elementToString( \DOMElement $element ) {
220
+        $newDocument = new \DOMDocument();
221
+        $cloned = $element->cloneNode( true );
222
+        $newDocument->appendChild( $newDocument->importNode( $cloned, true ) );
223
+        return trim( $newDocument->saveHTML() );
224
+    }
225 225
 
226 226
 }
Please login to merge, or discard this patch.
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -31,24 +31,24 @@  discard block
 block discarded – undo
31 31
 	 *
32 32
 	 * @return self
33 33
 	 */
34
-	public static function tagMatchingOutline( $htmlOutline ) {
35
-		return new self( $htmlOutline );
34
+	public static function tagMatchingOutline($htmlOutline) {
35
+		return new self($htmlOutline);
36 36
 	}
37 37
 
38 38
 	/**
39 39
 	 * @param string $tagHtmlRepresentation
40 40
 	 */
41
-	public function __construct( $tagHtmlRepresentation ) {
41
+	public function __construct($tagHtmlRepresentation) {
42 42
 		parent::__construct();
43 43
 
44 44
 		$this->tagHtmlOutline = $tagHtmlRepresentation;
45
-		$this->matcher = $this->createMatcherFromHtml( $tagHtmlRepresentation );
45
+		$this->matcher = $this->createMatcherFromHtml($tagHtmlRepresentation);
46 46
 	}
47 47
 
48
-	public function describeTo( Description $description ) {
49
-		$description->appendText( 'tag matching outline `' )
50
-			->appendText( $this->tagHtmlOutline )
51
-			->appendText( '` ' );
48
+	public function describeTo(Description $description) {
49
+		$description->appendText('tag matching outline `')
50
+			->appendText($this->tagHtmlOutline)
51
+			->appendText('` ');
52 52
 	}
53 53
 
54 54
 	/**
@@ -57,12 +57,12 @@  discard block
 block discarded – undo
57 57
 	 *
58 58
 	 * @return bool
59 59
 	 */
60
-	protected function matchesSafelyWithDiagnosticDescription( $item, Description $mismatchDescription ) {
61
-		$result = $this->matcher->matches( $item );
62
-		if ( !$result ) {
63
-			$mismatchDescription->appendText( 'was `' )
64
-				->appendText( $this->elementToString( $item ) )
65
-				->appendText( '`' );
60
+	protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription) {
61
+		$result = $this->matcher->matches($item);
62
+		if (!$result) {
63
+			$mismatchDescription->appendText('was `')
64
+				->appendText($this->elementToString($item))
65
+				->appendText('`');
66 66
 		}
67 67
 		return $result;
68 68
 	}
@@ -72,19 +72,19 @@  discard block
 block discarded – undo
72 72
 	 *
73 73
 	 * @return Matcher
74 74
 	 */
75
-	private function createMatcherFromHtml( $htmlOutline ) {
76
-		$document = $this->parseHtml( $htmlOutline );
77
-		$targetTag = $this->getSingleTagFromThe( $document );
75
+	private function createMatcherFromHtml($htmlOutline) {
76
+		$document = $this->parseHtml($htmlOutline);
77
+		$targetTag = $this->getSingleTagFromThe($document);
78 78
 
79
-		$this->assertTagDoesNotContainChildren( $targetTag );
79
+		$this->assertTagDoesNotContainChildren($targetTag);
80 80
 
81
-		$attributeMatchers = $this->createAttributeMatchers( $htmlOutline, $targetTag );
82
-		$classMatchers = $this->createClassMatchers( $targetTag );
81
+		$attributeMatchers = $this->createAttributeMatchers($htmlOutline, $targetTag);
82
+		$classMatchers = $this->createClassMatchers($targetTag);
83 83
 
84 84
 		return AllOf::allOf(
85
-			new TagNameMatcher( IsEqual::equalTo( $targetTag->tagName ) ),
86
-			call_user_func_array( [ AllOf::class, 'allOf' ], $attributeMatchers ),
87
-			call_user_func_array( [ AllOf::class, 'allOf' ], $classMatchers )
85
+			new TagNameMatcher(IsEqual::equalTo($targetTag->tagName)),
86
+			call_user_func_array([AllOf::class, 'allOf'], $attributeMatchers),
87
+			call_user_func_array([AllOf::class, 'allOf'], $classMatchers)
88 88
 		);
89 89
 	}
90 90
 
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 	 *
94 94
 	 * @return bool
95 95
 	 */
96
-	private function isUnknownTagError( \LibXMLError $error ) {
96
+	private function isUnknownTagError(\LibXMLError $error) {
97 97
 		return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
98 98
 	}
99 99
 
@@ -103,10 +103,10 @@  discard block
 block discarded – undo
103 103
 	 *
104 104
 	 * @return bool
105 105
 	 */
106
-	private function isBooleanAttribute( $inputHtml, $attributeName ) {
107
-		$quotedName = preg_quote( $attributeName, '/' );
106
+	private function isBooleanAttribute($inputHtml, $attributeName) {
107
+		$quotedName = preg_quote($attributeName, '/');
108 108
 
109
-		$attributeHasValueAssigned = preg_match( "/\b{$quotedName}\s*=/ui", $inputHtml );
109
+		$attributeHasValueAssigned = preg_match("/\b{$quotedName}\s*=/ui", $inputHtml);
110 110
 		return !$attributeHasValueAssigned;
111 111
 	}
112 112
 
@@ -116,27 +116,27 @@  discard block
 block discarded – undo
116 116
 	 * @return \DOMDocument
117 117
 	 * @throws \InvalidArgumentException
118 118
 	 */
119
-	private function parseHtml( $html ) {
120
-		$internalErrors = libxml_use_internal_errors( true );
119
+	private function parseHtml($html) {
120
+		$internalErrors = libxml_use_internal_errors(true);
121 121
 		$document = new \DOMDocument();
122 122
 
123 123
 		// phpcs:ignore Generic.PHP.NoSilencedErrors
124
-		if ( [email protected]$document->loadHTML( $html ) ) {
125
-			throw new \InvalidArgumentException( "There was some parsing error of `$html`" );
124
+		if ([email protected]$document->loadHTML($html)) {
125
+			throw new \InvalidArgumentException("There was some parsing error of `$html`");
126 126
 		}
127 127
 
128 128
 		$errors = libxml_get_errors();
129 129
 		libxml_clear_errors();
130
-		libxml_use_internal_errors( $internalErrors );
130
+		libxml_use_internal_errors($internalErrors);
131 131
 
132 132
 		/** @var \LibXMLError $error */
133
-		foreach ( $errors as $error ) {
134
-			if ( $this->isUnknownTagError( $error ) ) {
133
+		foreach ($errors as $error) {
134
+			if ($this->isUnknownTagError($error)) {
135 135
 				continue;
136 136
 			}
137 137
 
138 138
 			throw new \InvalidArgumentException(
139
-				'There was parsing error: ' . trim( $error->message ) . ' on line ' . $error->line
139
+				'There was parsing error: ' . trim($error->message) . ' on line ' . $error->line
140 140
 			);
141 141
 		}
142 142
 
@@ -149,24 +149,24 @@  discard block
 block discarded – undo
149 149
 	 * @return \DOMElement
150 150
 	 * @throws \InvalidArgumentException
151 151
 	 */
152
-	private function getSingleTagFromThe( \DOMDocument $document ) {
153
-		$directChildren = iterator_to_array( $document->documentElement->childNodes );
152
+	private function getSingleTagFromThe(\DOMDocument $document) {
153
+		$directChildren = iterator_to_array($document->documentElement->childNodes);
154 154
 
155
-		$body = array_shift( $directChildren );
156
-		$directChildren = iterator_to_array( $body->childNodes );
155
+		$body = array_shift($directChildren);
156
+		$directChildren = iterator_to_array($body->childNodes);
157 157
 
158
-		if ( count( $directChildren ) !== 1 ) {
158
+		if (count($directChildren) !== 1) {
159 159
 			throw new InvalidArgumentException(
160
-				'Expected exactly 1 tag description, got ' . count( $directChildren )
160
+				'Expected exactly 1 tag description, got ' . count($directChildren)
161 161
 			);
162 162
 		}
163 163
 
164 164
 		return $directChildren[0];
165 165
 	}
166 166
 
167
-	private function assertTagDoesNotContainChildren( \DOMElement $targetTag ) {
168
-		if ( $targetTag->childNodes->length > 0 ) {
169
-			throw new InvalidArgumentException( 'Nested elements are not allowed' );
167
+	private function assertTagDoesNotContainChildren(\DOMElement $targetTag) {
168
+		if ($targetTag->childNodes->length > 0) {
169
+			throw new InvalidArgumentException('Nested elements are not allowed');
170 170
 		}
171 171
 	}
172 172
 
@@ -176,17 +176,17 @@  discard block
 block discarded – undo
176 176
 	 *
177 177
 	 * @return AttributeMatcher[]
178 178
 	 */
179
-	private function createAttributeMatchers( $inputHtml, \DOMElement $targetTag ) {
179
+	private function createAttributeMatchers($inputHtml, \DOMElement $targetTag) {
180 180
 		$attributeMatchers = [];
181 181
 		/** @var \DOMAttr $attribute */
182
-		foreach ( $targetTag->attributes as $attribute ) {
183
-			if ( $attribute->name === 'class' ) {
182
+		foreach ($targetTag->attributes as $attribute) {
183
+			if ($attribute->name === 'class') {
184 184
 				continue;
185 185
 			}
186 186
 
187
-			$attributeMatcher = new AttributeMatcher( IsEqual::equalTo( $attribute->name ) );
188
-			if ( !$this->isBooleanAttribute( $inputHtml, $attribute->name ) ) {
189
-				$attributeMatcher = $attributeMatcher->havingValue( IsEqual::equalTo( $attribute->value ) );
187
+			$attributeMatcher = new AttributeMatcher(IsEqual::equalTo($attribute->name));
188
+			if (!$this->isBooleanAttribute($inputHtml, $attribute->name)) {
189
+				$attributeMatcher = $attributeMatcher->havingValue(IsEqual::equalTo($attribute->value));
190 190
 			}
191 191
 
192 192
 			$attributeMatchers[] = $attributeMatcher;
@@ -199,14 +199,14 @@  discard block
 block discarded – undo
199 199
 	 *
200 200
 	 * @return ClassMatcher[]
201 201
 	 */
202
-	private function createClassMatchers( \DOMElement $targetTag ) {
202
+	private function createClassMatchers(\DOMElement $targetTag) {
203 203
 		$classMatchers = [];
204
-		$classValue = $targetTag->getAttribute( 'class' );
205
-		foreach ( explode( ' ', $classValue ) as $expectedClass ) {
206
-			if ( $expectedClass === '' ) {
204
+		$classValue = $targetTag->getAttribute('class');
205
+		foreach (explode(' ', $classValue) as $expectedClass) {
206
+			if ($expectedClass === '') {
207 207
 				continue;
208 208
 			}
209
-			$classMatchers[] = new ClassMatcher( IsEqual::equalTo( $expectedClass ) );
209
+			$classMatchers[] = new ClassMatcher(IsEqual::equalTo($expectedClass));
210 210
 		}
211 211
 		return $classMatchers;
212 212
 	}
@@ -216,11 +216,11 @@  discard block
 block discarded – undo
216 216
 	 *
217 217
 	 * @return string
218 218
 	 */
219
-	private function elementToString( \DOMElement $element ) {
219
+	private function elementToString(\DOMElement $element) {
220 220
 		$newDocument = new \DOMDocument();
221
-		$cloned = $element->cloneNode( true );
222
-		$newDocument->appendChild( $newDocument->importNode( $cloned, true ) );
223
-		return trim( $newDocument->saveHTML() );
221
+		$cloned = $element->cloneNode(true);
222
+		$newDocument->appendChild($newDocument->importNode($cloned, true));
223
+		return trim($newDocument->saveHTML());
224 224
 	}
225 225
 
226 226
 }
Please login to merge, or discard this patch.
src/HtmlMatcher.php 2 patches
Indentation   +101 added lines, -101 removed lines patch added patch discarded remove patch
@@ -8,106 +8,106 @@
 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
-		$document = new \DOMDocument();
51
-
52
-		$html = $this->escapeScriptTagContents( $html );
53
-
54
-		// phpcs:ignore Generic.PHP.NoSilencedErrors
55
-		if ( [email protected]$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
56
-			$mismatchDescription->appendText( 'there was some parsing error' );
57
-			return false;
58
-		}
59
-
60
-		$errors = libxml_get_errors();
61
-		libxml_clear_errors();
62
-		libxml_use_internal_errors( $internalErrors );
63
-
64
-		$result = true;
65
-		/** @var \LibXMLError $error */
66
-		foreach ( $errors as $error ) {
67
-			if ( $this->isUnknownTagError( $error ) ) {
68
-				continue;
69
-			}
70
-
71
-			$mismatchDescription->appendText( 'there was parsing error: ' )
72
-				->appendText( trim( $error->message ) )
73
-				->appendText( ' on line ' )
74
-				->appendText( $error->line );
75
-			$result = false;
76
-		}
77
-
78
-		if ( $result === false ) {
79
-			return $result;
80
-		}
81
-		$mismatchDescription->appendText( 'valid html piece ' );
82
-
83
-		if ( $this->elementMatcher ) {
84
-			$result = $this->elementMatcher->matches( $document );
85
-			$this->elementMatcher->describeMismatch( $document, $mismatchDescription );
86
-		}
87
-
88
-		$mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
89
-
90
-		return $result;
91
-	}
92
-
93
-	/**
94
-	 * @param \LibXMLError $error
95
-	 *
96
-	 * @return bool
97
-	 */
98
-	private function isUnknownTagError( \LibXMLError $error ) {
99
-		return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
100
-	}
101
-
102
-	/**
103
-	 * @param string $html
104
-	 *
105
-	 * @return string HTML
106
-	 */
107
-	private function escapeScriptTagContents( $html ) {
108
-		return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
109
-			return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
110
-		}, $html );
111
-	}
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
+        $document = new \DOMDocument();
51
+
52
+        $html = $this->escapeScriptTagContents( $html );
53
+
54
+        // phpcs:ignore Generic.PHP.NoSilencedErrors
55
+        if ( [email protected]$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) {
56
+            $mismatchDescription->appendText( 'there was some parsing error' );
57
+            return false;
58
+        }
59
+
60
+        $errors = libxml_get_errors();
61
+        libxml_clear_errors();
62
+        libxml_use_internal_errors( $internalErrors );
63
+
64
+        $result = true;
65
+        /** @var \LibXMLError $error */
66
+        foreach ( $errors as $error ) {
67
+            if ( $this->isUnknownTagError( $error ) ) {
68
+                continue;
69
+            }
70
+
71
+            $mismatchDescription->appendText( 'there was parsing error: ' )
72
+                ->appendText( trim( $error->message ) )
73
+                ->appendText( ' on line ' )
74
+                ->appendText( $error->line );
75
+            $result = false;
76
+        }
77
+
78
+        if ( $result === false ) {
79
+            return $result;
80
+        }
81
+        $mismatchDescription->appendText( 'valid html piece ' );
82
+
83
+        if ( $this->elementMatcher ) {
84
+            $result = $this->elementMatcher->matches( $document );
85
+            $this->elementMatcher->describeMismatch( $document, $mismatchDescription );
86
+        }
87
+
88
+        $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html );
89
+
90
+        return $result;
91
+    }
92
+
93
+    /**
94
+     * @param \LibXMLError $error
95
+     *
96
+     * @return bool
97
+     */
98
+    private function isUnknownTagError( \LibXMLError $error ) {
99
+        return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE;
100
+    }
101
+
102
+    /**
103
+     * @param string $html
104
+     *
105
+     * @return string HTML
106
+     */
107
+    private function escapeScriptTagContents( $html ) {
108
+        return preg_replace_callback( '#(<script.*>)(.*)(</script>)#isU', function ( $matches ) {
109
+            return $matches[1] . str_replace( '</', '<\/', $matches[2] ) . $matches[3];
110
+        }, $html );
111
+    }
112 112
 
113 113
 }
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -24,18 +24,18 @@  discard block
 block discarded – undo
24 24
 	 *
25 25
 	 * @return self
26 26
 	 */
27
-	public static function htmlPiece( Matcher $elementMatcher = null ) {
28
-		return new static( $elementMatcher );
27
+	public static function htmlPiece(Matcher $elementMatcher = null) {
28
+		return new static($elementMatcher);
29 29
 	}
30 30
 
31
-	private function __construct( Matcher $elementMatcher = null ) {
31
+	private function __construct(Matcher $elementMatcher = null) {
32 32
 		$this->elementMatcher = $elementMatcher;
33 33
 	}
34 34
 
35
-	public function describeTo( Description $description ) {
36
-		$description->appendText( 'valid html piece ' );
37
-		if ( $this->elementMatcher ) {
38
-			$description->appendDescriptionOf( $this->elementMatcher );
35
+	public function describeTo(Description $description) {
36
+		$description->appendText('valid html piece ');
37
+		if ($this->elementMatcher) {
38
+			$description->appendDescriptionOf($this->elementMatcher);
39 39
 		}
40 40
 	}
41 41
 
@@ -45,47 +45,47 @@  discard block
 block discarded – undo
45 45
 	 *
46 46
 	 * @return bool
47 47
 	 */
48
-	protected function matchesWithDiagnosticDescription( $html, Description $mismatchDescription ) {
49
-		$internalErrors = libxml_use_internal_errors( true );
48
+	protected function matchesWithDiagnosticDescription($html, Description $mismatchDescription) {
49
+		$internalErrors = libxml_use_internal_errors(true);
50 50
 		$document = new \DOMDocument();