Passed
Push — master ( bb500e...eead79 )
by Domenico
02:09
created
src/Filters/Html/HtmlParser.php 1 patch
Spacing   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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.
@@ -201,50 +201,50 @@  discard block
 block discarded – undo
201 201
     /**
202 202
      * Handles character processing when in the STATE_HTML.
203 203
      */
204
-    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 {
205
-        switch ( $char ) {
204
+    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 {
205
+        switch ($char) {
206 206
             case '<':
207 207
                 // If we found a second less than symbol, the first one IS NOT a tag.
208 208
                 // See https://www.w3.org/TR/xml/#charsets
209
-                $output      .= $this->_fixWrongBuffer( $html_buffer );
209
+                $output .= $this->_fixWrongBuffer($html_buffer);
210 210
                 $html_buffer = $char;
211 211
                 break;
212 212
             case '>':
213 213
                 // End of current tag. Special-case for <script> or <style> blocks.
214
-                if ( $this->isScriptOrStyleTag( $html_buffer ) ) {
214
+                if ($this->isScriptOrStyleTag($html_buffer)) {
215 215
                     $html_buffer .= $char;
216
-                    $state       = static::STATE_JS_CSS;
216
+                    $state = static::STATE_JS_CSS;
217 217
                     break;
218 218
                 }
219 219
 
220 220
                 $in_quote_char = '';
221 221
                 $state         = static::STATE_PLAINTEXT;
222
-                $html_buffer   .= $char;
222
+                $html_buffer .= $char;
223 223
 
224 224
                 // Validate and finalize HTML tag. Invalid tags are corrected/errors handled.
225
-                if ( $this->_isTagValid( $html_buffer ) ) {
226
-                    $output .= $this->_finalizeMarkupTag( $html_buffer );
227
-                    if ( null !== $this->pipeline ) {
225
+                if ($this->_isTagValid($html_buffer)) {
226
+                    $output .= $this->_finalizeMarkupTag($html_buffer);
227
+                    if (null !== $this->pipeline) {
228 228
                         $this->_setSegmentContainsMarkup();
229 229
                     }
230 230
                 } else {
231
-                    $output .= $this->_fixWrongBuffer( $html_buffer );
231
+                    $output .= $this->_fixWrongBuffer($html_buffer);
232 232
                 }
233 233
                 $html_buffer = '';
234 234
                 break;
235 235
             case '"':
236 236
             case '\'':
237 237
                 // Track entry/exit into quoted attributes.
238
-                if ( $char == $in_quote_char ) {
238
+                if ($char == $in_quote_char) {
239 239
                     $in_quote_char = ''; // Exiting quote
240
-                } elseif ( $in_quote_char == '' ) {
240
+                } elseif ($in_quote_char == '') {
241 241
                     $in_quote_char = $char; // Entering quote
242 242
                 }
243 243
                 $html_buffer .= $char;
244 244
                 break;
245 245
             case '-':
246 246
                 // Detect HTML comment opening ('<!--').
247
-                if ( $html_buffer === '<!-' ) {
247
+                if ($html_buffer === '<!-') {
248 248
                     $state = static::STATE_COMMENT;
249 249
                 }
250 250
                 $html_buffer .= $char;
@@ -252,11 +252,11 @@  discard block
 block discarded – undo
252 252
             case ' ':
253 253
             case "\n":
254 254
                 // Space or newline immediately after '<' is invalid.
255
-                if ( $html_buffer === '<' ) {
255
+                if ($html_buffer === '<') {
256 256
                     $state       = static::STATE_PLAINTEXT;
257
-                    $output      .= $this->_fixWrongBuffer( '<' . $char );
257
+                    $output .= $this->_fixWrongBuffer('<'.$char);
258 258
                     $html_buffer = '';
259
-                    if ( null !== $this->pipeline ) {
259
+                    if (null !== $this->pipeline) {
260 260
                         $this->_setSegmentContainsMarkup();
261 261
                     }
262 262
                     break;
@@ -266,9 +266,9 @@  discard block
 block discarded – undo
266 266
             default:
267 267
                 $html_buffer .= $char;
268 268
                 // End of input: treat buffer as plain text if not a valid tag.
269
-                if ( $idx === ( $charCount - 1 ) && !$this->_isTagValid( $html_buffer ) ) {
269
+                if ($idx === ($charCount - 1) && !$this->_isTagValid($html_buffer)) {
270 270
                     $state             = static::STATE_PLAINTEXT; // Error: not a valid tag
271
-                    $plain_text_buffer .= $this->_fixWrongBuffer( $html_buffer );
271
+                    $plain_text_buffer .= $this->_fixWrongBuffer($html_buffer);
272 272
                     $html_buffer       = '';
273 273
                 }
274 274
                 break;
@@ -278,14 +278,14 @@  discard block
 block discarded – undo
278 278
     /**
279 279
      * Handles character processing when in the STATE_COMMENT.
280 280
      */
281
-    private function handleCommentState( string $char, int &$state, string &$html_buffer, string &$output ): void {
281
+    private function handleCommentState(string $char, int &$state, string &$html_buffer, string &$output): void {
282 282
         $html_buffer .= $char;
283 283
         // Check for the end of a comment: '-->'
284
-        if ( $char === '>' && substr( $html_buffer, -3 ) === '-->' ) {
284
+        if ($char === '>' && substr($html_buffer, -3) === '-->') {
285 285
             $state       = static::STATE_PLAINTEXT;
286
-            $output      .= $this->_finalizeScriptTag( $html_buffer );
286
+            $output .= $this->_finalizeScriptTag($html_buffer);
287 287
             $html_buffer = '';
288
-            if ( null !== $this->pipeline ) {
288
+            if (null !== $this->pipeline) {
289 289
                 $this->_setSegmentContainsMarkup();
290 290
             }
291 291
         }
@@ -294,16 +294,16 @@  discard block
 block discarded – undo
294 294
     /**
295 295
      * Handles character processing when in the STATE_JS_CSS.
296 296
      */
297
-    private function handleJsCssState( string $char, int &$state, string &$html_buffer, string &$output ): void {
297
+    private function handleJsCssState(string $char, int &$state, string &$html_buffer, string &$output): void {
298 298
         $html_buffer .= $char;
299 299
         // Detect close: e.g., '</script>' or '</style>'
300
-        if ( $char === '>' ) {
301
-            if ( in_array( substr( $html_buffer, -6 ), [ 'cript>', 'style>' ], true ) ) {
300
+        if ($char === '>') {
301
+            if (in_array(substr($html_buffer, -6), ['cript>', 'style>'], true)) {
302 302
                 $state = static::STATE_PLAINTEXT;
303
-                $this->_isTagValid( $html_buffer );
304
-                $output      .= $this->_finalizeScriptTag( $html_buffer );
303
+                $this->_isTagValid($html_buffer);
304
+                $output .= $this->_finalizeScriptTag($html_buffer);
305 305
                 $html_buffer = '';
306
-                if ( null !== $this->pipeline ) {
306
+                if (null !== $this->pipeline) {
307 307
                     $this->_setSegmentContainsMarkup();
308 308
                 }
309 309
             }
@@ -313,8 +313,8 @@  discard block
 block discarded – undo
313 313
     /**
314 314
      * Checks if the buffered HTML is the beginning of a script or style tag.
315 315
      */
316
-    private function isScriptOrStyleTag( string $html_buffer ): bool {
316
+    private function isScriptOrStyleTag(string $html_buffer): bool {
317 317
         // A tag starts with '<script' or '<style'. This also covers variants with spaces or attributes.
318
-        return in_array( substr( $html_buffer, 0, 8 ), [ '<script ', '<style', '<script', '<style ' ] );
318
+        return in_array(substr($html_buffer, 0, 8), ['<script ', '<style', '<script', '<style ']);
319 319
     }
320 320
 }
Please login to merge, or discard this patch.
src/Filters/DataRefRestore.php 1 patch
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -25,23 +25,23 @@  discard block
 block discarded – undo
25 25
      * @inheritDoc
26 26
      * @throws Exception
27 27
      */
28
-    public function transform( string $segment ): string {
28
+    public function transform(string $segment): string {
29 29
 
30
-        if ( empty( $this->dataRefMap ) ) {
30
+        if (empty($this->dataRefMap)) {
31 31
             $this->dataRefMap = $this->pipeline->getDataRefMap();
32 32
         }
33 33
 
34
-        if ( empty( $this->dataRefMap ) ) {
35
-            $segment = $this->restoreXliffPhTagsFromMatecatPhTags( $segment );
34
+        if (empty($this->dataRefMap)) {
35
+            $segment = $this->restoreXliffPhTagsFromMatecatPhTags($segment);
36 36
 
37
-            return $this->restoreXliffPcTagsFromMatecatPhTags( $segment );
37
+            return $this->restoreXliffPcTagsFromMatecatPhTags($segment);
38 38
         }
39 39
 
40
-        $dataRefReplacer = new DataRefReplacer( $this->dataRefMap );
41
-        $segment         = $dataRefReplacer->restore( $segment );
42
-        $segment         = $this->restoreXliffPhTagsFromMatecatPhTags( $segment );
40
+        $dataRefReplacer = new DataRefReplacer($this->dataRefMap);
41
+        $segment         = $dataRefReplacer->restore($segment);
42
+        $segment         = $this->restoreXliffPhTagsFromMatecatPhTags($segment);
43 43
 
44
-        return $this->restoreXliffPcTagsFromMatecatPhTags( $segment );
44
+        return $this->restoreXliffPcTagsFromMatecatPhTags($segment);
45 45
     }
46 46
 
47 47
     /**
@@ -60,15 +60,15 @@  discard block
 block discarded – undo
60 60
      *
61 61
      * @return string
62 62
      */
63
-    private function restoreXliffPhTagsFromMatecatPhTags( string $segment ): string {
64
-        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $matches );
63
+    private function restoreXliffPhTagsFromMatecatPhTags(string $segment): string {
64
+        preg_match_all('|<ph[^>]+? ctype="'.CTypeEnum::ORIGINAL_PH_OR_NOT_DATA_REF.'" equiv-text="base64:(.*?)"/>|iu', $segment, $matches);
65 65
 
66
-        if ( empty( $matches[ 0 ] ) ) {
66
+        if (empty($matches[0])) {
67 67
             return $segment;
68 68
         }
69 69
 
70
-        foreach ( $matches[ 0 ] as $index => $match ) {
71
-            $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment );
70
+        foreach ($matches[0] as $index => $match) {
71
+            $segment = str_replace($match, base64_decode($matches[1][$index]), $segment);
72 72
         }
73 73
 
74 74
         return $segment;
@@ -90,21 +90,21 @@  discard block
 block discarded – undo
90 90
      *
91 91
      * @return string
92 92
      */
93
-    private function restoreXliffPcTagsFromMatecatPhTags( string $segment ): string {
93
+    private function restoreXliffPcTagsFromMatecatPhTags(string $segment): string {
94 94
 
95 95
         $matches = [];
96
-        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PC_OPEN_NO_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $open_matches );
97
-        preg_match_all( '|<ph[^>]+? ctype="' . CTypeEnum::ORIGINAL_PC_CLOSE_NO_DATA_REF . '" equiv-text="base64:(.*?)"/>|iu', $segment, $close_matches );
96
+        preg_match_all('|<ph[^>]+? ctype="'.CTypeEnum::ORIGINAL_PC_OPEN_NO_DATA_REF.'" equiv-text="base64:(.*?)"/>|iu', $segment, $open_matches);
97
+        preg_match_all('|<ph[^>]+? ctype="'.CTypeEnum::ORIGINAL_PC_CLOSE_NO_DATA_REF.'" equiv-text="base64:(.*?)"/>|iu', $segment, $close_matches);
98 98
 
99
-        $matches[ 0 ] = array_merge( $open_matches[ 0 ], $close_matches[ 0 ] );
100
-        $matches[ 1 ] = array_merge( $open_matches[ 1 ], $close_matches[ 1 ] );
99
+        $matches[0] = array_merge($open_matches[0], $close_matches[0]);
100
+        $matches[1] = array_merge($open_matches[1], $close_matches[1]);
101 101
 
102
-        if ( empty( $matches[ 0 ] ) ) {
102
+        if (empty($matches[0])) {
103 103
             return $segment;
104 104
         }
105 105
 
106
-        foreach ( $matches[ 0 ] as $index => $match ) {
107
-            $segment = str_replace( $match, base64_decode( $matches[ 1 ][ $index ] ), $segment );
106
+        foreach ($matches[0] as $index => $match) {
107
+            $segment = str_replace($match, base64_decode($matches[1][$index]), $segment);
108 108
         }
109 109
 
110 110
         return $segment;
Please login to merge, or discard this patch.
src/Utils/Utils.php 1 patch
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -9,13 +9,13 @@  discard block
 block discarded – undo
9 9
      *
10 10
      * @return bool
11 11
      */
12
-    public static function array_is_list( $array ): bool {
12
+    public static function array_is_list($array): bool {
13 13
 
14
-        if ( $array === [] ) {
14
+        if ($array === []) {
15 15
             return true;
16 16
         }
17 17
 
18
-        return array_keys( $array ) === range( 0, count( $array ) - 1 );
18
+        return array_keys($array) === range(0, count($array) - 1);
19 19
 
20 20
     }
21 21
 
@@ -25,8 +25,8 @@  discard block
 block discarded – undo
25 25
      *
26 26
      * @return bool
27 27
      */
28
-    public static function contains( string $needle, string $haystack ): bool {
29
-        return strpos( $haystack, $needle ) !== false;
28
+    public static function contains(string $needle, string $haystack): bool {
29
+        return strpos($haystack, $needle) !== false;
30 30
     }
31 31
 
32 32
     /**
@@ -39,22 +39,22 @@  discard block
 block discarded – undo
39 39
      * @return int
40 40
      *
41 41
      */
42
-    public static function fastUnicode2ord( string $mb_char ) {
43
-        switch ( strlen( $mb_char ) ) {
42
+    public static function fastUnicode2ord(string $mb_char) {
43
+        switch (strlen($mb_char)) {
44 44
             case 1:
45
-                return ord( $mb_char );
45
+                return ord($mb_char);
46 46
             case 2:
47
-                return ( ord( $mb_char[ 0 ] ) - 0xC0 ) * 0x40 +
48
-                        ord( $mb_char[ 1 ] ) - 0x80;
47
+                return (ord($mb_char[0]) - 0xC0) * 0x40 +
48
+                        ord($mb_char[1]) - 0x80;
49 49
             case 3:
50
-                return ( ord( $mb_char[ 0 ] ) - 0xE0 ) * 0x1000 +
51
-                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x40 +
52
-                        ord( $mb_char[ 2 ] ) - 0x80;
50
+                return (ord($mb_char[0]) - 0xE0) * 0x1000 +
51
+                        (ord($mb_char[1]) - 0x80) * 0x40 +
52
+                        ord($mb_char[2]) - 0x80;
53 53
             case 4:
54
-                return ( ord( $mb_char[ 0 ] ) - 0xF0 ) * 0x40000 +
55
-                        ( ord( $mb_char[ 1 ] ) - 0x80 ) * 0x1000 +
56
-                        ( ord( $mb_char[ 2 ] ) - 0x80 ) * 0x40 +
57
-                        ord( $mb_char[ 3 ] ) - 0x80;
54
+                return (ord($mb_char[0]) - 0xF0) * 0x40000 +
55
+                        (ord($mb_char[1]) - 0x80) * 0x1000 +
56
+                        (ord($mb_char[2]) - 0x80) * 0x40 +
57
+                        ord($mb_char[3]) - 0x80;
58 58
         }
59 59
 
60 60
         return 20; //as default, return a space (should never happen)
@@ -65,8 +65,8 @@  discard block
 block discarded – undo
65 65
      *
66 66
      * @return string
67 67
      */
68
-    public static function htmlentitiesFromUnicode( array $str ): string {
69
-        return "&#" . self::fastUnicode2ord( $str[ 1 ] ) . ";";
68
+    public static function htmlentitiesFromUnicode(array $str): string {
69
+        return "&#".self::fastUnicode2ord($str[1]).";";
70 70
     }
71 71
 
72 72
     /**
@@ -79,8 +79,8 @@  discard block
 block discarded – undo
79 79
      *
80 80
      * @return string
81 81
      */
82
-    public static function unicode2chr( int $o ): string {
83
-        return (string)mb_convert_encoding( '&#' . $o . ';', 'UTF-8', 'HTML-ENTITIES' );
82
+    public static function unicode2chr(int $o): string {
83
+        return (string) mb_convert_encoding('&#'.$o.';', 'UTF-8', 'HTML-ENTITIES');
84 84
     }
85 85
 
86 86
 }
87 87
\ No newline at end of file
Please login to merge, or discard this patch.
src/Utils/DataRefReplacer.php 1 patch
Spacing   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -27,8 +27,8 @@  discard block
 block discarded – undo
27 27
      *
28 28
      * @param array $map
29 29
      */
30
-    public function __construct( array $map = [] ) {
31
-        $this->map = Map::instance( $this->sanitizeMap( $map ) );
30
+    public function __construct(array $map = []) {
31
+        $this->map = Map::instance($this->sanitizeMap($map));
32 32
     }
33 33
 
34 34
     /**
@@ -42,42 +42,42 @@  discard block
 block discarded – undo
42 42
      *
43 43
      * @return string
44 44
      */
45
-    public function replace( string $string ): string {
45
+    public function replace(string $string): string {
46 46
 
47 47
         // if the map is empty
48 48
         // or the string has not a dataRef attribute
49 49
         // return string as is
50
-        if ( $this->map->isEmpty() || !$this->hasAnyDataRefAttribute( $string ) ) {
50
+        if ($this->map->isEmpty() || !$this->hasAnyDataRefAttribute($string)) {
51 51
             return $string;
52 52
         }
53 53
 
54 54
         // try not to throw exception for wrong segments with opening tags and no closing
55 55
         try {
56 56
 
57
-            $html = XmlParser::parse( $string, true );
57
+            $html = XmlParser::parse($string, true);
58 58
 
59 59
             $dataRefEndMap = new ArrayList();
60 60
 
61
-            foreach ( $html as $node ) {
61
+            foreach ($html as $node) {
62 62
 
63 63
                 // 1. Replace <ph>|<sc>|<ec> tags
64
-                $string = $this->recursiveTransformDataRefToPhTag( $node, $string );
64
+                $string = $this->recursiveTransformDataRefToPhTag($node, $string);
65 65
 
66 66
                 // 2. Replace self-closed <pc dataRefStart="xyz" /> tags
67
-                $string = $this->recursiveReplaceSelfClosedPcTags( $node, $string );
67
+                $string = $this->recursiveReplaceSelfClosedPcTags($node, $string);
68 68
 
69 69
                 // 3. Build the DataRefEndMap needed by replaceClosingPcTags function
70 70
                 // (needed for correct handling of </pc> closing tags)
71 71
                 // make this inline with one foreach cycle
72
-                $this->extractDataRefMapRecursively( $node, $string, $dataRefEndMap );
72
+                $this->extractDataRefMapRecursively($node, $string, $dataRefEndMap);
73 73
 
74 74
             }
75 75
 
76 76
             // 4. replace pc tags
77
-            $string = $this->replaceOpeningPcTags( $string );
78
-            $string = $this->replaceClosingPcTags( $string, $dataRefEndMap );
77
+            $string = $this->replaceOpeningPcTags($string);
78
+            $string = $this->replaceClosingPcTags($string, $dataRefEndMap);
79 79
 
80
-        } catch ( Exception $ignore ) {
80
+        } catch (Exception $ignore) {
81 81
             // if something fails here, do not throw an exception and return the original string instead
82 82
 //            var_dump( $ignore );
83 83
         } finally {
@@ -91,8 +91,8 @@  discard block
 block discarded – undo
91 91
      *
92 92
      * @return bool
93 93
      */
94
-    private function hasAnyDataRefAttribute( string $string ): bool {
95
-        return (bool)preg_match( '/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string );
94
+    private function hasAnyDataRefAttribute(string $string): bool {
95
+        return (bool) preg_match('/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string);
96 96
     }
97 97
 
98 98
     /**
@@ -108,18 +108,18 @@  discard block
 block discarded – undo
108 108
      *
109 109
      * @return string
110 110
      */
111
-    private function recursiveTransformDataRefToPhTag( object $node, string $string ): string {
111
+    private function recursiveTransformDataRefToPhTag(object $node, string $string): string {
112 112
 
113
-        if ( $node->has_children ) {
113
+        if ($node->has_children) {
114 114
 
115
-            foreach ( $node->inner_html as $childNode ) {
116
-                $string = $this->recursiveTransformDataRefToPhTag( $childNode, $string );
115
+            foreach ($node->inner_html as $childNode) {
116
+                $string = $this->recursiveTransformDataRefToPhTag($childNode, $string);
117 117
             }
118 118
 
119 119
         } else {
120 120
 
121 121
             // accept only those tags
122
-            switch ( $node->tagName ) {
122
+            switch ($node->tagName) {
123 123
                 case 'ph':
124 124
                     $ctype = CTypeEnum::PH_DATA_REF;
125 125
                     break;
@@ -134,17 +134,17 @@  discard block
 block discarded – undo
134 134
             }
135 135
 
136 136
             // if isset a value in the map, proceed with conversion, otherwise skip
137
-            $attributesMap = Map::instance( $node->attributes );
138
-            if ( !$this->map->get( $attributesMap->get( 'dataRef' ) ) ) {
137
+            $attributesMap = Map::instance($node->attributes);
138
+            if (!$this->map->get($attributesMap->get('dataRef'))) {
139 139
                 return $string;
140 140
             }
141 141
 
142
-            $dataRefName = $node->attributes[ 'dataRef' ];   // map identifier. Eg: source1
142
+            $dataRefName = $node->attributes['dataRef']; // map identifier. Eg: source1
143 143
 
144 144
             return $this->replaceNewTagString(
145 145
                     $node->node,
146
-                    $attributesMap->getOrDefault( 'id', $dataRefName ),
147
-                    $this->map->getOrDefault( $node->attributes[ 'dataRef' ], 'NULL' ),
146
+                    $attributesMap->getOrDefault('id', $dataRefName),
147
+                    $this->map->getOrDefault($node->attributes['dataRef'], 'NULL'),
148 148
                     $ctype,
149 149
                     $string,
150 150
                     null
@@ -162,11 +162,11 @@  discard block
 block discarded – undo
162 162
      *
163 163
      * @return array
164 164
      */
165
-    private function sanitizeMap( array $map ): array {
165
+    private function sanitizeMap(array $map): array {
166 166
 
167
-        foreach ( $map as $name => $value ) {
168
-            if ( is_null( $value ) || $value === '' ) {
169
-                $map[ $name ] = 'NULL';
167
+        foreach ($map as $name => $value) {
168
+            if (is_null($value) || $value === '') {
169
+                $map[$name] = 'NULL';
170 170
             }
171 171
         }
172 172
 
@@ -182,23 +182,23 @@  discard block
 block discarded – undo
182 182
      * @throws InvalidXmlException
183 183
      * @throws XmlParsingException
184 184
      */
185
-    private function recursiveReplaceSelfClosedPcTags( object $node, string $string ): string {
185
+    private function recursiveReplaceSelfClosedPcTags(object $node, string $string): string {
186 186
 
187
-        if ( $node->has_children ) {
187
+        if ($node->has_children) {
188 188
 
189
-            foreach ( $node->inner_html as $childNode ) {
190
-                $string = $this->recursiveReplaceSelfClosedPcTags( $childNode, $string );
189
+            foreach ($node->inner_html as $childNode) {
190
+                $string = $this->recursiveReplaceSelfClosedPcTags($childNode, $string);
191 191
             }
192 192
 
193
-        } elseif ( $node->tagName == 'pc' && $node->self_closed === true ) {
193
+        } elseif ($node->tagName == 'pc' && $node->self_closed === true) {
194 194
 
195
-            $attributesMap = Map::instance( $node->attributes );
195
+            $attributesMap = Map::instance($node->attributes);
196 196
 
197
-            if ( $dataRefStartValue = $this->map->get( $node->attributes[ 'dataRefStart' ] ) ) {
197
+            if ($dataRefStartValue = $this->map->get($node->attributes['dataRefStart'])) {
198 198
 
199 199
                 $string = $this->replaceNewTagString(
200 200
                         $node->node,
201
-                        $attributesMap->get( 'id' ),
201
+                        $attributesMap->get('id'),
202 202
                         $dataRefStartValue,
203 203
                         CTypeEnum::PC_SELF_CLOSE_DATA_REF,
204 204
                         $string
@@ -219,12 +219,12 @@  discard block
 block discarded – undo
219 219
      * @param string    $completeString
220 220
      * @param ArrayList $dataRefEndMap
221 221
      */
222
-    private function extractDataRefMapRecursively( object $node, string $completeString, ArrayList $dataRefEndMap ) {
222
+    private function extractDataRefMapRecursively(object $node, string $completeString, ArrayList $dataRefEndMap) {
223 223
 
224 224
         // we have to build the map for the closing pc tag, so get the children first
225
-        if ( $node->has_children ) {
226
-            foreach ( $node->inner_html as $nestedNode ) {
227
-                $this->extractDataRefMapRecursively( $nestedNode, $completeString, $dataRefEndMap );
225
+        if ($node->has_children) {
226
+            foreach ($node->inner_html as $nestedNode) {
227
+                $this->extractDataRefMapRecursively($nestedNode, $completeString, $dataRefEndMap);
228 228
             }
229 229
         }
230 230
 
@@ -237,15 +237,15 @@  discard block
 block discarded – undo
237 237
         //   becomes
238 238
         // <pc id="source5" dataRefStart="source5"></pc>
239 239
         //
240
-        $isATagPairWithEmptyTextNode = strpos( $completeString, substr( $node->node, 0, -2 ) . '></pc>' ) !== false;
240
+        $isATagPairWithEmptyTextNode = strpos($completeString, substr($node->node, 0, -2).'></pc>') !== false;
241 241
 
242
-        if ( $node->tagName === 'pc' && ( $node->self_closed === false || $isATagPairWithEmptyTextNode ) ) {
242
+        if ($node->tagName === 'pc' && ($node->self_closed === false || $isATagPairWithEmptyTextNode)) {
243 243
 
244
-            $attributesMap = Map::instance( $node->attributes );
245
-            $dataRefEnd    = $attributesMap->getOrDefault( 'dataRefEnd', $attributesMap->get( 'dataRefStart' ) );
244
+            $attributesMap = Map::instance($node->attributes);
245
+            $dataRefEnd    = $attributesMap->getOrDefault('dataRefEnd', $attributesMap->get('dataRefStart'));
246 246
 
247 247
             $dataRefEndMap[] = [
248
-                    'id'         => $attributesMap->get( 'id' ),
248
+                    'id'         => $attributesMap->get('id'),
249 249
                     'dataRefEnd' => $dataRefEnd,
250 250
             ];
251 251
 
@@ -263,31 +263,31 @@  discard block
 block discarded – undo
263 263
      * @throws InvalidXmlException
264 264
      * @throws XmlParsingException
265 265
      */
266
-    private function replaceOpeningPcTags( string $string ): string {
266
+    private function replaceOpeningPcTags(string $string): string {
267 267
 
268
-        preg_match_all( '|<pc ([^>/]+?)>|iu', $string, $openingPcMatches );
268
+        preg_match_all('|<pc ([^>/]+?)>|iu', $string, $openingPcMatches);
269 269
 
270
-        foreach ( $openingPcMatches[ 0 ] as $match ) {
270
+        foreach ($openingPcMatches[0] as $match) {
271 271
 
272
-            $node = XmlParser::parse( $match . '</pc>', true )[ 0 ]; // add a closing tag to not break XML integrity
272
+            $node = XmlParser::parse($match.'</pc>', true)[0]; // add a closing tag to not break XML integrity
273 273
 
274 274
             // CASE 1 - Missing `dataRefStart`
275
-            if ( isset( $node->attributes[ 'dataRefEnd' ] ) && !isset( $node->attributes[ 'dataRefStart' ] ) ) {
276
-                $node->attributes[ 'dataRefStart' ] = $node->attributes[ 'dataRefEnd' ];
275
+            if (isset($node->attributes['dataRefEnd']) && !isset($node->attributes['dataRefStart'])) {
276
+                $node->attributes['dataRefStart'] = $node->attributes['dataRefEnd'];
277 277
             }
278 278
 
279 279
             // CASE 2 - Missing `dataRefEnd`
280
-            if ( isset( $node->attributes[ 'dataRefStart' ] ) && !isset( $node->attributes[ 'dataRefEnd' ] ) ) {
281
-                $node->attributes[ 'dataRefEnd' ] = $node->attributes[ 'dataRefStart' ];
280
+            if (isset($node->attributes['dataRefStart']) && !isset($node->attributes['dataRefEnd'])) {
281
+                $node->attributes['dataRefEnd'] = $node->attributes['dataRefStart'];
282 282
             }
283 283
 
284
-            if ( isset( $node->attributes[ 'dataRefStart' ] ) ) {
284
+            if (isset($node->attributes['dataRefStart'])) {
285 285
 
286
-                $attributesMap = Map::instance( $node->attributes );
286
+                $attributesMap = Map::instance($node->attributes);
287 287
                 $string        = $this->replaceNewTagString(
288 288
                         $match,
289
-                        $attributesMap->get( 'id' ),
290
-                        $this->map->getOrDefault( $node->attributes[ 'dataRefStart' ], 'NULL' ),
289
+                        $attributesMap->get('id'),
290
+                        $this->map->getOrDefault($node->attributes['dataRefStart'], 'NULL'),
291 291
                         CTypeEnum::PC_OPEN_DATA_REF,
292 292
                         $string
293 293
                 );
@@ -307,31 +307,31 @@  discard block
 block discarded – undo
307 307
      *
308 308
      * @return string
309 309
      */
310
-    private function replaceClosingPcTags( string $string, ArrayList $dataRefEndMap ): string {
310
+    private function replaceClosingPcTags(string $string, ArrayList $dataRefEndMap): string {
311 311
 
312
-        preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE );
312
+        preg_match_all('|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE);
313 313
         $delta = 0;
314 314
 
315
-        foreach ( $closingPcMatches[ 0 ] as $index => $match ) {
315
+        foreach ($closingPcMatches[0] as $index => $match) {
316 316
 
317
-            $offset = $match[ 1 ];
317
+            $offset = $match[1];
318 318
             $length = 5; // strlen of '</pc>'
319 319
 
320
-            $attr = $dataRefEndMap->get( $index );
321
-            if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) {
320
+            $attr = $dataRefEndMap->get($index);
321
+            if (!empty($attr) && isset($attr['dataRefEnd'])) {
322 322
 
323 323
                 // conversion for opening <pc> tag
324 324
                 $completeTag = $this->getNewTagString(
325 325
                         '</pc>',
326
-                        $attr[ 'id' ],
327
-                        $this->map->getOrDefault( $attr[ 'dataRefEnd' ], 'NULL' ),
326
+                        $attr['id'],
327
+                        $this->map->getOrDefault($attr['dataRefEnd'], 'NULL'),
328 328
                         CTypeEnum::PC_CLOSE_DATA_REF,
329 329
                         '_2'
330 330
                 );
331 331
 
332
-                $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta );
333
-                $string     = (string)substr_replace( $string, $completeTag, $realOffset, $length );
334
-                $delta      = $delta + strlen( $completeTag ) - $length;
332
+                $realOffset = ($delta === 0) ? $offset : ($offset + $delta);
333
+                $string     = (string) substr_replace($string, $completeTag, $realOffset, $length);
334
+                $delta      = $delta + strlen($completeTag) - $length;
335 335
 
336 336
             }
337 337
 
@@ -349,17 +349,17 @@  discard block
 block discarded – undo
349 349
      * @throws InvalidXmlException
350 350
      * @throws XmlParsingException
351 351
      */
352
-    public function restore( string $string ): string {
352
+    public function restore(string $string): string {
353 353
 
354 354
         // if the map is empty return string as is
355
-        if ( $this->map->isEmpty() ) {
355
+        if ($this->map->isEmpty()) {
356 356
             return $string;
357 357
         }
358 358
 
359
-        $html = XmlParser::parse( $string, true );
359
+        $html = XmlParser::parse($string, true);
360 360
 
361
-        foreach ( $html as $node ) {
362
-            $string = $this->recursiveRestoreOriginalTags( $node, $string );
361
+        foreach ($html as $node) {
362
+            $string = $this->recursiveRestoreOriginalTags($node, $string);
363 363
         }
364 364
 
365 365
         return $string;
@@ -371,26 +371,26 @@  discard block
 block discarded – undo
371 371
      *
372 372
      * @return string
373 373
      */
374
-    private function recursiveRestoreOriginalTags( object $node, string $string ): string {
374
+    private function recursiveRestoreOriginalTags(object $node, string $string): string {
375 375
 
376
-        if ( $node->has_children ) {
376
+        if ($node->has_children) {
377 377
 
378
-            foreach ( $node->inner_html as $childNode ) {
379
-                $string = $this->recursiveRestoreOriginalTags( $childNode, $string );
378
+            foreach ($node->inner_html as $childNode) {
379
+                $string = $this->recursiveRestoreOriginalTags($childNode, $string);
380 380
             }
381 381
 
382 382
         } else {
383 383
 
384
-            $nodeAttributesMap = Map::instance( $node->attributes );
384
+            $nodeAttributesMap = Map::instance($node->attributes);
385 385
 
386
-            if ( !$nodeAttributesMap->get( 'x-orig' ) ) {
386
+            if (!$nodeAttributesMap->get('x-orig')) {
387 387
                 return $string;
388 388
             }
389 389
 
390
-            $cType = $nodeAttributesMap->get( 'ctype' );
390
+            $cType = $nodeAttributesMap->get('ctype');
391 391
 
392
-            if ( CTypeEnum::isLayer2Constant( $cType ?? '' ) ) {
393
-                return preg_replace( '/' . preg_quote( $node->node, '/' ) . '/', base64_decode( $nodeAttributesMap->get( 'x-orig' ) ), $string, 1 );
392
+            if (CTypeEnum::isLayer2Constant($cType ?? '')) {
393
+                return preg_replace('/'.preg_quote($node->node, '/').'/', base64_decode($nodeAttributesMap->get('x-orig')), $string, 1);
394 394
             }
395 395
 
396 396
         }
@@ -408,24 +408,24 @@  discard block
 block discarded – undo
408 408
      *
409 409
      * @return string
410 410
      */
411
-    private function getNewTagString( string $actualNodeString, ?string $id, string $dataRefValue, string $ctype, ?string $upCountIdValue = null ): string {
411
+    private function getNewTagString(string $actualNodeString, ?string $id, string $dataRefValue, string $ctype, ?string $upCountIdValue = null): string {
412 412
 
413
-        $newTag = [ '<ph' ];
413
+        $newTag = ['<ph'];
414 414
 
415
-        if ( isset( $id ) ) {
416
-            $newTag[] = 'id="' . $id . $upCountIdValue . '"';
415
+        if (isset($id)) {
416
+            $newTag[] = 'id="'.$id.$upCountIdValue.'"';
417 417
         }
418 418
 
419
-        $newTag[] = 'ctype="' . $ctype . '"';
420
-        $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"';
421
-        $newTag[] = 'x-orig="' . base64_encode( $actualNodeString ) . '"';
419
+        $newTag[] = 'ctype="'.$ctype.'"';
420
+        $newTag[] = 'equiv-text="base64:'.base64_encode($dataRefValue).'"';
421
+        $newTag[] = 'x-orig="'.base64_encode($actualNodeString).'"';
422 422
 
423
-        return implode( " ", $newTag ) . '/>';
423
+        return implode(" ", $newTag).'/>';
424 424
 
425 425
     }
426 426
 
427
-    private function replaceNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1' ) {
428
-        return str_replace( $actualNodeString, $this->getNewTagString( $actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue ), $originalString );
427
+    private function replaceNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $originalString, $upCountIdValue = '_1') {
428
+        return str_replace($actualNodeString, $this->getNewTagString($actualNodeString, $id, $dataRefValue, $ctype, $upCountIdValue), $originalString);
429 429
     }
430 430
 
431 431
 }
432 432
\ No newline at end of file
Please login to merge, or discard this patch.