Passed
Pull Request — master (#90)
by Domenico
08:32
created
src/XliffReplacer/Xliff20.php 1 patch
Spacing   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -32,18 +32,18 @@  discard block
 block discarded – undo
32 32
     /**
33 33
      * @inheritDoc
34 34
      */
35
-    protected function tagOpen( $parser, $name, $attr ) {
35
+    protected function tagOpen($parser, $name, $attr) {
36 36
 
37
-        $this->handleOpenUnit( $name, $attr );
37
+        $this->handleOpenUnit($name, $attr);
38 38
 
39
-        if ( 'mda:metadata' === $name ) {
39
+        if ('mda:metadata' === $name) {
40 40
             $this->unitContainsMda = true;
41 41
         }
42 42
 
43 43
         // check if we are entering into a <target>
44
-        if ( 'target' === $name ) {
44
+        if ('target' === $name) {
45 45
 
46
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
46
+            if ($this->currentTransUnitIsTranslatable === 'no') {
47 47
                 $this->inTarget = false;
48 48
             } else {
49 49
                 $this->inTarget = true;
@@ -51,13 +51,13 @@  discard block
 block discarded – undo
51 51
         }
52 52
 
53 53
         // open buffer
54
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
54
+        if (in_array($name, $this->nodesToBuffer)) {
55 55
             $this->bufferIsActive = true;
56 56
         }
57 57
 
58 58
         // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit
59 59
         // <target> must be stripped to be replaced, so this check avoids <target> reconstruction
60
-        if ( !$this->inTarget ) {
60
+        if (!$this->inTarget) {
61 61
 
62 62
             $tag = '';
63 63
 
@@ -80,31 +80,31 @@  discard block
 block discarded – undo
80 80
             // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit
81 81
             //
82 82
             if (
83
-                    ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) &&
83
+                    ($name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable') &&
84 84
                     $this->unitContainsMda === false &&
85
-                    !empty( $this->transUnits[ $this->currentTransUnitId ] ) &&
85
+                    !empty($this->transUnits[$this->currentTransUnitId]) &&
86 86
                     !$this->hasWrittenCounts
87 87
             ) {
88 88
                 // we need to update counts here
89 89
                 $this->updateCounts();
90 90
                 $this->hasWrittenCounts = true;
91
-                $tag                    .= $this->getWordCountGroupForXliffV2();
91
+                $tag .= $this->getWordCountGroupForXliffV2();
92 92
                 $this->unitContainsMda  = true;
93 93
             }
94 94
 
95 95
             // construct tag
96 96
             $tag .= "<$name ";
97 97
 
98
-            foreach ( $attr as $k => $v ) {
98
+            foreach ($attr as $k => $v) {
99 99
 
100 100
                 //if tag name is file, we must replace the target-language attribute
101
-                if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) {
101
+                if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) {
102 102
                     //replace Target language with job language provided from constructor
103 103
                     $tag .= "$k=\"$this->targetLang\" ";
104 104
                 } else {
105 105
 
106 106
                     //normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute
107
-                    if ( $k != 'state' ) {
107
+                    if ($k != 'state') {
108 108
                         $tag .= "$k=\"$v\" ";
109 109
                     }
110 110
 
@@ -114,39 +114,39 @@  discard block
 block discarded – undo
114 114
 
115 115
             $seg = $this->getCurrentSegment();
116 116
 
117
-            if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) {
117
+            if ($name === $this->tuTagName and !empty($seg) and isset($seg['sid'])) {
118 118
 
119 119
                 // add `help-id` to xliff v.1*
120 120
                 // add `mtc:segment-id` to xliff v.2*
121
-                if ( strpos( $tag, 'mtc:segment-id' ) === false ) {
122
-                    $tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" ";
121
+                if (strpos($tag, 'mtc:segment-id') === false) {
122
+                    $tag .= "mtc:segment-id=\"{$seg['sid']}\" ";
123 123
                 }
124 124
 
125 125
             }
126 126
 
127 127
             // replace state for xliff v2
128
-            if ( 'segment' === $name ) { // add state to segment in Xliff v2
129
-                [ $stateProp, ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion );
128
+            if ('segment' === $name) { // add state to segment in Xliff v2
129
+                [$stateProp, ] = StatusToStateAttribute::getState($seg['status'], $this->xliffVersion);
130 130
                 $tag .= $stateProp;
131 131
             }
132 132
 
133 133
 
134 134
             // add oasis xliff 20 namespace
135
-            if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) {
135
+            if ($name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) {
136 136
                 $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"';
137 137
             }
138 138
 
139 139
             // add MateCat specific namespace, we want maybe add non-XLIFF attributes
140
-            if ( $name === 'xliff' && !array_key_exists( 'xmlns:mtc', $attr ) ) {
140
+            if ($name === 'xliff' && !array_key_exists('xmlns:mtc', $attr)) {
141 141
                 $tag .= ' xmlns:mtc="https://www.matecat.com" ';
142 142
             }
143 143
 
144 144
             // trgLang
145
-            if ( $name === 'xliff' ) {
146
-                $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag );
145
+            if ($name === 'xliff') {
146
+                $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag);
147 147
             }
148 148
 
149
-            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
149
+            $this->checkForSelfClosedTagAndFlush($parser, $tag);
150 150
 
151 151
         }
152 152
 
@@ -155,7 +155,7 @@  discard block
 block discarded – undo
155 155
     /**
156 156
      * @inheritDoc
157 157
      */
158
-    protected function tagClose( $parser, $name ) {
158
+    protected function tagClose($parser, $name) {
159 159
         $tag = '';
160 160
 
161 161
         /**
@@ -164,32 +164,32 @@  discard block
 block discarded – undo
164 164
          *
165 165
          * self::tagOpen method
166 166
          */
167
-        if ( !$this->isEmpty ) {
167
+        if (!$this->isEmpty) {
168 168
 
169
-            if ( !$this->inTarget ) {
169
+            if (!$this->inTarget) {
170 170
                 $tag = "</$name>";
171 171
             }
172 172
 
173
-            if ( 'target' == $name ) {
173
+            if ('target' == $name) {
174 174
 
175
-                if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) && $this->currentTransUnitIsTranslatable !== 'no' ) {
175
+                if (isset($this->transUnits[$this->currentTransUnitId]) && $this->currentTransUnitIsTranslatable !== 'no') {
176 176
 
177 177
                     $seg = $this->getCurrentSegment();
178 178
 
179 179
                     // update counts
180
-                    if ( !$this->hasWrittenCounts && !empty( $seg ) ) {
181
-                        $this->updateSegmentCounts( $seg );
180
+                    if (!$this->hasWrittenCounts && !empty($seg)) {
181
+                        $this->updateSegmentCounts($seg);
182 182
                     }
183 183
 
184 184
                     // delete translations so the prepareSegment
185 185
                     // will put source content in target tag
186
-                    if ( $this->sourceInTarget ) {
187
-                        $seg[ 'translation' ] = '';
186
+                    if ($this->sourceInTarget) {
187
+                        $seg['translation'] = '';
188 188
                         $this->resetCounts();
189 189
                     }
190 190
 
191 191
                     // append $translation
192
-                    $translation = $this->prepareTranslation( $seg );
192
+                    $translation = $this->prepareTranslation($seg);
193 193
 
194 194
                     //append translation
195 195
                     $tag = "<target>$translation</target>";
@@ -199,21 +199,21 @@  discard block
 block discarded – undo
199 199
                 // signal we are leaving a target
200 200
                 $this->targetWasWritten = true;
201 201
                 $this->inTarget         = false;
202
-                $this->postProcAndFlush( $this->outputFP, $tag, true );
203
-            } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section
202
+                $this->postProcAndFlush($this->outputFP, $tag, true);
203
+            } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section
204 204
 
205 205
                 $this->bufferIsActive = false;
206 206
 
207 207
                 // only for Xliff 2.*
208 208
                 // write here <mda:metaGroup> and <mda:meta> if already present in the <unit>
209
-                if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) {
209
+                if ('mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts) {
210 210
 
211 211
                     // we need to update counts here
212 212
                     $this->updateCounts();
213 213
                     $this->hasWrittenCounts = true;
214 214
 
215 215
                     $tag = $this->CDATABuffer;
216
-                    $tag .= $this->getWordCountGroupForXliffV2( false );
216
+                    $tag .= $this->getWordCountGroupForXliffV2(false);
217 217
                     $tag .= "    </mda:metadata>";
218 218
 
219 219
                 } else {
@@ -223,19 +223,19 @@  discard block
 block discarded – undo
223 223
                 $this->CDATABuffer = "";
224 224
 
225 225
                 //flush to the pointer
226
-                $this->postProcAndFlush( $this->outputFP, $tag );
226
+                $this->postProcAndFlush($this->outputFP, $tag);
227 227
 
228
-            } elseif ( 'segment' === $name ) {
228
+            } elseif ('segment' === $name) {
229 229
 
230 230
                 // only for Xliff 2.*
231 231
                 // if segment has no <target> add it BEFORE </segment>
232
-                if ( !$this->targetWasWritten ) {
232
+                if (!$this->targetWasWritten) {
233 233
 
234 234
                     $seg = $this->getCurrentSegment();
235 235
 
236
-                    if ( isset( $seg[ 'translation' ] ) ) {
236
+                    if (isset($seg['translation'])) {
237 237
 
238
-                        $translation = $this->prepareTranslation( $seg );
238
+                        $translation = $this->prepareTranslation($seg);
239 239
                         // replace the tag
240 240
                         $tag = "<target>$translation</target>";
241 241
 
@@ -248,17 +248,17 @@  discard block
 block discarded – undo
248 248
                 // update segmentPositionInTu
249 249
                 $this->segmentInUnitPosition++;
250 250
 
251
-                $this->postProcAndFlush( $this->outputFP, $tag );
251
+                $this->postProcAndFlush($this->outputFP, $tag);
252 252
 
253 253
                 // we are leaving <segment>, reset $segmentHasTarget
254 254
                 $this->targetWasWritten = false;
255 255
 
256
-            } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
256
+            } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
257 257
                 $this->CDATABuffer .= "</$name>";
258 258
                 // Do NOT Flush
259 259
             } else { //generic tag closure do Nothing
260 260
                 // flush to pointer
261
-                $this->postProcAndFlush( $this->outputFP, $tag );
261
+                $this->postProcAndFlush($this->outputFP, $tag);
262 262
             }
263 263
         } else {
264 264
             //ok, nothing to be done; reset flag for next coming tag
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
         }
267 267
 
268 268
         // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
269
-        if ( $this->tuTagName === $name ) {
269
+        if ($this->tuTagName === $name) {
270 270
             $this->currentTransUnitIsTranslatable = null;
271 271
             $this->inTU                           = false;
272 272
             $this->segmentPositionInTu            = -1;
@@ -283,8 +283,8 @@  discard block
 block discarded – undo
283 283
     private function updateCounts() {
284 284
 
285 285
         $seg = $this->getCurrentSegment();
286
-        if ( !empty( $seg ) ) {
287
-            $this->updateSegmentCounts( $seg );
286
+        if (!empty($seg)) {
287
+            $this->updateSegmentCounts($seg);
288 288
         }
289 289
 
290 290
     }
@@ -294,30 +294,30 @@  discard block
 block discarded – undo
294 294
      *
295 295
      * @return string
296 296
      */
297
-    private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string {
297
+    private function getWordCountGroupForXliffV2(bool $withMetadataTag = true): string {
298 298
 
299 299
         $this->mdaGroupCounter++;
300
-        $segments_count_array = $this->counts[ 'segments_count_array' ];
300
+        $segments_count_array = $this->counts['segments_count_array'];
301 301
 
302 302
         $tag = '';
303 303
 
304
-        if ( $withMetadataTag === true ) {
304
+        if ($withMetadataTag === true) {
305 305
             $tag .= '<mda:metadata>';
306 306
         }
307 307
 
308 308
         $index = 0;
309
-        foreach ( $segments_count_array as $segments_count_item ) {
309
+        foreach ($segments_count_array as $segments_count_item) {
310 310
 
311 311
             $id = 'word_count_tu[' . $this->currentTransUnitId . '][' . $index . ']';
312 312
             $index++;
313 313
 
314 314
             $tag .= "    <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\">
315
-                                <mda:meta type=\"x-matecat-raw\">" . $segments_count_item[ 'raw_word_count' ] . "</mda:meta>
316
-                                <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item[ 'eq_word_count' ] . "</mda:meta>
315
+                                <mda:meta type=\"x-matecat-raw\">" . $segments_count_item['raw_word_count'] . "</mda:meta>
316
+                                <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item['eq_word_count'] . "</mda:meta>
317 317
                             </mda:metaGroup>";
318 318
         }
319 319
 
320
-        if ( $withMetadataTag === true ) {
320
+        if ($withMetadataTag === true) {
321 321
             $tag .= '</mda:metadata>';
322 322
         }
323 323
 
@@ -332,18 +332,18 @@  discard block
 block discarded – undo
332 332
      *
333 333
      * @return string
334 334
      */
335
-    protected function prepareTranslation( array $seg ): string {
335
+    protected function prepareTranslation(array $seg): string {
336 336
 
337
-        $segment     = Strings::removeDangerousChars( $seg [ 'segment' ] );
338
-        $translation = Strings::removeDangerousChars( $seg [ 'translation' ] );
339
-        $dataRefMap  = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : [];
337
+        $segment     = Strings::removeDangerousChars($seg ['segment']);
338
+        $translation = Strings::removeDangerousChars($seg ['translation']);
339
+        $dataRefMap  = (isset($seg['data_ref_map'])) ? Strings::jsonToArray($seg['data_ref_map']) : [];
340 340
 
341
-        if ( $seg [ 'translation' ] == '' ) {
341
+        if ($seg ['translation'] == '') {
342 342
             $translation = $segment;
343 343
         } else {
344
-            if ( $this->callback instanceof XliffReplacerCallbackInterface ) {
345
-                $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null;
346
-                if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) {
344
+            if ($this->callback instanceof XliffReplacerCallbackInterface) {
345
+                $error = (!empty($seg['error'])) ? $seg['error'] : null;
346
+                if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) {
347 347
                     $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||';
348 348
                 }
349 349
             }
Please login to merge, or discard this patch.
src/XliffReplacer/AbstractXliffReplacer.php 1 patch
Spacing   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -8,27 +8,27 @@  discard block
 block discarded – undo
8 8
 abstract class AbstractXliffReplacer {
9 9
     protected $originalFP;
10 10
 
11
-    protected $tuTagName;                 // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*)
12
-    protected $inTU                = false;  // flag to check whether we are in a <trans-unit>
13
-    protected $inTarget            = false;  // flag to check whether we are in a <target>, to ignore everything
14
-    protected $isEmpty             = false;  // flag to check whether we are in an empty tag (<tag/>)
15
-    protected $targetWasWritten    = false;  // flag to check is <target> was written in the current unit
16
-    protected $segmentPositionInTu = -1;  // the current position of segment in the current <unit> (forXliff v 2.*)
17
-
18
-    protected $CDATABuffer    = "";       // buffer for special tag
19
-    protected $bufferIsActive = false;    // buffer for special tag
20
-
21
-    protected     $offset                = 0;         // offset for SAX pointer
22
-    protected     $outputFP;                  // output stream pointer
23
-    protected     $currentBuffer;             // the current piece of text it's been parsed
24
-    protected     $len;                       // length of the currentBuffer
25
-    protected     $segments;                  // array of translations
11
+    protected $tuTagName; // <trans-unit> (forXliff v 1.*) or <unit> (forXliff v 2.*)
12
+    protected $inTU                = false; // flag to check whether we are in a <trans-unit>
13
+    protected $inTarget            = false; // flag to check whether we are in a <target>, to ignore everything
14
+    protected $isEmpty             = false; // flag to check whether we are in an empty tag (<tag/>)
15
+    protected $targetWasWritten    = false; // flag to check is <target> was written in the current unit
16
+    protected $segmentPositionInTu = -1; // the current position of segment in the current <unit> (forXliff v 2.*)
17
+
18
+    protected $CDATABuffer    = ""; // buffer for special tag
19
+    protected $bufferIsActive = false; // buffer for special tag
20
+
21
+    protected     $offset = 0; // offset for SAX pointer
22
+    protected     $outputFP; // output stream pointer
23
+    protected     $currentBuffer; // the current piece of text it's been parsed
24
+    protected     $len; // length of the currentBuffer
25
+    protected     $segments; // array of translations
26 26
     protected     $lastTransUnit         = [];
27 27
     protected int $segmentInUnitPosition = 0;
28
-    protected     $currentTransUnitId;        // id of current <trans-unit>
28
+    protected     $currentTransUnitId; // id of current <trans-unit>
29 29
     protected     $currentTransUnitIsTranslatable; // 'translate' attribute of current <trans-unit>
30
-    protected     $unitContainsMda       = false;   // check if <unit> already contains a <mda:metadata> (forXliff v 2.*)
31
-    protected     $hasWrittenCounts      = false;  // check if <unit> already wrote segment counts (forXliff v 2.*)
30
+    protected     $unitContainsMda       = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*)
31
+    protected     $hasWrittenCounts      = false; // check if <unit> already wrote segment counts (forXliff v 2.*)
32 32
 
33 33
     protected $targetLang;
34 34
 
@@ -75,8 +75,8 @@  discard block
 block discarded – undo
75 75
             XliffReplacerCallbackInterface $callback = null
76 76
     ) {
77 77
         self::$INTERNAL_TAG_PLACEHOLDER = $this->getInternalTagPlaceholder();
78
-        $this->createOutputFileIfDoesNotExist( $outputFilePath );
79
-        $this->setFileDescriptors( $originalXliffPath, $outputFilePath );
78
+        $this->createOutputFileIfDoesNotExist($outputFilePath);
79
+        $this->setFileDescriptors($originalXliffPath, $outputFilePath);
80 80
         $this->xliffVersion = $xliffVersion;
81 81
         $this->setTuTagName();
82 82
         $this->segments       = $segments;
@@ -88,17 +88,17 @@  discard block
 block discarded – undo
88 88
     }
89 89
 
90 90
     public function replaceTranslation() {
91
-        fwrite( $this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>' );
91
+        fwrite($this->outputFP, '<?xml version="1.0" encoding="UTF-8"?>');
92 92
 
93 93
         //create Sax parser
94 94
         $xmlParser = $this->initSaxParser();
95 95
 
96
-        while ( $this->currentBuffer = fread( $this->originalFP, 4096 ) ) {
96
+        while ($this->currentBuffer = fread($this->originalFP, 4096)) {
97 97
             /*
98 98
                preprocess file
99 99
              */
100 100
             // obfuscate entities because sax automatically does html_entity_decode
101
-            $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
101
+            $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
102 102
 
103 103
             //avoid cutting entities in half:
104 104
             //the last fread could have truncated an entity (say, '&lt;' in '&l'), thus invalidating the escaping
@@ -110,67 +110,67 @@  discard block
 block discarded – undo
110 110
             // add 9 Bytes and substitute the entities, if the & is present, and it is not at the end
111 111
             //it can't be an entity, exit the loop
112 112
 
113
-            while ( true ) {
114
-                $_ampPos = strpos( $temporary_check_buffer, '&' );
113
+            while (true) {
114
+                $_ampPos = strpos($temporary_check_buffer, '&');
115 115
 
116 116
                 //check for real entity or escape it to safely exit from the loop!!!
117
-                if ( $_ampPos === false || strlen( substr( $temporary_check_buffer, $_ampPos ) ) > 9 ) {
117
+                if ($_ampPos === false || strlen(substr($temporary_check_buffer, $_ampPos)) > 9) {
118 118
                     $escape_AMP = true;
119 119
                     break;
120 120
                 }
121 121
 
122 122
                 //if an entity is still present, fetch some more and repeat the escaping
123
-                $this->currentBuffer    .= fread( $this->originalFP, 9 );
124
-                $temporary_check_buffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
123
+                $this->currentBuffer .= fread($this->originalFP, 9);
124
+                $temporary_check_buffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
125 125
             }
126 126
 
127 127
             //free stuff outside the loop
128
-            unset( $temporary_check_buffer );
128
+            unset($temporary_check_buffer);
129 129
 
130
-            $this->currentBuffer = preg_replace( "/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
131
-            if ( $escape_AMP ) {
132
-                $this->currentBuffer = str_replace( "&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer );
130
+            $this->currentBuffer = preg_replace("/&(.*?);/", self::$INTERNAL_TAG_PLACEHOLDER . '$1' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
131
+            if ($escape_AMP) {
132
+                $this->currentBuffer = str_replace("&", self::$INTERNAL_TAG_PLACEHOLDER . 'amp' . self::$INTERNAL_TAG_PLACEHOLDER, $this->currentBuffer);
133 133
             }
134 134
 
135 135
             //get length of chunk
136
-            $this->len = strlen( $this->currentBuffer );
136
+            $this->len = strlen($this->currentBuffer);
137 137
 
138 138
             //parse chunk of text
139
-            if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) {
139
+            if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) {
140 140
                 //if unable, raise an exception
141
-                throw new RuntimeException( sprintf(
141
+                throw new RuntimeException(sprintf(
142 142
                         "XML error: %s at line %d",
143
-                        xml_error_string( xml_get_error_code( $xmlParser ) ),
144
-                        xml_get_current_line_number( $xmlParser )
145
-                ) );
143
+                        xml_error_string(xml_get_error_code($xmlParser)),
144
+                        xml_get_current_line_number($xmlParser)
145
+                ));
146 146
             }
147 147
             //get accumulated this->offset in document: as long as SAX pointer advances, we keep track of total bytes it has seen so far; this way, we can translate its global pointer in an address local to the current buffer of text to retrieve last char of tag
148 148
             $this->offset += $this->len;
149 149
         }
150 150
 
151 151
         // close Sax parser
152
-        $this->closeSaxParser( $xmlParser );
152
+        $this->closeSaxParser($xmlParser);
153 153
 
154 154
     }
155 155
 
156
-    protected function getLastCharacter( $parser ) {
156
+    protected function getLastCharacter($parser) {
157 157
 
158 158
         //this logic helps detecting empty tags
159 159
         //get current position of SAX pointer in all the stream of data is has read so far:
160 160
         //it points at the end of current tag
161
-        $idx = xml_get_current_byte_index( $parser );
161
+        $idx = xml_get_current_byte_index($parser);
162 162
 
163 163
         //check whether the bounds of current tag are entirely in current buffer or the end of the current tag
164 164
         //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop);
165 165
         //this check is necessary because we may have truncated a tag in half with current read,
166 166
         //and the other half may be encountered in the next buffer it will be passed
167
-        if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) {
167
+        if (isset($this->currentBuffer[$idx - $this->offset])) {
168 168
             //if this tag entire lenght fitted in the buffer, the last char must be the last
169 169
             //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/'
170
-            $lastChar = $this->currentBuffer[ $idx - $this->offset ];
170
+            $lastChar = $this->currentBuffer[$idx - $this->offset];
171 171
         } else {
172 172
             //if it's out, simple use the last character of the chunk
173
-            $lastChar = $this->currentBuffer[ $this->len - 1 ];
173
+            $lastChar = $this->currentBuffer[$this->len - 1];
174 174
         }
175 175
 
176 176
         return $lastChar;
@@ -184,19 +184,19 @@  discard block
 block discarded – undo
184 184
         return "§" .
185 185
                 substr(
186 186
                         str_replace(
187
-                                [ '+', '/' ],
187
+                                ['+', '/'],
188 188
                                 '',
189
-                                base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) )
189
+                                base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong))
190 190
                         ),
191 191
                         0,
192 192
                         4
193 193
                 );
194 194
     }
195 195
 
196
-    private function createOutputFileIfDoesNotExist( $outputFilePath ) {
196
+    private function createOutputFileIfDoesNotExist($outputFilePath) {
197 197
         // create output file
198
-        if ( !file_exists( $outputFilePath ) ) {
199
-            touch( $outputFilePath );
198
+        if (!file_exists($outputFilePath)) {
199
+            touch($outputFilePath);
200 200
         }
201 201
     }
202 202
 
@@ -204,13 +204,13 @@  discard block
 block discarded – undo
204 204
      * @param $originalXliffPath
205 205
      * @param $outputFilePath
206 206
      */
207
-    private function setFileDescriptors( $originalXliffPath, $outputFilePath ) {
208
-        $this->outputFP = fopen( $outputFilePath, 'w+' );
207
+    private function setFileDescriptors($originalXliffPath, $outputFilePath) {
208
+        $this->outputFP = fopen($outputFilePath, 'w+');
209 209
 
210 210
         $streamArgs = null;
211 211
 
212
-        if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) {
213
-            throw new RuntimeException( "could not open XML input" );
212
+        if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) {
213
+            throw new RuntimeException("could not open XML input");
214 214
         }
215 215
     }
216 216
 
@@ -219,7 +219,7 @@  discard block
 block discarded – undo
219 219
      * <trans-unit> (xliff v1.*) || <unit> (xliff v2.*)
220 220
      */
221 221
     private function setTuTagName() {
222
-        $this->tuTagName = ( $this->xliffVersion === 2 ) ? 'unit' : 'trans-unit';
222
+        $this->tuTagName = ($this->xliffVersion === 2) ? 'unit' : 'trans-unit';
223 223
     }
224 224
 
225 225
     /**
@@ -228,8 +228,8 @@  discard block
 block discarded – undo
228 228
     public function __destruct() {
229 229
         //this stream can be closed outside the class
230 230
         //to permit multiple concurrent downloads, so suppress warnings
231
-        @fclose( $this->originalFP );
232
-        fclose( $this->outputFP );
231
+        @fclose($this->originalFP);
232
+        fclose($this->outputFP);
233 233
     }
234 234
 
235 235
     /**
@@ -238,11 +238,11 @@  discard block
 block discarded – undo
238 238
      * @return resource
239 239
      */
240 240
     protected function initSaxParser() {
241
-        $xmlSaxParser = xml_parser_create( 'UTF-8' );
242
-        xml_set_object( $xmlSaxParser, $this );
243
-        xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false );
244
-        xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' );
245
-        xml_set_character_data_handler( $xmlSaxParser, 'characterData' );
241
+        $xmlSaxParser = xml_parser_create('UTF-8');
242
+        xml_set_object($xmlSaxParser, $this);
243
+        xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false);
244
+        xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose');
245
+        xml_set_character_data_handler($xmlSaxParser, 'characterData');
246 246
 
247 247
         return $xmlSaxParser;
248 248
     }
@@ -250,8 +250,8 @@  discard block
 block discarded – undo
250 250
     /**
251 251
      * @param resource $xmlSaxParser
252 252
      */
253
-    protected function closeSaxParser( $xmlSaxParser ) {
254
-        xml_parser_free( $xmlSaxParser );
253
+    protected function closeSaxParser($xmlSaxParser) {
254
+        xml_parser_free($xmlSaxParser);
255 255
     }
256 256
 
257 257
     /**
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
      *
262 262
      * @return mixed
263 263
      */
264
-    abstract protected function tagOpen( $parser, $name, $attr );
264
+    abstract protected function tagOpen($parser, $name, $attr);
265 265
 
266 266
     /**
267 267
      * @param $parser
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
      *
270 270
      * @return mixed
271 271
      */
272
-    abstract protected function tagClose( $parser, $name );
272
+    abstract protected function tagClose($parser, $name);
273 273
 
274 274
     /**
275 275
      * @param $parser
@@ -277,11 +277,11 @@  discard block
 block discarded – undo
277 277
      *
278 278
      * @return mixed
279 279
      */
280
-    protected function characterData( $parser, $data ): void {
280
+    protected function characterData($parser, $data): void {
281 281
         // don't write <target> data
282
-        if ( !$this->inTarget && !$this->bufferIsActive ) {
283
-            $this->postProcAndFlush( $this->outputFP, $data );
284
-        } elseif ( $this->bufferIsActive ) {
282
+        if (!$this->inTarget && !$this->bufferIsActive) {
283
+            $this->postProcAndFlush($this->outputFP, $data);
284
+        } elseif ($this->bufferIsActive) {
285 285
             $this->CDATABuffer .= $data;
286 286
         }
287 287
     }
@@ -293,34 +293,34 @@  discard block
 block discarded – undo
293 293
      * @param string   $data
294 294
      * @param bool     $treatAsCDATA
295 295
      */
296
-    protected function postProcAndFlush( $fp, $data, $treatAsCDATA = false ) {
296
+    protected function postProcAndFlush($fp, $data, $treatAsCDATA = false) {
297 297
         //postprocess string
298
-        $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data );
299
-        $data = str_replace( '&nbsp;', ' ', $data );
300
-        if ( !$treatAsCDATA ) {
298
+        $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data);
299
+        $data = str_replace('&nbsp;', ' ', $data);
300
+        if (!$treatAsCDATA) {
301 301
             //unix2dos
302
-            $data = str_replace( "\r\n", "\r", $data );
303
-            $data = str_replace( "\n", "\r", $data );
304
-            $data = str_replace( "\r", "\r\n", $data );
302
+            $data = str_replace("\r\n", "\r", $data);
303
+            $data = str_replace("\n", "\r", $data);
304
+            $data = str_replace("\r", "\r\n", $data);
305 305
         }
306 306
 
307 307
         //flush to disk
308
-        fwrite( $fp, $data );
308
+        fwrite($fp, $data);
309 309
     }
310 310
 
311
-    protected function handleOpenUnit( string $name, array $attr ) {
311
+    protected function handleOpenUnit(string $name, array $attr) {
312 312
 
313 313
         // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
314
-        if ( $this->tuTagName === $name ) {
314
+        if ($this->tuTagName === $name) {
315 315
             $this->inTU = true;
316 316
 
317 317
             // get id
318 318
             // trim to first 100 characters because this is the limit on Matecat's DB
319
-            $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 );
319
+            $this->currentTransUnitId = substr($attr['id'], 0, 100);
320 320
 
321 321
             // `translate` attribute can be only yes or no
322 322
             // current 'translate' attribute of the current trans-unit
323
-            $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ];
323
+            $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate'];
324 324
 
325 325
             $this->setLastTransUnitSegments();
326 326
 
@@ -330,36 +330,36 @@  discard block
 block discarded – undo
330 330
     /**
331 331
      * @param array $seg
332 332
      */
333
-    protected function updateSegmentCounts( array $seg = [] ) {
333
+    protected function updateSegmentCounts(array $seg = []) {
334 334
 
335
-        $raw_word_count = $seg[ 'raw_word_count' ];
336
-        $eq_word_count  = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 );
335
+        $raw_word_count = $seg['raw_word_count'];
336
+        $eq_word_count  = (floor($seg['eq_word_count'] * 100) / 100);
337 337
 
338
-        $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [
338
+        $this->counts['segments_count_array'][$seg['sid']] = [
339 339
                 'raw_word_count' => $raw_word_count,
340 340
                 'eq_word_count'  => $eq_word_count,
341 341
         ];
342 342
 
343
-        $this->counts[ 'raw_word_count' ] += $raw_word_count;
344
-        $this->counts[ 'eq_word_count' ]  += $eq_word_count;
343
+        $this->counts['raw_word_count'] += $raw_word_count;
344
+        $this->counts['eq_word_count']  += $eq_word_count;
345 345
     }
346 346
 
347 347
     protected function resetCounts() {
348
-        $this->counts[ 'segments_count_array' ] = [];
349
-        $this->counts[ 'raw_word_count' ]       = 0;
350
-        $this->counts[ 'eq_word_count' ]        = 0;
348
+        $this->counts['segments_count_array'] = [];
349
+        $this->counts['raw_word_count']       = 0;
350
+        $this->counts['eq_word_count']        = 0;
351 351
     }
352 352
 
353
-    protected function checkForSelfClosedTagAndFlush( $parser, $tag ) {
353
+    protected function checkForSelfClosedTagAndFlush($parser, $tag) {
354 354
 
355
-        $lastChar = $this->getLastCharacter( $parser );
355
+        $lastChar = $this->getLastCharacter($parser);
356 356
 
357 357
         //trim last space
358
-        $tag = rtrim( $tag );
358
+        $tag = rtrim($tag);
359 359
 
360 360
         //detect empty tag
361 361
         $this->isEmpty = $lastChar == '/';
362
-        if ( $this->isEmpty ) {
362
+        if ($this->isEmpty) {
363 363
             $tag .= $lastChar;
364 364
         }
365 365
 
@@ -367,11 +367,11 @@  discard block
 block discarded – undo
367 367
         $tag .= ">";
368 368
 
369 369
         //set a Buffer for the segSource Source tag
370
-        if ( $this->bufferIsActive ) { // we are opening a critical CDATA section
370
+        if ($this->bufferIsActive) { // we are opening a critical CDATA section
371 371
             //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer
372 372
             $this->CDATABuffer .= $tag;
373 373
         } else {
374
-            $this->postProcAndFlush( $this->outputFP, $tag );
374
+            $this->postProcAndFlush($this->outputFP, $tag);
375 375
         }
376 376
 
377 377
     }
@@ -396,18 +396,18 @@  discard block
 block discarded – undo
396 396
          */
397 397
         $this->lastTransUnit = [];
398 398
 
399
-        if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
399
+        if (!isset($this->transUnits[$this->currentTransUnitId])) {
400 400
             return;
401 401
         }
402 402
 
403
-        $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ];
403
+        $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId];
404 404
         $last_value        = null;
405
-        $segmentsCount     = count( $listOfSegmentsIds );
406
-        for ( $i = 0; $i < $segmentsCount; $i++ ) {
407
-            $id = $listOfSegmentsIds[ $i ];
408
-            if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) {
409
-                $last_value            = $listOfSegmentsIds[ $i ];
410
-                $this->lastTransUnit[] = $this->segments[ $id ];
405
+        $segmentsCount     = count($listOfSegmentsIds);
406
+        for ($i = 0; $i < $segmentsCount; $i++) {
407
+            $id = $listOfSegmentsIds[$i];
408
+            if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) {
409
+                $last_value            = $listOfSegmentsIds[$i];
410
+                $this->lastTransUnit[] = $this->segments[$id];
411 411
             }
412 412
         }
413 413
 
@@ -417,8 +417,8 @@  discard block
 block discarded – undo
417 417
      * @return array
418 418
      */
419 419
     protected function getCurrentSegment(): array {
420
-        if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
421
-            return $this->segments[ $this->segmentInUnitPosition ];
420
+        if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) {
421
+            return $this->segments[$this->segmentInUnitPosition];
422 422
         }
423 423
 
424 424
         return [];
Please login to merge, or discard this patch.
src/XliffReplacer/XliffReplacerFactory.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -28,14 +28,14 @@
 block discarded – undo
28 28
             LoggerInterface                $logger = null,
29 29
             XliffReplacerCallbackInterface $callback = null
30 30
     ) {
31
-        $info = XliffProprietaryDetect::getInfo( $originalXliffPath );
31
+        $info = XliffProprietaryDetect::getInfo($originalXliffPath);
32 32
 
33
-        if ( $info[ 'version' ] == 1 && $info[ 'proprietary_short_name' ] !== 'trados' ) {
34
-            return new Xliff12( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
35
-        } elseif ( $info[ 'version' ] == 2 ) {
36
-            return new Xliff20( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
33
+        if ($info['version'] == 1 && $info['proprietary_short_name'] !== 'trados') {
34
+            return new Xliff12($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
35
+        } elseif ($info['version'] == 2) {
36
+            return new Xliff20($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
37 37
         }
38 38
 
39
-        return new XliffSdl( $originalXliffPath, $info[ 'version' ], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback );
39
+        return new XliffSdl($originalXliffPath, $info['version'], $data, $transUnits, $targetLang, $outputFilePath, $setSourceInTarget, $logger, $callback);
40 40
     }
41 41
 }
Please login to merge, or discard this patch.
src/XliffReplacer/Xliff12.php 1 patch
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -27,14 +27,14 @@  discard block
 block discarded – undo
27 27
     /**
28 28
      * @inheritDoc
29 29
      */
30
-    protected function tagOpen( $parser, $name, $attr ) {
30
+    protected function tagOpen($parser, $name, $attr) {
31 31
 
32
-        $this->handleOpenUnit( $name, $attr );
32
+        $this->handleOpenUnit($name, $attr);
33 33
 
34 34
         // check if we are entering into a <target>
35
-        if ( 'target' === $name ) {
35
+        if ('target' === $name) {
36 36
 
37
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
37
+            if ($this->currentTransUnitIsTranslatable === 'no') {
38 38
                 $this->inTarget = false;
39 39
             } else {
40 40
                 $this->inTarget = true;
@@ -42,23 +42,23 @@  discard block
 block discarded – undo
42 42
         }
43 43
 
44 44
         // open buffer
45
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
45
+        if (in_array($name, $this->nodesToBuffer)) {
46 46
             $this->bufferIsActive = true;
47 47
         }
48 48
 
49 49
         // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit
50 50
         // <target> must be stripped to be replaced, so this check avoids <target> reconstruction
51
-        if ( !$this->inTarget ) {
51
+        if (!$this->inTarget) {
52 52
 
53 53
             $tag = '';
54 54
 
55 55
             // construct tag
56 56
             $tag .= "<$name ";
57 57
 
58
-            foreach ( $attr as $k => $v ) {
58
+            foreach ($attr as $k => $v) {
59 59
 
60 60
                 //if tag name is file, we must replace the target-language attribute
61
-                if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) {
61
+                if ($name === 'file' && $k === 'target-language' && !empty($this->targetLang)) {
62 62
                     //replace Target language with job language provided from constructor
63 63
                     $tag .= "$k=\"$this->targetLang\" ";
64 64
                 } else {
@@ -69,11 +69,11 @@  discard block
 block discarded – undo
69 69
 
70 70
             $seg = $this->getCurrentSegment();
71 71
 
72
-            if ( $name === $this->tuTagName && !empty( $seg ) and isset( $seg[ 'sid' ] ) ) {
72
+            if ($name === $this->tuTagName && !empty($seg) and isset($seg['sid'])) {
73 73
 
74
-                if ( strpos( $tag, 'help-id' ) === false ) {
75
-                    if ( !empty( $seg[ 'sid' ] ) ) {
76
-                        $tag .= "help-id=\"{$seg[ 'sid' ]}\" ";
74
+                if (strpos($tag, 'help-id') === false) {
75
+                    if (!empty($seg['sid'])) {
76
+                        $tag .= "help-id=\"{$seg['sid']}\" ";
77 77
                     }
78 78
                 }
79 79
 
@@ -81,14 +81,14 @@  discard block
 block discarded – undo
81 81
 
82 82
             // Add MateCat specific namespace.
83 83
             // Add trgLang
84
-            if ( $name === 'xliff' ) {
85
-                if ( !array_key_exists( 'xmlns:mtc', $attr ) ) {
84
+            if ($name === 'xliff') {
85
+                if (!array_key_exists('xmlns:mtc', $attr)) {
86 86
                     $tag .= ' xmlns:mtc="https://www.matecat.com" ';
87 87
                 }
88
-                $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag );
88
+                $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag);
89 89
             }
90 90
 
91
-            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
91
+            $this->checkForSelfClosedTagAndFlush($parser, $tag);
92 92
 
93 93
         }
94 94
 
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
     /**
99 99
      * @inheritDoc
100 100
      */
101
-    protected function tagClose( $parser, $name ) {
101
+    protected function tagClose($parser, $name) {
102 102
         $tag = '';
103 103
 
104 104
         /**
@@ -107,15 +107,15 @@  discard block
 block discarded – undo
107 107
          *
108 108
          * self::tagOpen method
109 109
          */
110
-        if ( !$this->isEmpty ) {
110
+        if (!$this->isEmpty) {
111 111
 
112
-            if ( !$this->inTarget ) {
112
+            if (!$this->inTarget) {
113 113
                 $tag = "</$name>";
114 114
             }
115 115
 
116
-            if ( 'target' == $name ) {
116
+            if ('target' == $name) {
117 117
 
118
-                if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
118
+                if (isset($this->transUnits[$this->currentTransUnitId])) {
119 119
 
120 120
                     // get translation of current segment, by indirect indexing: id -> positional index -> segment
121 121
                     // actually there may be more than one segment to that ID if there are two mrk of the same source segment
@@ -126,42 +126,42 @@  discard block
 block discarded – undo
126 126
                 $this->targetWasWritten = true;
127 127
                 // signal we are leaving a target
128 128
                 $this->inTarget = false;
129
-                $this->postProcAndFlush( $this->outputFP, $tag, true );
129
+                $this->postProcAndFlush($this->outputFP, $tag, true);
130 130
 
131
-            } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section
131
+            } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section
132 132
 
133 133
                 $this->bufferIsActive = false;
134 134
                 $tag                  = $this->CDATABuffer . "</$name>";
135 135
                 $this->CDATABuffer    = "";
136 136
 
137 137
                 //flush to the pointer
138
-                $this->postProcAndFlush( $this->outputFP, $tag );
138
+                $this->postProcAndFlush($this->outputFP, $tag);
139 139
 
140
-            } elseif ( $name === $this->tuTagName ) {
140
+            } elseif ($name === $this->tuTagName) {
141 141
 
142 142
                 $tag = "";
143 143
 
144 144
                 // handling </trans-unit> closure
145
-                if ( !$this->targetWasWritten ) {
145
+                if (!$this->targetWasWritten) {
146 146
 
147
-                    if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
147
+                    if (isset($this->transUnits[$this->currentTransUnitId])) {
148 148
                         $tag = $this->rebuildTarget();
149 149
                     } else {
150
-                        $tag = $this->createTargetTag( "", "" );
150
+                        $tag = $this->createTargetTag("", "");
151 151
                     }
152 152
 
153 153
                 }
154 154
 
155
-                $tag                    .= "</$this->tuTagName>";
155
+                $tag .= "</$this->tuTagName>";
156 156
                 $this->targetWasWritten = false;
157
-                $this->postProcAndFlush( $this->outputFP, $tag );
157
+                $this->postProcAndFlush($this->outputFP, $tag);
158 158
 
159
-            } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
159
+            } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
160 160
                 $this->CDATABuffer .= "</$name>";
161 161
                 // Do NOT Flush
162 162
             } else { //generic tag closure do Nothing
163 163
                 // flush to pointer
164
-                $this->postProcAndFlush( $this->outputFP, $tag );
164
+                $this->postProcAndFlush($this->outputFP, $tag);
165 165
             }
166 166
 
167 167
         } else {
@@ -170,7 +170,7 @@  discard block
 block discarded – undo
170 170
         }
171 171
 
172 172
         // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
173
-        if ( $this->tuTagName === $name ) {
173
+        if ($this->tuTagName === $name) {
174 174
             $this->currentTransUnitIsTranslatable = null;
175 175
             $this->inTU                           = false;
176 176
             $this->hasWrittenCounts               = false;
@@ -187,35 +187,35 @@  discard block
 block discarded – undo
187 187
      *
188 188
      * @return string
189 189
      */
190
-    protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string {
190
+    protected function prepareTranslation(array $seg, string $transUnitTranslation = ""): string {
191 191
 
192
-        $segment     = Strings::removeDangerousChars( $seg [ 'segment' ] );
193
-        $translation = Strings::removeDangerousChars( $seg [ 'translation' ] );
192
+        $segment     = Strings::removeDangerousChars($seg ['segment']);
193
+        $translation = Strings::removeDangerousChars($seg ['translation']);
194 194
 
195
-        if ( $seg [ 'translation' ] == '' ) {
195
+        if ($seg ['translation'] == '') {
196 196
             $translation = $segment;
197 197
         } else {
198
-            if ( $this->callback instanceof XliffReplacerCallbackInterface ) {
199
-                $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null;
200
-                if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) {
198
+            if ($this->callback instanceof XliffReplacerCallbackInterface) {
199
+                $error = (!empty($seg['error'])) ? $seg['error'] : null;
200
+                if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, [], $error)) {
201 201
                     $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||';
202 202
                 }
203 203
             }
204 204
         }
205 205
 
206
-        $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] );
206
+        $transUnitTranslation .= $seg['prev_tags'] . $this->rebuildMarks($seg, $translation) . ltrim($seg['succ_tags']);
207 207
 
208 208
         return $transUnitTranslation;
209 209
     }
210 210
 
211
-    protected function rebuildMarks( array $seg, string $translation ): string {
211
+    protected function rebuildMarks(array $seg, string $translation): string {
212 212
 
213
-        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
214
-            if ( $this->targetLang === 'ja-JP' ) {
215
-                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
213
+        if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') {
214
+            if ($this->targetLang === 'ja-JP') {
215
+                $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']);
216 216
             }
217 217
 
218
-            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>";
218
+            $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . $translation . $seg['mrk_succ_tags'] . "</mrk>";
219 219
         }
220 220
 
221 221
         return $translation;
@@ -230,10 +230,10 @@  discard block
 block discarded – undo
230 230
      *
231 231
      * @return string
232 232
      */
233
-    private function createTargetTag( string $translation, string $stateProp ): string {
233
+    private function createTargetTag(string $translation, string $stateProp): string {
234 234
         $targetLang = ' xml:lang="' . $this->targetLang . '"';
235 235
         $tag        = "<target $targetLang $stateProp>$translation</target>";
236
-        $tag        .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts[ 'raw_word_count' ] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts[ 'eq_word_count' ] . '</count></count-group>';
236
+        $tag .= "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">" . $this->counts['raw_word_count'] . "</count><count count-type=\"x-matecat-weighted\">" . $this->counts['eq_word_count'] . '</count></count-group>';
237 237
 
238 238
         return $tag;
239 239
 
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
         // we must reset the lastMrkId found because this is a new segment.
250 250
         $lastMrkId = -1;
251 251
 
252
-        foreach ( $this->lastTransUnit as $pos => $seg ) {
252
+        foreach ($this->lastTransUnit as $pos => $seg) {
253 253
 
254 254
             /*
255 255
              * This routine works to respect the positional orders of markers.
@@ -262,8 +262,8 @@  discard block
 block discarded – undo
262 262
              * pre-assign zero to the new mrk if this is the first one ( in this segment )
263 263
              * If it is null leave it NULL
264 264
              */
265
-            if ( (int)$seg[ "mrk_id" ] < 0 && $seg[ "mrk_id" ] !== null ) {
266
-                $seg[ "mrk_id" ] = 0;
265
+            if ((int)$seg["mrk_id"] < 0 && $seg["mrk_id"] !== null) {
266
+                $seg["mrk_id"] = 0;
267 267
             }
268 268
 
269 269
             /*
@@ -272,33 +272,33 @@  discard block
 block discarded – undo
272 272
              * ( null <= -1 ) === true
273 273
              * so, cast to int
274 274
              */
275
-            if ( (int)$seg[ "mrk_id" ] <= $lastMrkId ) {
275
+            if ((int)$seg["mrk_id"] <= $lastMrkId) {
276 276
                 break;
277 277
             }
278 278
 
279 279
             // update counts
280
-            if ( !empty( $seg ) ) {
281
-                $this->updateSegmentCounts( $seg );
280
+            if (!empty($seg)) {
281
+                $this->updateSegmentCounts($seg);
282 282
             }
283 283
 
284 284
             // delete translations so the prepareSegment
285 285
             // will put source content in target tag
286
-            if ( $this->sourceInTarget ) {
287
-                $seg[ 'translation' ] = '';
286
+            if ($this->sourceInTarget) {
287
+                $seg['translation'] = '';
288 288
                 $this->resetCounts();
289 289
             }
290 290
 
291 291
             // append $translation
292
-            $translation = $this->prepareTranslation( $seg, $translation );
292
+            $translation = $this->prepareTranslation($seg, $translation);
293 293
 
294
-            $lastMrkId = $seg[ "mrk_id" ];
294
+            $lastMrkId = $seg["mrk_id"];
295 295
 
296
-            [ $stateProp, $lastMrkState ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion, $stateProp, $lastMrkState );
296
+            [$stateProp, $lastMrkState] = StatusToStateAttribute::getState($seg['status'], $this->xliffVersion, $stateProp, $lastMrkState);
297 297
 
298 298
         }
299 299
 
300 300
         //append translation
301
-        return $this->createTargetTag( $translation, $stateProp );
301
+        return $this->createTargetTag($translation, $stateProp);
302 302
 
303 303
     }
304 304
 
Please login to merge, or discard this patch.
src/XliffReplacer/StatusToStateAttribute.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -22,26 +22,26 @@  discard block
 block discarded – undo
22 22
      *
23 23
      * @return array
24 24
      */
25
-    public static function getState( string $status, int $xliffVersion, ?string $state_prop = '', ?string $lastMrkState = '' ): array {
25
+    public static function getState(string $status, int $xliffVersion, ?string $state_prop = '', ?string $lastMrkState = ''): array {
26 26
 
27
-        switch ( $status ) {
27
+        switch ($status) {
28 28
 
29 29
             case TranslationStatus::STATUS_FIXED:
30 30
             case TranslationStatus::STATUS_APPROVED2:
31
-                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED2 ) {
31
+                if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED2) {
32 32
                     $state_prop   = "state=\"final\"";
33 33
                     $lastMrkState = TranslationStatus::STATUS_APPROVED2;
34 34
                 }
35 35
                 break;
36 36
             case TranslationStatus::STATUS_APPROVED:
37
-                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) {
38
-                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"";
37
+                if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED) {
38
+                    $state_prop   = ($xliffVersion === 2) ? "state=\"reviewed\"" : "state=\"signed-off\"";
39 39
                     $lastMrkState = TranslationStatus::STATUS_APPROVED;
40 40
                 }
41 41
                 break;
42 42
 
43 43
             case TranslationStatus::STATUS_TRANSLATED:
44
-                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED ) {
44
+                if ($lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED) {
45 45
                     $state_prop   = "state=\"translated\"";
46 46
                     $lastMrkState = TranslationStatus::STATUS_TRANSLATED;
47 47
                 }
@@ -49,22 +49,22 @@  discard block
 block discarded – undo
49 49
 
50 50
             case TranslationStatus::STATUS_REJECTED:  // if there is a mark REJECTED and there is not a DRAFT, all the trans-unit is REJECTED. In V2 there is no way to mark
51 51
             case TranslationStatus::STATUS_REBUTTED:
52
-                if ( ( $lastMrkState == null ) || ( $lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT ) ) {
53
-                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"";
52
+                if (($lastMrkState == null) || ($lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT)) {
53
+                    $state_prop   = ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"needs-review-translation\"";
54 54
                     $lastMrkState = TranslationStatus::STATUS_REJECTED;
55 55
                 }
56 56
                 break;
57 57
 
58 58
             case TranslationStatus::STATUS_NEW:
59
-                if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_NEW ) {
60
-                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
59
+                if (($lastMrkState == null) || $lastMrkState != TranslationStatus::STATUS_NEW) {
60
+                    $state_prop   = ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"";
61 61
                     $lastMrkState = TranslationStatus::STATUS_NEW;
62 62
                 }
63 63
                 break;
64 64
 
65 65
             case TranslationStatus::STATUS_DRAFT:
66
-                if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) {
67
-                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
66
+                if (($lastMrkState == null) || $lastMrkState != TranslationStatus::STATUS_DRAFT) {
67
+                    $state_prop   = ($xliffVersion === 2) ? "state=\"initial\"" : "state=\"new\"";
68 68
                     $lastMrkState = TranslationStatus::STATUS_DRAFT;
69 69
                 }
70 70
                 break;
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
                 // this is the case when a segment is not showed in cattool, so the row in
74 74
                 // segment_translations does not exists and
75 75
                 // ---> $seg[ 'status' ] is NULL
76
-                if ( $lastMrkState == null ) { //this is the first MRK ID
76
+                if ($lastMrkState == null) { //this is the first MRK ID
77 77
                     $state_prop   = "state=\"translated\"";
78 78
                     $lastMrkState = TranslationStatus::STATUS_TRANSLATED;
79 79
                 } else {
@@ -82,7 +82,7 @@  discard block
 block discarded – undo
82 82
                 break;
83 83
         }
84 84
 
85
-        return [ $state_prop, $lastMrkState ];
85
+        return [$state_prop, $lastMrkState];
86 86
 
87 87
     }
88 88
 
Please login to merge, or discard this patch.
src/XliffReplacer/XliffSdl.php 1 patch
Spacing   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -16,13 +16,13 @@  discard block
 block discarded – undo
16 16
     /**
17 17
      * @inheritDoc
18 18
      */
19
-    protected function tagOpen( $parser, $name, $attr ) {
19
+    protected function tagOpen($parser, $name, $attr) {
20 20
 
21
-        $this->handleOpenUnit( $name, $attr );
21
+        $this->handleOpenUnit($name, $attr);
22 22
 
23 23
         // check if we are entering into a <target>
24
-        if ( 'target' == $name ) {
25
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
24
+        if ('target' == $name) {
25
+            if ($this->currentTransUnitIsTranslatable === 'no') {
26 26
                 $this->inTarget = false;
27 27
             } else {
28 28
                 $this->inTarget = true;
@@ -30,18 +30,18 @@  discard block
 block discarded – undo
30 30
         }
31 31
 
32 32
         // reset Marker positions
33
-        if ( 'sdl:seg-defs' == $name ) {
33
+        if ('sdl:seg-defs' == $name) {
34 34
             $this->segmentInUnitPosition = 0;
35 35
         }
36 36
 
37 37
         // open buffer
38
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
38
+        if (in_array($name, $this->nodesToBuffer)) {
39 39
             $this->bufferIsActive = true;
40 40
         }
41 41
 
42 42
         // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit
43 43
         // <target> must be stripped to be replaced, so this check avoids <target> reconstruction
44
-        if ( !$this->inTarget ) {
44
+        if (!$this->inTarget) {
45 45
 
46 46
             // costruct tag
47 47
             $tag = "<$name ";
@@ -51,16 +51,16 @@  discard block
 block discarded – undo
51 51
             // so the check on it's name is not enough
52 52
             $_sdlStatus_confWritten = false;
53 53
 
54
-            foreach ( $attr as $k => $v ) {
54
+            foreach ($attr as $k => $v) {
55 55
 
56 56
                 // if tag name is file, we must replace the target-language attribute
57
-                if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) {
57
+                if ($name == 'file' && $k == 'target-language' && !empty($this->targetLang)) {
58 58
                     //replace Target language with job language provided from constructor
59 59
                     $tag .= "$k=\"$this->targetLang\" ";
60 60
                 } else {
61 61
                     //normal tag flux, put attributes in it
62 62
                     // Warning, this is NOT an elseif
63
-                    if ( $k != 'conf' ) {
63
+                    if ($k != 'conf') {
64 64
                         //put also the current attribute in it if it is not a "conf" attribute
65 65
                         $tag .= "$k=\"$v\" ";
66 66
                     }
@@ -69,11 +69,11 @@  discard block
 block discarded – undo
69 69
 
70 70
             $seg = $this->getCurrentSegment();
71 71
 
72
-            if ( 'sdl:seg' == $name && !empty( $seg ) and isset( $seg[ 'sid' ] ) ) {
73
-                $tag .= $this->prepareTargetStatuses( $seg );
72
+            if ('sdl:seg' == $name && !empty($seg) and isset($seg['sid'])) {
73
+                $tag .= $this->prepareTargetStatuses($seg);
74 74
             }
75 75
 
76
-            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
76
+            $this->checkForSelfClosedTagAndFlush($parser, $tag);
77 77
 
78 78
         }
79 79
 
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
      *
85 85
      * @return string
86 86
      */
87
-    protected function prepareTargetStatuses( $segment ): string {
87
+    protected function prepareTargetStatuses($segment): string {
88 88
         $statusMap = [
89 89
                 'NEW'        => '',
90 90
                 'DRAFT'      => 'Draft',
@@ -93,22 +93,22 @@  discard block
 block discarded – undo
93 93
                 'REJECTED'   => 'RejectedTranslation',
94 94
         ];
95 95
 
96
-        return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" ";
96
+        return "conf=\"{$statusMap[$segment['status']]}\" ";
97 97
     }
98 98
 
99
-    protected function rebuildMarks( array $seg, string $translation ): string {
99
+    protected function rebuildMarks(array $seg, string $translation): string {
100 100
 
101 101
         $trailingSpaces = '';
102
-        for ( $s = 0; $s < Strings::getTheNumberOfTrailingSpaces( $translation ); $s++ ) {
102
+        for ($s = 0; $s < Strings::getTheNumberOfTrailingSpaces($translation); $s++) {
103 103
             $trailingSpaces .= ' ';
104 104
         }
105 105
 
106
-        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
107
-            if ( $this->targetLang === 'ja-JP' ) {
108
-                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
106
+        if ($seg['mrk_id'] !== null && $seg['mrk_id'] != '') {
107
+            if ($this->targetLang === 'ja-JP') {
108
+                $seg['mrk_succ_tags'] = ltrim($seg['mrk_succ_tags']);
109 109
             }
110 110
 
111
-            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces;
111
+            $translation = "<mrk mid=\"" . $seg['mrk_id'] . "\" mtype=\"seg\">" . $seg['mrk_prev_tags'] . rtrim($translation) . $seg['mrk_succ_tags'] . "</mrk>" . $trailingSpaces;
112 112
         }
113 113
 
114 114
         return $translation;
Please login to merge, or discard this patch.