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