Passed
Push — master ( 7aa9cc...d16220 )
by Domenico
01:50
created
src/Filters/XmlToPh.php 1 patch
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -38,7 +38,7 @@  discard block
 block discarded – undo
38 38
      *
39 39
      * @return string The original buffer.
40 40
      */
41
-    protected function _finalizePlainText( string $buffer ): string {
41
+    protected function _finalizePlainText(string $buffer): string {
42 42
         return $buffer;
43 43
     }
44 44
 
@@ -53,18 +53,18 @@  discard block
 block discarded – undo
53 53
      *
54 54
      * @return string The generated <ph> placeholder tag.
55 55
      */
56
-    protected function _finalizeMarkupTag( string $buffer ): string {
56
+    protected function _finalizeMarkupTag(string $buffer): string {
57 57
         // Decode attributes by locking < and > first
58 58
         // Because a HTML tag has it's attributes encoded and here we get lt and gt decoded but not other parts of the string
59 59
         // Ex:
60 60
         // incoming string: <a href="/users/settings?test=123&amp;amp;foobar=1" target="_blank">
61 61
         // this should be: <a href="/users/settings?test=123&amp;foobar=1" target="_blank"> with only one ampersand encoding
62 62
         //
63
-        $buffer = str_replace( [ '<', '>' ], [ '#_lt_#', '#_gt_#' ], $buffer );
64
-        $buffer = html_entity_decode( $buffer, ENT_NOQUOTES | 16 /* ENT_XML1 */, 'UTF-8' );
65
-        $buffer = str_replace( [ '#_lt_#', '#_gt_#' ], [ '<', '>' ], $buffer );
63
+        $buffer = str_replace(['<', '>'], ['#_lt_#', '#_gt_#'], $buffer);
64
+        $buffer = html_entity_decode($buffer, ENT_NOQUOTES | 16 /* ENT_XML1 */, 'UTF-8');
65
+        $buffer = str_replace(['#_lt_#', '#_gt_#'], ['<', '>'], $buffer);
66 66
 
67
-        return $this->_finalizeTag( $buffer );
67
+        return $this->_finalizeTag($buffer);
68 68
 
69 69
     }
70 70
 
@@ -76,8 +76,8 @@  discard block
 block discarded – undo
76 76
      *
77 77
      * @return string The resulting <ph> tag.
78 78
      */
79
-    protected function _finalizeTag( string $buffer ): string {
80
-        return '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . ( $this->isHTML ? CTypeEnum::HTML : CTypeEnum::XML ) . '" equiv-text="base64:' . base64_encode( htmlentities( $buffer, ENT_NOQUOTES | 16 /* ENT_XML1 */ ) ) . '"/>';
79
+    protected function _finalizeTag(string $buffer): string {
80
+        return '<ph id="'.$this->getPipeline()->getNextId().'" ctype="'.($this->isHTML ? CTypeEnum::HTML : CTypeEnum::XML).'" equiv-text="base64:'.base64_encode(htmlentities($buffer, ENT_NOQUOTES | 16 /* ENT_XML1 */)).'"/>';
81 81
     }
82 82
 
83 83
     /**
@@ -87,10 +87,10 @@  discard block
 block discarded – undo
87 87
      *
88 88
      * @return string The fixed string with escaped angle brackets.
89 89
      */
90
-    protected function _fixWrongBuffer( string $buffer ): string {
91
-        $buffer = str_replace( "<", "&lt;", $buffer );
90
+    protected function _fixWrongBuffer(string $buffer): string {
91
+        $buffer = str_replace("<", "&lt;", $buffer);
92 92
 
93
-        return str_replace( ">", "&gt;", $buffer );
93
+        return str_replace(">", "&gt;", $buffer);
94 94
     }
95 95
 
96 96
     /**
@@ -100,8 +100,8 @@  discard block
 block discarded – undo
100 100
      *
101 101
      * @return string The generated <ph> placeholder tag.
102 102
      */
103
-    protected function _finalizeScriptTag( string $buffer ): string {
104
-        return $this->_finalizeTag( $buffer );
103
+    protected function _finalizeScriptTag(string $buffer): string {
104
+        return $this->_finalizeTag($buffer);
105 105
     }
106 106
 
107 107
     /**
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
      *
119 119
      * @return bool True if the buffer is a valid tag, false otherwise.
120 120
      */
121
-    protected function _isTagValid( string $buffer ): bool {
121
+    protected function _isTagValid(string $buffer): bool {
122 122
 
123 123
         // This is a safeguard against misinterpreting partially processed strings.
124 124
         // During filtering, inner tags might be replaced by placeholders (e.g., ##LESSTHAN##).
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
         // content is not yet restored, so we must not treat it as a valid, final tag.
127 127
         // For example, an original string like '&lt;a href="<x/>"&gt;' could become
128 128
         // '<a href="##LESSTHAN##x/##GREATERTHAN##">', which should not be converted to a <ph> tag.
129
-        if ( strpos( $buffer, ConstantEnum::LTPLACEHOLDER ) !== false || strpos( $buffer, ConstantEnum::GTPLACEHOLDER ) !== false ) {
129
+        if (strpos($buffer, ConstantEnum::LTPLACEHOLDER) !== false || strpos($buffer, ConstantEnum::GTPLACEHOLDER) !== false) {
130 130
             return false;
131 131
         }
132 132
 
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
         // This regex validates the general structure of an XML/HTML tag.
144 144
         // It checks for a valid tag name (not starting with a number), optional attributes
145 145
         // (with quoted or unquoted values), and correct opening/closing brackets.
146
-        if ( preg_match( '#</?(?![0-9]+)[a-z0-9\-._:]+?(?:\s+[:a-z0-9\-._]+(?:=(?:"[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*/?>#ui', $buffer ) ) {
146
+        if (preg_match('#</?(?![0-9]+)[a-z0-9\-._:]+?(?:\s+[:a-z0-9\-._]+(?:=(?:"[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*/?>#ui', $buffer)) {
147 147
 
148 148
             /**
149 149
              * HTML5 Tag Matcher and Global Attribute Parser
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
              * - This regex is intended for validation and parsing in contexts that allow Unicode and extended characters.
181 181
              * - For `dataset` access in JavaScript, `getAttribute` is recommended for attributes with non-ASCII names.
182 182
              */
183
-            if ( preg_match( '#<\s*/?\s*(?:html|head|body|header|footer|main|section|article|nav|aside|h1|h2|h3|h4|h5|h6|p|hr|pre|blockquote|ol|ul|li|dl|dt|dd|figure|figcaption|div|a|em|strong|small|s|cite|q|dfn|abbr|ruby|rt|rp|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|bdi|bdo|span|br|wbr|form|label|input|button|select|datalist|optgroup|option|textarea|output|fieldset|legend|meter|progress|img|audio|video|source|track|picture|map|area|iframe|embed|object|param|table|caption|colgroup|col|tbody|thead|tfoot|tr|td|th|script|noscript|template|canvas|link|style|meta|base|title|details|summary|dialog|menu|menuitem|slot|portal)\b(?:\s+(?:accesskey|class|contenteditable|data-[^\s=]+|dir|draggable|enterkeyhint|hidden|id|inert|inputmode|lang|popover|spellcheck|style|tabindex|title|translate|xml:lang|xml:base|role|aria-[^\s=]+|on\w+)(?:=(?:"[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*/?\s*>#ui', $buffer ) ) {
183
+            if (preg_match('#<\s*/?\s*(?:html|head|body|header|footer|main|section|article|nav|aside|h1|h2|h3|h4|h5|h6|p|hr|pre|blockquote|ol|ul|li|dl|dt|dd|figure|figcaption|div|a|em|strong|small|s|cite|q|dfn|abbr|ruby|rt|rp|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|bdi|bdo|span|br|wbr|form|label|input|button|select|datalist|optgroup|option|textarea|output|fieldset|legend|meter|progress|img|audio|video|source|track|picture|map|area|iframe|embed|object|param|table|caption|colgroup|col|tbody|thead|tfoot|tr|td|th|script|noscript|template|canvas|link|style|meta|base|title|details|summary|dialog|menu|menuitem|slot|portal)\b(?:\s+(?:accesskey|class|contenteditable|data-[^\s=]+|dir|draggable|enterkeyhint|hidden|id|inert|inputmode|lang|popover|spellcheck|style|tabindex|title|translate|xml:lang|xml:base|role|aria-[^\s=]+|on\w+)(?:=(?:"[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*/?\s*>#ui', $buffer)) {
184 184
                 $this->isHTML = true;
185 185
             }
186 186
 
@@ -201,16 +201,16 @@  discard block
 block discarded – undo
201 201
      *
202 202
      * @return string The transformed segment.
203 203
      */
204
-    public function transform( string $segment ): string {
204
+    public function transform(string $segment): string {
205 205
 
206 206
         // restore < e >
207
-        $segment = str_replace( "&lt;", "<", $segment );
208
-        $segment = str_replace( "&gt;", ">", $segment );
207
+        $segment = str_replace("&lt;", "<", $segment);
208
+        $segment = str_replace("&gt;", ">", $segment);
209 209
 
210 210
         $parser = new HtmlParser();
211
-        $parser->registerCallbacksHandler( $this );
211
+        $parser->registerCallbacksHandler($this);
212 212
 
213
-        return $parser->transform( $segment );
213
+        return $parser->transform($segment);
214 214
     }
215 215
 
216 216
 }
217 217
\ No newline at end of file
Please login to merge, or discard this patch.
src/AbstractFilter.php 1 patch
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
      *
88 88
      * @return AbstractFilter The configured instance of the filter.
89 89
      */
90
-    public static function getInstance( FeatureSetInterface $featureSet, ?string $source = null, ?string $target = null, ?array $dataRefMap = [], ?array $handlerClassNamesForLayer0ToLayer1Transition = [] ): ?AbstractFilter {
90
+    public static function getInstance(FeatureSetInterface $featureSet, ?string $source = null, ?string $target = null, ?array $dataRefMap = [], ?array $handlerClassNamesForLayer0ToLayer1Transition = []): ?AbstractFilter {
91 91
         // Create a new instance of the specific filter class (e.g., MateCatFilter).
92 92
         $newInstance = new static();
93 93
 
@@ -99,17 +99,17 @@  discard block
 block discarded – undo
99 99
         $newInstance->dataRefMap = $dataRefMap ?? [];
100 100
 
101 101
         // Determine which handlers to use for the Layer 0 to Layer 1 transition.
102
-        if ( is_array( $handlerClassNamesForLayer0ToLayer1Transition ) && empty( $handlerClassNamesForLayer0ToLayer1Transition ) ) {
102
+        if (is_array($handlerClassNamesForLayer0ToLayer1Transition) && empty($handlerClassNamesForLayer0ToLayer1Transition)) {
103 103
             // If an empty array is passed, load the default set of handlers from the sorter.
104
-            $handlerClassNamesForLayer0ToLayer1Transition = array_keys( HandlersSorter::getDefaultInjectedHandlers() );
105
-        } elseif ( is_null( $handlerClassNamesForLayer0ToLayer1Transition ) ) {
104
+            $handlerClassNamesForLayer0ToLayer1Transition = array_keys(HandlersSorter::getDefaultInjectedHandlers());
105
+        } elseif (is_null($handlerClassNamesForLayer0ToLayer1Transition)) {
106 106
             // If null is passed, use no handlers.
107 107
             $handlerClassNamesForLayer0ToLayer1Transition = [];
108 108
         }
109 109
         // Otherwise, use the custom list of handlers provided.
110 110
 
111 111
         // Sort the dynamic feature-based handlers.
112
-        $sorter                                                  = new HandlersSorter( $handlerClassNamesForLayer0ToLayer1Transition );
112
+        $sorter                                                  = new HandlersSorter($handlerClassNamesForLayer0ToLayer1Transition);
113 113
         $newInstance->orderedHandlersForLayer0ToLayer1Transition = $sorter->getOrderedHandlersClassNames();
114 114
 
115 115
         // Return the fully configured filter instance.
@@ -127,26 +127,26 @@  discard block
 block discarded – undo
127 127
      * @return string The transformed segment in Layer 0 format.
128 128
      * @throws Exception If any handler in the pipeline fails.
129 129
      */
130
-    public function fromLayer1ToLayer0( string $segment ): string {
130
+    public function fromLayer1ToLayer0(string $segment): string {
131 131
         // Initialize a new pipeline for this transformation.
132
-        $channel = new Pipeline( $this->source, $this->target, $this->dataRefMap );
132
+        $channel = new Pipeline($this->source, $this->target, $this->dataRefMap);
133 133
 
134 134
         // Add handlers to reverse the sub-filtering process.
135
-        $channel->addLast( MateCatCustomPHToOriginalValue::class ); // Restore original PH values
136
-        $channel->addLast( PlaceHoldXliffTags::class );             // Isolate XLIFF tags
137
-        $channel->addLast( EncodeToRawXML::class );                 // Encode for raw XML storage
138
-        $channel->addLast( LtGtEncode::class );                     // Encode '<' and '>'
139
-        $channel->addLast( RestoreXliffTagsContent::class );        // Restore original XLIFF content
140
-        $channel->addLast( RestorePlaceHoldersToXLIFFLtGt::class ); // Restore placeholders for '<' and '>'
141
-        $channel->addLast( SplitPlaceholder::class );               // Handle split placeholders
142
-        $channel->addLast( RestoreEquivText::class );               // Restore equiv-text content
135
+        $channel->addLast(MateCatCustomPHToOriginalValue::class); // Restore original PH values
136
+        $channel->addLast(PlaceHoldXliffTags::class); // Isolate XLIFF tags
137
+        $channel->addLast(EncodeToRawXML::class); // Encode for raw XML storage
138
+        $channel->addLast(LtGtEncode::class); // Encode '<' and '>'
139
+        $channel->addLast(RestoreXliffTagsContent::class); // Restore original XLIFF content
140
+        $channel->addLast(RestorePlaceHoldersToXLIFFLtGt::class); // Restore placeholders for '<' and '>'
141
+        $channel->addLast(SplitPlaceholder::class); // Handle split placeholders
142
+        $channel->addLast(RestoreEquivText::class); // Restore equiv-text content
143 143
 
144 144
         // Allow the current feature set to modify the pipeline (e.g., add or remove handlers).
145 145
         /** @var $channel Pipeline */
146
-        $channel = $this->featureSet->filter( 'fromLayer1ToLayer0', $channel );
146
+        $channel = $this->featureSet->filter('fromLayer1ToLayer0', $channel);
147 147
 
148 148
         // Process the segment through the pipeline and return the result.
149
-        return $channel->transform( $segment );
149
+        return $channel->transform($segment);
150 150
     }
151 151
 
152 152
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
      *
163 163
      * @return string The transformed segment after processing from Layer 0 to Layer 1.
164 164
      */
165
-    public abstract function fromLayer0ToLayer1( string $segment, ?string $cid = null ): string;
165
+    public abstract function fromLayer0ToLayer1(string $segment, ?string $cid = null): string;
166 166
 
167 167
     /**
168 168
      * Configures the pipeline for transforming content from Layer 0 to Layer 1.
@@ -177,23 +177,23 @@  discard block
 block discarded – undo
177 177
      * @param Pipeline    $channel
178 178
      * @param string|null $cid
179 179
      */
180
-    protected function configureFromLayer0ToLayer1Pipeline( Pipeline $channel, ?string $cid = null ): void {
180
+    protected function configureFromLayer0ToLayer1Pipeline(Pipeline $channel, ?string $cid = null): void {
181 181
 
182 182
         // Add initial handlers for standard XLIFF and placeholder normalization.
183
-        $channel->addLast( StandardPHToMateCatCustomPH::class );
184
-        $channel->addLast( StandardXEquivTextToMateCatCustomPH::class );
185
-        $channel->addLast( PlaceHoldXliffTags::class );
183
+        $channel->addLast(StandardPHToMateCatCustomPH::class);
184
+        $channel->addLast(StandardXEquivTextToMateCatCustomPH::class);
185
+        $channel->addLast(PlaceHoldXliffTags::class);
186 186
 //        $channel->addLast( LtGtDecode::class );
187 187
 
188 188
         // Add the dynamic feature-based handlers.
189
-        foreach ( $this->orderedHandlersForLayer0ToLayer1Transition as $handler ) {
190
-            $channel->addLast( $handler );
189
+        foreach ($this->orderedHandlersForLayer0ToLayer1Transition as $handler) {
190
+            $channel->addLast($handler);
191 191
         }
192 192
 
193 193
         // Add final handlers to restore XLIFF content and encode for the target layer.
194
-        $channel->addLast( RestoreXliffTagsContent::class );
195
-        $channel->addLast( RestorePlaceHoldersToXLIFFLtGt::class );
196
-        $channel->addLast( EquivTextToBase64::class );
194
+        $channel->addLast(RestoreXliffTagsContent::class);
195
+        $channel->addLast(RestorePlaceHoldersToXLIFFLtGt::class);
196
+        $channel->addLast(EquivTextToBase64::class);
197 197
 
198 198
     }
199 199
 
Please login to merge, or discard this patch.