@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * |
| 72 | 72 | * @param Pipeline|null $pipeline |
| 73 | 73 | */ |
| 74 | - public function __construct( ?Pipeline $pipeline = null ) { |
|
| 74 | + public function __construct(?Pipeline $pipeline = null) { |
|
| 75 | 75 | $this->pipeline = $pipeline; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -83,10 +83,10 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @throws RuntimeException If the handler does not use the CallbacksHandler trait. |
| 85 | 85 | */ |
| 86 | - public function registerCallbacksHandler( AbstractHandler $class ) { |
|
| 86 | + public function registerCallbacksHandler(AbstractHandler $class) { |
|
| 87 | 87 | //check: $class must use CallbacksHandler trait |
| 88 | - if ( !in_array( CallbacksHandler::class, array_merge( class_uses( $class ), class_uses( get_parent_class( $class ) ) ) ) ) { |
|
| 89 | - throw new RuntimeException( "Class passed to " . __METHOD__ . " must use " . CallbacksHandler::class . " trait." ); |
|
| 88 | + if (!in_array(CallbacksHandler::class, array_merge(class_uses($class), class_uses(get_parent_class($class))))) { |
|
| 89 | + throw new RuntimeException("Class passed to ".__METHOD__." must use ".CallbacksHandler::class." trait."); |
|
| 90 | 90 | } |
| 91 | 91 | $this->callbacksHandler = $class; |
| 92 | 92 | $this->pipeline = $this->callbacksHandler->getPipeline(); |
@@ -103,18 +103,18 @@ discard block |
||
| 103 | 103 | * @return mixed Return value from the handler's method. |
| 104 | 104 | * @throws ReflectionException If a method cannot be found/reflected. |
| 105 | 105 | */ |
| 106 | - public function __call( string $name, array $arguments = [] ) { |
|
| 106 | + public function __call(string $name, array $arguments = []) { |
|
| 107 | 107 | |
| 108 | 108 | // Create a ReflectionMethod instance for the method being called on the callback handler |
| 109 | - $reflector = new ReflectionMethod( $this->callbacksHandler, $name ); |
|
| 109 | + $reflector = new ReflectionMethod($this->callbacksHandler, $name); |
|
| 110 | 110 | |
| 111 | 111 | // If the method is not public, make it accessible |
| 112 | - if ( !$reflector->isPublic() ) { |
|
| 113 | - $reflector->setAccessible( true ); |
|
| 112 | + if (!$reflector->isPublic()) { |
|
| 113 | + $reflector->setAccessible(true); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // Invoke the method on the callback handler with the provided arguments |
| 117 | - return $reflector->invoke( $this->callbacksHandler, $arguments[ 0 ] ?? null ); |
|
| 117 | + return $reflector->invoke($this->callbacksHandler, $arguments[0] ?? null); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -128,10 +128,10 @@ discard block |
||
| 128 | 128 | * @return string The processed segment, with tags and text handled appropriately. |
| 129 | 129 | */ |
| 130 | 130 | |
| 131 | - public function transform( string $segment ): string { |
|
| 131 | + public function transform(string $segment): string { |
|
| 132 | 132 | // Split input into Unicode codepoints for accurate char-by-char iteration. |
| 133 | - $originalSplit = preg_split( '//u', $segment, -1, PREG_SPLIT_NO_EMPTY ); |
|
| 134 | - if ( empty( $originalSplit ) ) { |
|
| 133 | + $originalSplit = preg_split('//u', $segment, -1, PREG_SPLIT_NO_EMPTY); |
|
| 134 | + if (empty($originalSplit)) { |
|
| 135 | 135 | return ''; |
| 136 | 136 | } |
| 137 | 137 | |
@@ -140,36 +140,36 @@ discard block |
||
| 140 | 140 | $plain_text_buffer = ''; |
| 141 | 141 | $in_quote_char = ''; |
| 142 | 142 | $output = ''; |
| 143 | - $charCount = count( $originalSplit ); |
|
| 143 | + $charCount = count($originalSplit); |
|
| 144 | 144 | |
| 145 | - foreach ( $originalSplit as $idx => $char ) { |
|
| 146 | - switch ( $state ) { |
|
| 145 | + foreach ($originalSplit as $idx => $char) { |
|
| 146 | + switch ($state) { |
|
| 147 | 147 | case static::STATE_PLAINTEXT: |
| 148 | - $this->handlePlainTextState( $char, $state, $html_buffer, $plain_text_buffer, $output ); |
|
| 148 | + $this->handlePlainTextState($char, $state, $html_buffer, $plain_text_buffer, $output); |
|
| 149 | 149 | break; |
| 150 | 150 | case static::STATE_HTML: |
| 151 | - $this->handleHtmlState( $char, $idx, $charCount, $state, $html_buffer, $plain_text_buffer, $output, $in_quote_char ); |
|
| 151 | + $this->handleHtmlState($char, $idx, $charCount, $state, $html_buffer, $plain_text_buffer, $output, $in_quote_char); |
|
| 152 | 152 | break; |
| 153 | 153 | case static::STATE_COMMENT: |
| 154 | - $this->handleCommentState( $char, $state, $html_buffer, $output ); |
|
| 154 | + $this->handleCommentState($char, $state, $html_buffer, $output); |
|
| 155 | 155 | break; |
| 156 | 156 | case static::STATE_JS_CSS: |
| 157 | - $this->handleJsCssState( $char, $state, $html_buffer, $output ); |
|
| 157 | + $this->handleJsCssState($char, $state, $html_buffer, $output); |
|
| 158 | 158 | break; |
| 159 | 159 | } |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | // HTML Partial at the end, treat as invalid and preserve the string content |
| 163 | - if ( !empty( $html_buffer ) ) { |
|
| 164 | - if ( $this->_isTagValid( $html_buffer ) && null !== $this->pipeline ) { |
|
| 163 | + if (!empty($html_buffer)) { |
|
| 164 | + if ($this->_isTagValid($html_buffer) && null !== $this->pipeline) { |
|
| 165 | 165 | $this->_setSegmentContainsMarkup(); |
| 166 | 166 | } |
| 167 | - $output .= $this->_fixWrongBuffer( $html_buffer ); |
|
| 167 | + $output .= $this->_fixWrongBuffer($html_buffer); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | // Any trailing plain text: finalize it. |
| 171 | - if ( '' !== $plain_text_buffer ) { |
|
| 172 | - $output .= $this->_finalizePlainText( $plain_text_buffer ); |
|
| 171 | + if ('' !== $plain_text_buffer) { |
|
| 172 | + $output .= $this->_finalizePlainText($plain_text_buffer); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | return $output; |
@@ -178,18 +178,18 @@ discard block |
||
| 178 | 178 | /** |
| 179 | 179 | * Handles character processing when in the STATE_PLAINTEXT. |
| 180 | 180 | */ |
| 181 | - private function handlePlainTextState( string $char, int &$state, string &$html_buffer, string &$plain_text_buffer, string &$output ): void { |
|
| 182 | - switch ( $char ) { |
|
| 181 | + private function handlePlainTextState(string $char, int &$state, string &$html_buffer, string &$plain_text_buffer, string &$output): void { |
|
| 182 | + switch ($char) { |
|
| 183 | 183 | case '<': |
| 184 | 184 | // Potential new tag starts; finalize plain text so far. |
| 185 | 185 | $state = static::STATE_HTML; |
| 186 | 186 | $html_buffer .= $char; |
| 187 | - $output .= $this->_finalizePlainText( $plain_text_buffer ); |
|
| 187 | + $output .= $this->_finalizePlainText($plain_text_buffer); |
|
| 188 | 188 | $plain_text_buffer = ''; |
| 189 | 189 | break; |
| 190 | 190 | case '>': |
| 191 | 191 | // Unescaped '>' in plaintext; treat as literal via error handing. |
| 192 | - $plain_text_buffer .= $this->_fixWrongBuffer( $char ); |
|
| 192 | + $plain_text_buffer .= $this->_fixWrongBuffer($char); |
|
| 193 | 193 | break; |
| 194 | 194 | default: |
| 195 | 195 | // Collect as plain text. |
@@ -203,27 +203,27 @@ discard block |
||
| 203 | 203 | * This method acts as a dispatcher based on the character. |
| 204 | 204 | * Assumes parser state variables (state, html_buffer, etc.) are now class properties. |
| 205 | 205 | */ |
| 206 | - private function handleHtmlState( string $char, int $idx, int $charCount, int &$state, string &$html_buffer, string &$plain_text_buffer, string &$output, string &$in_quote_char ): void { |
|
| 207 | - switch ( $char ) { |
|
| 206 | + private function handleHtmlState(string $char, int $idx, int $charCount, int &$state, string &$html_buffer, string &$plain_text_buffer, string &$output, string &$in_quote_char): void { |
|
| 207 | + switch ($char) { |
|
| 208 | 208 | case '<': |
| 209 | - $this->onLessThanInHtml( $char, $output, $html_buffer ); |
|
| 209 | + $this->onLessThanInHtml($char, $output, $html_buffer); |
|
| 210 | 210 | break; |
| 211 | 211 | case '>': |
| 212 | - $this->onGreaterThanInHtml( $char, $state, $html_buffer, $output, $in_quote_char ); |
|
| 212 | + $this->onGreaterThanInHtml($char, $state, $html_buffer, $output, $in_quote_char); |
|
| 213 | 213 | break; |
| 214 | 214 | case '"': |
| 215 | 215 | case '\'': |
| 216 | - $this->onQuoteInHtml( $char, $html_buffer, $in_quote_char ); |
|
| 216 | + $this->onQuoteInHtml($char, $html_buffer, $in_quote_char); |
|
| 217 | 217 | break; |
| 218 | 218 | case '-': |
| 219 | - $this->onDashInHtml( $char, $state, $html_buffer ); |
|
| 219 | + $this->onDashInHtml($char, $state, $html_buffer); |
|
| 220 | 220 | break; |
| 221 | 221 | case ' ': |
| 222 | 222 | case "\n": |
| 223 | - $this->onWhitespaceInHtml( $char, $state, $html_buffer, $output ); |
|
| 223 | + $this->onWhitespaceInHtml($char, $state, $html_buffer, $output); |
|
| 224 | 224 | break; |
| 225 | 225 | default: |
| 226 | - $this->onDefaultCharInHtml( $char, $idx, $charCount, $state, $html_buffer, $plain_text_buffer ); |
|
| 226 | + $this->onDefaultCharInHtml($char, $idx, $charCount, $state, $html_buffer, $plain_text_buffer); |
|
| 227 | 227 | break; |
| 228 | 228 | } |
| 229 | 229 | } |
@@ -231,37 +231,37 @@ discard block |
||
| 231 | 231 | /** |
| 232 | 232 | * Handles the '<' character in the HTML state. |
| 233 | 233 | */ |
| 234 | - private function onLessThanInHtml( string $char, string &$output, string &$html_buffer ): void { |
|
| 234 | + private function onLessThanInHtml(string $char, string &$output, string &$html_buffer): void { |
|
| 235 | 235 | // If we found a second less than symbol, the first one IS NOT a tag. |
| 236 | 236 | // See https://www.w3.org/TR/xml/#charsets |
| 237 | - $output .= $this->_fixWrongBuffer( $html_buffer ); |
|
| 237 | + $output .= $this->_fixWrongBuffer($html_buffer); |
|
| 238 | 238 | $html_buffer = $char; |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | /** |
| 242 | 242 | * Handles the '>' character in the HTML state. |
| 243 | 243 | */ |
| 244 | - private function onGreaterThanInHtml( string $char, int &$state, string &$html_buffer, string &$output, string &$in_quote_char ): void { |
|
| 244 | + private function onGreaterThanInHtml(string $char, int &$state, string &$html_buffer, string &$output, string &$in_quote_char): void { |
|
| 245 | 245 | // End of current tag. Special-case for <script> or <style> blocks. |
| 246 | - if ( $this->isScriptOrStyleTag( $html_buffer ) ) { |
|
| 246 | + if ($this->isScriptOrStyleTag($html_buffer)) { |
|
| 247 | 247 | $html_buffer .= $char; |
| 248 | - $state = static::STATE_JS_CSS; |
|
| 248 | + $state = static::STATE_JS_CSS; |
|
| 249 | 249 | |
| 250 | 250 | return; |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | $in_quote_char = ''; |
| 254 | 254 | $state = static::STATE_PLAINTEXT; |
| 255 | - $html_buffer .= $char; |
|
| 255 | + $html_buffer .= $char; |
|
| 256 | 256 | |
| 257 | 257 | // Validate and finalize HTML tag. Invalid tags are corrected/errors handled. |
| 258 | - if ( $this->_isTagValid( $html_buffer ) ) { |
|
| 259 | - $output .= $this->_finalizeMarkupTag( $html_buffer ); |
|
| 260 | - if ( null !== $this->pipeline ) { |
|
| 258 | + if ($this->_isTagValid($html_buffer)) { |
|
| 259 | + $output .= $this->_finalizeMarkupTag($html_buffer); |
|
| 260 | + if (null !== $this->pipeline) { |
|
| 261 | 261 | $this->_setSegmentContainsMarkup(); |
| 262 | 262 | } |
| 263 | 263 | } else { |
| 264 | - $output .= $this->_fixWrongBuffer( $html_buffer ); |
|
| 264 | + $output .= $this->_fixWrongBuffer($html_buffer); |
|
| 265 | 265 | } |
| 266 | 266 | $html_buffer = ''; |
| 267 | 267 | } |
@@ -269,11 +269,11 @@ discard block |
||
| 269 | 269 | /** |
| 270 | 270 | * Handles quote characters ('"' or "'") in the HTML state. |
| 271 | 271 | */ |
| 272 | - private function onQuoteInHtml( string $char, string &$html_buffer, string &$in_quote_char ): void { |
|
| 272 | + private function onQuoteInHtml(string $char, string &$html_buffer, string &$in_quote_char): void { |
|
| 273 | 273 | // Track entry/exit into quoted attributes. |
| 274 | - if ( $char == $in_quote_char ) { |
|
| 274 | + if ($char == $in_quote_char) { |
|
| 275 | 275 | $in_quote_char = ''; // Exiting quote |
| 276 | - } elseif ( $in_quote_char == '' ) { |
|
| 276 | + } elseif ($in_quote_char == '') { |
|
| 277 | 277 | $in_quote_char = $char; // Entering quote |
| 278 | 278 | } |
| 279 | 279 | $html_buffer .= $char; |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | /** |
| 283 | 283 | * Handles the '-' character in the HTML state. |
| 284 | 284 | */ |
| 285 | - private function onDashInHtml( string $char, int &$state, string &$html_buffer ): void { |
|
| 285 | + private function onDashInHtml(string $char, int &$state, string &$html_buffer): void { |
|
| 286 | 286 | // Detect HTML comment opening ('<!--'). |
| 287 | - if ( $html_buffer === '<!-' ) { |
|
| 287 | + if ($html_buffer === '<!-') { |
|
| 288 | 288 | $state = static::STATE_COMMENT; |
| 289 | 289 | } |
| 290 | 290 | $html_buffer .= $char; |
@@ -293,13 +293,13 @@ discard block |
||
| 293 | 293 | /** |
| 294 | 294 | * Handles whitespace characters in the HTML state. |
| 295 | 295 | */ |
| 296 | - private function onWhitespaceInHtml( string $char, int &$state, string &$html_buffer, string &$output ): void { |
|
| 296 | + private function onWhitespaceInHtml(string $char, int &$state, string &$html_buffer, string &$output): void { |
|
| 297 | 297 | // Space or newline immediately after '<' is invalid. |
| 298 | - if ( $html_buffer === '<' ) { |
|
| 298 | + if ($html_buffer === '<') { |
|
| 299 | 299 | $state = static::STATE_PLAINTEXT; |
| 300 | - $output .= $this->_fixWrongBuffer( '<' . $char ); |
|
| 300 | + $output .= $this->_fixWrongBuffer('<'.$char); |
|
| 301 | 301 | $html_buffer = ''; |
| 302 | - if ( null !== $this->pipeline ) { |
|
| 302 | + if (null !== $this->pipeline) { |
|
| 303 | 303 | $this->_setSegmentContainsMarkup(); |
| 304 | 304 | } |
| 305 | 305 | |
@@ -311,12 +311,12 @@ discard block |
||
| 311 | 311 | /** |
| 312 | 312 | * Handles any other default character in the HTML state. |
| 313 | 313 | */ |
| 314 | - private function onDefaultCharInHtml( string $char, int $idx, int $charCount, int &$state, string &$html_buffer, string &$plain_text_buffer ): void { |
|
| 314 | + private function onDefaultCharInHtml(string $char, int $idx, int $charCount, int &$state, string &$html_buffer, string &$plain_text_buffer): void { |
|
| 315 | 315 | $html_buffer .= $char; |
| 316 | 316 | // End of input: treat buffer as plain text if not a valid tag. |
| 317 | - if ( $idx === ( $charCount - 1 ) && !$this->_isTagValid( $html_buffer ) ) { |
|
| 317 | + if ($idx === ($charCount - 1) && !$this->_isTagValid($html_buffer)) { |
|
| 318 | 318 | $state = static::STATE_PLAINTEXT; // Error: not a valid tag |
| 319 | - $plain_text_buffer .= $this->_fixWrongBuffer( $html_buffer ); |
|
| 319 | + $plain_text_buffer .= $this->_fixWrongBuffer($html_buffer); |
|
| 320 | 320 | $html_buffer = ''; |
| 321 | 321 | } |
| 322 | 322 | } |
@@ -324,14 +324,14 @@ discard block |
||
| 324 | 324 | /** |
| 325 | 325 | * Handles character processing when in the STATE_COMMENT. |
| 326 | 326 | */ |
| 327 | - private function handleCommentState( string $char, int &$state, string &$html_buffer, string &$output ): void { |
|
| 327 | + private function handleCommentState(string $char, int &$state, string &$html_buffer, string &$output): void { |
|
| 328 | 328 | $html_buffer .= $char; |
| 329 | 329 | // Check for the end of a comment: '-->' |
| 330 | - if ( $char === '>' && substr( $html_buffer, -3 ) === '-->' ) { |
|
| 330 | + if ($char === '>' && substr($html_buffer, -3) === '-->') { |
|
| 331 | 331 | $state = static::STATE_PLAINTEXT; |
| 332 | - $output .= $this->_finalizeScriptTag( $html_buffer ); |
|
| 332 | + $output .= $this->_finalizeScriptTag($html_buffer); |
|
| 333 | 333 | $html_buffer = ''; |
| 334 | - if ( null !== $this->pipeline ) { |
|
| 334 | + if (null !== $this->pipeline) { |
|
| 335 | 335 | $this->_setSegmentContainsMarkup(); |
| 336 | 336 | } |
| 337 | 337 | } |
@@ -340,16 +340,16 @@ discard block |
||
| 340 | 340 | /** |
| 341 | 341 | * Handles character processing when in the STATE_JS_CSS. |
| 342 | 342 | */ |
| 343 | - private function handleJsCssState( string $char, int &$state, string &$html_buffer, string &$output ): void { |
|
| 343 | + private function handleJsCssState(string $char, int &$state, string &$html_buffer, string &$output): void { |
|
| 344 | 344 | $html_buffer .= $char; |
| 345 | 345 | // Detect close: e.g., '</script>' or '</style>' |
| 346 | - if ( $char === '>' ) { |
|
| 347 | - if ( in_array( substr( $html_buffer, -6 ), [ 'cript>', 'style>' ], true ) ) { |
|
| 346 | + if ($char === '>') { |
|
| 347 | + if (in_array(substr($html_buffer, -6), ['cript>', 'style>'], true)) { |
|
| 348 | 348 | $state = static::STATE_PLAINTEXT; |
| 349 | - $this->_isTagValid( $html_buffer ); |
|
| 350 | - $output .= $this->_finalizeScriptTag( $html_buffer ); |
|
| 349 | + $this->_isTagValid($html_buffer); |
|
| 350 | + $output .= $this->_finalizeScriptTag($html_buffer); |
|
| 351 | 351 | $html_buffer = ''; |
| 352 | - if ( null !== $this->pipeline ) { |
|
| 352 | + if (null !== $this->pipeline) { |
|
| 353 | 353 | $this->_setSegmentContainsMarkup(); |
| 354 | 354 | } |
| 355 | 355 | } |
@@ -359,9 +359,9 @@ discard block |
||
| 359 | 359 | /** |
| 360 | 360 | * Checks if the buffered HTML is the beginning of a script or style tag. |
| 361 | 361 | */ |
| 362 | - private function isScriptOrStyleTag( string $html_buffer ): bool { |
|
| 362 | + private function isScriptOrStyleTag(string $html_buffer): bool { |
|
| 363 | 363 | // A tag starts with '<script' or '<style'. This also covers variants with spaces or attributes. |
| 364 | - return in_array( substr( $html_buffer, 0, 8 ), [ '<script ', '<style', '<script', '<style ' ] ); |
|
| 364 | + return in_array(substr($html_buffer, 0, 8), ['<script ', '<style', '<script', '<style ']); |
|
| 365 | 365 | } |
| 366 | 366 | |
| 367 | 367 | } |