@@ -31,24 +31,24 @@ discard block |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 |
||
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 ( !@$document->loadHTML( $html ) ) { |
|
125 | - throw new \InvalidArgumentException( "There was some parsing error of `$html`" ); |
|
124 | + if (!@$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 |
||
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 |
||
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 |
||
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 |
||
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 | } |
@@ -24,18 +24,18 @@ discard block |
||
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 |
||
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(); |
51 | 51 | |
52 | - $html = $this->escapeScriptTagContents( $html ); |
|
52 | + $html = $this->escapeScriptTagContents($html); |
|
53 | 53 | |
54 | 54 | // phpcs:ignore Generic.PHP.NoSilencedErrors |
55 | - if ( !@$document->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ) ) ) { |
|
56 | - $mismatchDescription->appendText( 'there was some parsing error' ); |
|
55 | + if (!@$document->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))) { |
|
56 | + $mismatchDescription->appendText('there was some parsing error'); |
|
57 | 57 | return false; |
58 | 58 | } |
59 | 59 | |
60 | 60 | $errors = libxml_get_errors(); |
61 | 61 | libxml_clear_errors(); |
62 | - libxml_use_internal_errors( $internalErrors ); |
|
62 | + libxml_use_internal_errors($internalErrors); |
|
63 | 63 | |
64 | 64 | $result = true; |
65 | 65 | /** @var \LibXMLError $error */ |
66 | - foreach ( $errors as $error ) { |
|
67 | - if ( $this->isUnknownTagError( $error ) ) { |
|
66 | + foreach ($errors as $error) { |
|
67 | + if ($this->isUnknownTagError($error)) { |
|
68 | 68 | continue; |
69 | 69 | } |
70 | 70 | |
71 | - $mismatchDescription->appendText( 'there was parsing error: ' ) |
|
72 | - ->appendText( trim( $error->message ) ) |
|
73 | - ->appendText( ' on line ' ) |
|
74 | - ->appendText( $error->line ); |
|
71 | + $mismatchDescription->appendText('there was parsing error: ') |
|
72 | + ->appendText(trim($error->message)) |
|
73 | + ->appendText(' on line ') |
|
74 | + ->appendText($error->line); |
|
75 | 75 | $result = false; |
76 | 76 | } |
77 | 77 | |
78 | - if ( $result === false ) { |
|
78 | + if ($result === false) { |
|
79 | 79 | return $result; |
80 | 80 | } |
81 | - $mismatchDescription->appendText( 'valid html piece ' ); |
|
81 | + $mismatchDescription->appendText('valid html piece '); |
|
82 | 82 | |
83 | - if ( $this->elementMatcher ) { |
|
84 | - $result = $this->elementMatcher->matches( $document ); |
|
85 | - $this->elementMatcher->describeMismatch( $document, $mismatchDescription ); |
|
83 | + if ($this->elementMatcher) { |
|
84 | + $result = $this->elementMatcher->matches($document); |
|
85 | + $this->elementMatcher->describeMismatch($document, $mismatchDescription); |
|
86 | 86 | } |
87 | 87 | |
88 | - $mismatchDescription->appendText( "\nActual html:\n" )->appendText( $html ); |
|
88 | + $mismatchDescription->appendText("\nActual html:\n")->appendText($html); |
|
89 | 89 | |
90 | 90 | return $result; |
91 | 91 | } |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * |
96 | 96 | * @return bool |
97 | 97 | */ |
98 | - private function isUnknownTagError( \LibXMLError $error ) { |
|
98 | + private function isUnknownTagError(\LibXMLError $error) { |
|
99 | 99 | return $error->code === self::XML_UNKNOWN_TAG_ERROR_CODE; |
100 | 100 | } |
101 | 101 | |
@@ -104,10 +104,10 @@ discard block |
||
104 | 104 | * |
105 | 105 | * @return string HTML |
106 | 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 ); |
|
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 | 111 | } |
112 | 112 | |
113 | 113 | } |