Passed
Pull Request — master (#90)
by Domenico
03:23
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 ) and isset( $seg[ 'sid' ] ) ) {
62
+            if ($name === $this->tuTagName && !empty($seg) and 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
 
@@ -73,48 +73,48 @@  discard block
 block discarded – undo
73 73
             // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit
74 74
             //
75 75
             if (
76
-                    ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) &&
76
+                    ($name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable') &&
77 77
                     $this->unitContainsMda === false &&
78
-                    !empty( $this->transUnits[ $this->currentTransUnitId ] ) &&
78
+                    !empty($this->transUnits[$this->currentTransUnitId]) &&
79 79
                     !$this->hasWrittenCounts
80 80
             ) {
81 81
                 // we need to update counts here
82 82
                 $this->updateCounts();
83 83
                 $this->hasWrittenCounts = true;
84
-                $tag                    .= $this->getWordCountGroupForXliffV2();
84
+                $tag .= $this->getWordCountGroupForXliffV2();
85 85
                 $this->unitContainsMda  = true;
86 86
             }
87 87
 
88 88
             // construct tag
89 89
             $tag .= "<$name ";
90 90
 
91
-            foreach ( $attr as $k => $v ) {
91
+            foreach ($attr as $k => $v) {
92 92
                 //normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute
93
-                if ( $k != 'state' ) {
93
+                if ($k != 'state') {
94 94
                     $tag .= "$k=\"$v\" ";
95 95
                 }
96 96
             }
97 97
 
98 98
             $seg = $this->getCurrentSegment();
99 99
 
100
-            if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) {
100
+            if ($name === $this->tuTagName and !empty($seg) and isset($seg['sid'])) {
101 101
 
102 102
                 // add `mtc:segment-id` to xliff v.2*
103
-                if ( strpos( $tag, 'mtc:segment-id' ) === false ) {
104
-                    $tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" ";
103
+                if (strpos($tag, 'mtc:segment-id') === false) {
104
+                    $tag .= "mtc:segment-id=\"{$seg['sid']}\" ";
105 105
                 }
106 106
 
107 107
             }
108 108
 
109 109
             // replace state for xliff v2
110
-            if ( 'segment' === $name ) { // add state to segment in Xliff v2
111
-                [ $stateProp, ] = StatusToStateAttribute::getState( $this->xliffVersion, $seg[ 'status' ] );
110
+            if ('segment' === $name) { // add state to segment in Xliff v2
111
+                [$stateProp, ] = StatusToStateAttribute::getState($this->xliffVersion, $seg['status']);
112 112
                 $tag .= $stateProp;
113 113
             }
114 114
 
115
-            $tag = $this->handleOpenXliffTag( $name, $attr, $tag );
115
+            $tag = $this->handleOpenXliffTag($name, $attr, $tag);
116 116
 
117
-            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
117
+            $this->checkForSelfClosedTagAndFlush($parser, $tag);
118 118
 
119 119
         }
120 120
 
@@ -127,10 +127,10 @@  discard block
 block discarded – undo
127 127
      *
128 128
      * @return string
129 129
      */
130
-    protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string {
131
-        $tag = parent::handleOpenXliffTag( $name, $attr, $tag );
130
+    protected function handleOpenXliffTag(string $name, array $attr, string $tag): string {
131
+        $tag = parent::handleOpenXliffTag($name, $attr, $tag);
132 132
         // add oasis xliff 20 namespace
133
-        if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) {
133
+        if ($name === 'xliff' && !array_key_exists('xmlns:mda', $attr)) {
134 134
             $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"';
135 135
         }
136 136
 
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
     /**
141 141
      * @inheritDoc
142 142
      */
143
-    protected function tagClose( $parser, string $name ) {
143
+    protected function tagClose($parser, string $name) {
144 144
         $tag = '';
145 145
 
146 146
         /**
@@ -149,32 +149,32 @@  discard block
 block discarded – undo
149 149
          *
150 150
          * self::tagOpen method
151 151
          */
152
-        if ( !$this->isEmpty ) {
152
+        if (!$this->isEmpty) {
153 153
 
154
-            if ( !$this->inTarget ) {
154
+            if (!$this->inTarget) {
155 155
                 $tag = "</$name>";
156 156
             }
157 157
 
158
-            if ( 'target' == $name ) {
158
+            if ('target' == $name) {
159 159
 
160
-                if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
160
+                if (isset($this->transUnits[$this->currentTransUnitId])) {
161 161
 
162 162
                     $seg = $this->getCurrentSegment();
163 163
 
164 164
                     // update counts
165
-                    if ( !$this->hasWrittenCounts && !empty( $seg ) ) {
166
-                        $this->updateSegmentCounts( $seg );
165
+                    if (!$this->hasWrittenCounts && !empty($seg)) {
166
+                        $this->updateSegmentCounts($seg);
167 167
                     }
168 168
 
169 169
                     // delete translations so the prepareSegment
170 170
                     // will put source content in target tag
171
-                    if ( $this->sourceInTarget ) {
172
-                        $seg[ 'translation' ] = '';
171
+                    if ($this->sourceInTarget) {
172
+                        $seg['translation'] = '';
173 173
                         $this->resetCounts();
174 174
                     }
175 175
 
176 176
                     // append $translation
177
-                    $translation = $this->prepareTranslation( $seg );
177
+                    $translation = $this->prepareTranslation($seg);
178 178
 
179 179
                     //append translation
180 180
                     $tag = "<target>$translation</target>";
@@ -184,22 +184,22 @@  discard block
 block discarded – undo
184 184
                 // signal we are leaving a target
185 185
                 $this->targetWasWritten = true;
186 186
                 $this->inTarget         = false;
187
-                $this->postProcAndFlush( $this->outputFP, $tag, true );
187
+                $this->postProcAndFlush($this->outputFP, $tag, true);
188 188
 
189
-            } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section
189
+            } elseif (in_array($name, $this->nodesToBuffer)) { // we are closing a critical CDATA section
190 190
 
191 191
                 $this->bufferIsActive = false;
192 192
 
193 193
                 // only for Xliff 2.*
194 194
                 // write here <mda:metaGroup> and <mda:meta> if already present in the <unit>
195
-                if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) {
195
+                if ('mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts) {
196 196
 
197 197
                     // we need to update counts here
198 198
                     $this->updateCounts();
199 199
                     $this->hasWrittenCounts = true;
200 200
 
201 201
                     $tag = $this->CDATABuffer;
202
-                    $tag .= $this->getWordCountGroupForXliffV2( false );
202
+                    $tag .= $this->getWordCountGroupForXliffV2(false);
203 203
                     $tag .= "    </mda:metadata>";
204 204
 
205 205
                 } else {
@@ -209,19 +209,19 @@  discard block
 block discarded – undo
209 209
                 $this->CDATABuffer = "";
210 210
 
211 211
                 //flush to the pointer
212
-                $this->postProcAndFlush( $this->outputFP, $tag );
212
+                $this->postProcAndFlush($this->outputFP, $tag);
213 213
 
214
-            } elseif ( 'segment' === $name ) {
214
+            } elseif ('segment' === $name) {
215 215
 
216 216
                 // only for Xliff 2.*
217 217
                 // if segment has no <target> add it BEFORE </segment>
218
-                if ( !$this->targetWasWritten ) {
218
+                if (!$this->targetWasWritten) {
219 219
 
220 220
                     $seg = $this->getCurrentSegment();
221 221
 
222
-                    if ( isset( $seg[ 'translation' ] ) ) {
222
+                    if (isset($seg['translation'])) {
223 223
 
224
-                        $translation = $this->prepareTranslation( $seg );
224
+                        $translation = $this->prepareTranslation($seg);
225 225
                         // replace the tag
226 226
                         $tag = "<target>$translation</target>";
227 227
 
@@ -234,17 +234,17 @@  discard block
 block discarded – undo
234 234
                 // update segmentPositionInTu
235 235
                 $this->segmentInUnitPosition++;
236 236
 
237
-                $this->postProcAndFlush( $this->outputFP, $tag );
237
+                $this->postProcAndFlush($this->outputFP, $tag);
238 238
 
239 239
                 // we are leaving <segment>, reset $segmentHasTarget
240 240
                 $this->targetWasWritten = false;
241 241
 
242
-            } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
242
+            } elseif ($this->bufferIsActive) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag
243 243
                 $this->CDATABuffer .= "</$name>";
244 244
                 // Do NOT Flush
245 245
             } else { //generic tag closure do Nothing
246 246
                 // flush to pointer
247
-                $this->postProcAndFlush( $this->outputFP, $tag );
247
+                $this->postProcAndFlush($this->outputFP, $tag);
248 248
             }
249 249
         } else {
250 250
             //ok, nothing to be done; reset flag for next coming tag
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
         }
253 253
 
254 254
         // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
255
-        if ( $this->tuTagName === $name ) {
255
+        if ($this->tuTagName === $name) {
256 256
             $this->currentTransUnitIsTranslatable = null;
257 257
             $this->inTU                           = false;
258 258
             $this->unitContainsMda                = false;
@@ -268,8 +268,8 @@  discard block
 block discarded – undo
268 268
     private function updateCounts() {
269 269
 
270 270
         $seg = $this->getCurrentSegment();
271
-        if ( !empty( $seg ) ) {
272
-            $this->updateSegmentCounts( $seg );
271
+        if (!empty($seg)) {
272
+            $this->updateSegmentCounts($seg);
273 273
         }
274 274
 
275 275
     }
@@ -279,30 +279,30 @@  discard block
 block discarded – undo
279 279
      *
280 280
      * @return string
281 281
      */
282
-    private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string {
282
+    private function getWordCountGroupForXliffV2(bool $withMetadataTag = true): string {
283 283
 
284 284
         $this->mdaGroupCounter++;
285
-        $segments_count_array = $this->counts[ 'segments_count_array' ];
285
+        $segments_count_array = $this->counts['segments_count_array'];
286 286
 
287 287
         $tag = '';
288 288
 
289
-        if ( $withMetadataTag === true ) {
289
+        if ($withMetadataTag === true) {
290 290
             $tag .= '<mda:metadata>';
291 291
         }
292 292
 
293 293
         $index = 0;
294
-        foreach ( $segments_count_array as $segments_count_item ) {
294
+        foreach ($segments_count_array as $segments_count_item) {
295 295
 
296 296
             $id = 'word_count_tu[' . $this->currentTransUnitId . '][' . $index . ']';
297 297
             $index++;
298 298
 
299 299
             $tag .= "    <mda:metaGroup id=\"" . $id . "\" category=\"row_xml_attribute\">
300
-                                <mda:meta type=\"x-matecat-raw\">" . $segments_count_item[ 'raw_word_count' ] . "</mda:meta>
301
-                                <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item[ 'eq_word_count' ] . "</mda:meta>
300
+                                <mda:meta type=\"x-matecat-raw\">" . $segments_count_item['raw_word_count'] . "</mda:meta>
301
+                                <mda:meta type=\"x-matecat-weighted\">" . $segments_count_item['eq_word_count'] . "</mda:meta>
302 302
                             </mda:metaGroup>";
303 303
         }
304 304
 
305
-        if ( $withMetadataTag === true ) {
305
+        if ($withMetadataTag === true) {
306 306
             $tag .= '</mda:metadata>';
307 307
         }
308 308
 
@@ -317,18 +317,18 @@  discard block
 block discarded – undo
317 317
      *
318 318
      * @return string
319 319
      */
320
-    protected function prepareTranslation( array $seg ): string {
320
+    protected function prepareTranslation(array $seg): string {
321 321
 
322
-        $segment     = Strings::removeDangerousChars( $seg [ 'segment' ] );
323
-        $translation = Strings::removeDangerousChars( $seg [ 'translation' ] );
324
-        $dataRefMap  = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : [];
322
+        $segment     = Strings::removeDangerousChars($seg ['segment']);
323
+        $translation = Strings::removeDangerousChars($seg ['translation']);
324
+        $dataRefMap  = (isset($seg['data_ref_map'])) ? Strings::jsonToArray($seg['data_ref_map']) : [];
325 325
 
326
-        if ( $seg [ 'translation' ] == '' ) {
326
+        if ($seg ['translation'] == '') {
327 327
             $translation = $segment;
328 328
         } else {
329
-            if ( $this->callback instanceof XliffReplacerCallbackInterface ) {
330
-                $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null;
331
-                if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) {
329
+            if ($this->callback instanceof XliffReplacerCallbackInterface) {
330
+                $error = (!empty($seg['error'])) ? $seg['error'] : null;
331
+                if ($this->callback->thereAreErrors($seg['sid'], $segment, $translation, $dataRefMap, $error)) {
332 332
                     $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||';
333 333
                 }
334 334
             }
Please login to merge, or discard this patch.
src/XliffReplacer/AbstractXliffReplacer.php 1 patch
Spacing   +110 added lines, -110 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,36 +107,36 @@  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
             //parse chunk of text
133
-            if ( !xml_parse( $xmlParser, $this->currentBuffer, feof( $this->originalFP ) ) ) {
133
+            if (!xml_parse($xmlParser, $this->currentBuffer, feof($this->originalFP))) {
134 134
                 //if unable, raise an exception
135
-                throw new RuntimeException( sprintf(
135
+                throw new RuntimeException(sprintf(
136 136
                         "XML error: %s at line %d",
137
-                        xml_error_string( xml_get_error_code( $xmlParser ) ),
138
-                        xml_get_current_line_number( $xmlParser )
139
-                ) );
137
+                        xml_error_string(xml_get_error_code($xmlParser)),
138
+                        xml_get_current_line_number($xmlParser)
139
+                ));
140 140
             }
141 141
             /*
142 142
             * Get the accumulated this->offset in the document:
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
         }
148 148
 
149 149
         // close Sax parser
150
-        $this->closeSaxParser( $xmlParser );
150
+        $this->closeSaxParser($xmlParser);
151 151
 
152 152
     }
153 153
 
@@ -156,24 +156,24 @@  discard block
 block discarded – undo
156 156
      *
157 157
      * @return string
158 158
      */
159
-    protected function getLastCharacter( $parser ): string {
159
+    protected function getLastCharacter($parser): string {
160 160
 
161 161
         //this logic helps detecting empty tags
162 162
         //get current position of SAX pointer in all the stream of data is has read so far:
163 163
         //it points at the end of current tag
164
-        $idx = xml_get_current_byte_index( $parser );
164
+        $idx = xml_get_current_byte_index($parser);
165 165
 
166 166
         //check whether the bounds of current tag are entirely in current buffer or the end of the current tag
167 167
         //is outside current buffer (in the latter case, it's in next buffer to be read by the while loop);
168 168
         //this check is necessary because we may have truncated a tag in half with current read,
169 169
         //and the other half may be encountered in the next buffer it will be passed
170
-        if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) {
170
+        if (isset($this->currentBuffer[$idx - $this->offset])) {
171 171
             //if this tag entire lenght fitted in the buffer, the last char must be the last
172 172
             //symbol before the '>'; if it's an empty tag, it is assumed that it's a '/'
173
-            $lastChar = $this->currentBuffer[ $idx - $this->offset ];
173
+            $lastChar = $this->currentBuffer[$idx - $this->offset];
174 174
         } else {
175 175
             //if it's out, simple use the last character of the chunk
176
-            $lastChar = $this->currentBuffer[ $this->len - 1 ];
176
+            $lastChar = $this->currentBuffer[$this->len - 1];
177 177
         }
178 178
 
179 179
         return $lastChar;
@@ -187,19 +187,19 @@  discard block
 block discarded – undo
187 187
         return "§" .
188 188
                 substr(
189 189
                         str_replace(
190
-                                [ '+', '/' ],
190
+                                ['+', '/'],
191 191
                                 '',
192
-                                base64_encode( openssl_random_pseudo_bytes( 10, $_crypto_strong ) )
192
+                                base64_encode(openssl_random_pseudo_bytes(10, $_crypto_strong))
193 193
                         ),
194 194
                         0,
195 195
                         4
196 196
                 );
197 197
     }
198 198
 
199
-    private function createOutputFileIfDoesNotExist( string $outputFilePath ) {
199
+    private function createOutputFileIfDoesNotExist(string $outputFilePath) {
200 200
         // create output file
201
-        if ( !file_exists( $outputFilePath ) ) {
202
-            touch( $outputFilePath );
201
+        if (!file_exists($outputFilePath)) {
202
+            touch($outputFilePath);
203 203
         }
204 204
     }
205 205
 
@@ -207,13 +207,13 @@  discard block
 block discarded – undo
207 207
      * @param string $originalXliffPath
208 208
      * @param string $outputFilePath
209 209
      */
210
-    private function setFileDescriptors( string $originalXliffPath, string $outputFilePath ) {
211
-        $this->outputFP = fopen( $outputFilePath, 'w+' );
210
+    private function setFileDescriptors(string $originalXliffPath, string $outputFilePath) {
211
+        $this->outputFP = fopen($outputFilePath, 'w+');
212 212
 
213 213
         $streamArgs = null;
214 214
 
215
-        if ( !( $this->originalFP = fopen( $originalXliffPath, "r", false, stream_context_create( $streamArgs ) ) ) ) {
216
-            throw new RuntimeException( "could not open XML input" );
215
+        if (!($this->originalFP = fopen($originalXliffPath, "r", false, stream_context_create($streamArgs)))) {
216
+            throw new RuntimeException("could not open XML input");
217 217
         }
218 218
     }
219 219
 
@@ -223,8 +223,8 @@  discard block
 block discarded – undo
223 223
     public function __destruct() {
224 224
         //this stream can be closed outside the class
225 225
         //to permit multiple concurrent downloads, so suppress warnings
226
-        @fclose( $this->originalFP );
227
-        fclose( $this->outputFP );
226
+        @fclose($this->originalFP);
227
+        fclose($this->outputFP);
228 228
     }
229 229
 
230 230
     /**
@@ -233,11 +233,11 @@  discard block
 block discarded – undo
233 233
      * @return resource
234 234
      */
235 235
     protected function initSaxParser() {
236
-        $xmlSaxParser = xml_parser_create( 'UTF-8' );
237
-        xml_set_object( $xmlSaxParser, $this );
238
-        xml_parser_set_option( $xmlSaxParser, XML_OPTION_CASE_FOLDING, false );
239
-        xml_set_element_handler( $xmlSaxParser, 'tagOpen', 'tagClose' );
240
-        xml_set_character_data_handler( $xmlSaxParser, 'characterData' );
236
+        $xmlSaxParser = xml_parser_create('UTF-8');
237
+        xml_set_object($xmlSaxParser, $this);
238
+        xml_parser_set_option($xmlSaxParser, XML_OPTION_CASE_FOLDING, false);
239
+        xml_set_element_handler($xmlSaxParser, 'tagOpen', 'tagClose');
240
+        xml_set_character_data_handler($xmlSaxParser, 'characterData');
241 241
 
242 242
         return $xmlSaxParser;
243 243
     }
@@ -245,8 +245,8 @@  discard block
 block discarded – undo
245 245
     /**
246 246
      * @param resource $xmlSaxParser
247 247
      */
248
-    protected function closeSaxParser( $xmlSaxParser ) {
249
-        xml_parser_free( $xmlSaxParser );
248
+    protected function closeSaxParser($xmlSaxParser) {
249
+        xml_parser_free($xmlSaxParser);
250 250
     }
251 251
 
252 252
     /**
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
      *
257 257
      * @return mixed
258 258
      */
259
-    abstract protected function tagOpen( $parser, string $name, array $attr );
259
+    abstract protected function tagOpen($parser, string $name, array $attr);
260 260
 
261 261
     /**
262 262
      * @param resource $parser
@@ -264,7 +264,7 @@  discard block
 block discarded – undo
264 264
      *
265 265
      * @return mixed
266 266
      */
267
-    abstract protected function tagClose( $parser, string $name );
267
+    abstract protected function tagClose($parser, string $name);
268 268
 
269 269
     /**
270 270
      * @param resource $parser
@@ -272,11 +272,11 @@  discard block
 block discarded – undo
272 272
      *
273 273
      * @return mixed
274 274
      */
275
-    protected function characterData( $parser, string $data ): void {
275
+    protected function characterData($parser, string $data): void {
276 276
         // don't write <target> data
277
-        if ( !$this->inTarget && !$this->bufferIsActive ) {
278
-            $this->postProcAndFlush( $this->outputFP, $data );
279
-        } elseif ( $this->bufferIsActive ) {
277
+        if (!$this->inTarget && !$this->bufferIsActive) {
278
+            $this->postProcAndFlush($this->outputFP, $data);
279
+        } elseif ($this->bufferIsActive) {
280 280
             $this->CDATABuffer .= $data;
281 281
         }
282 282
     }
@@ -288,19 +288,19 @@  discard block
 block discarded – undo
288 288
      * @param string   $data
289 289
      * @param bool     $treatAsCDATA
290 290
      */
291
-    protected function postProcAndFlush( $fp, string $data, bool $treatAsCDATA = false ) {
291
+    protected function postProcAndFlush($fp, string $data, bool $treatAsCDATA = false) {
292 292
         //postprocess string
293
-        $data = preg_replace( "/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data );
294
-        $data = str_replace( '&nbsp;', ' ', $data );
295
-        if ( !$treatAsCDATA ) {
293
+        $data = preg_replace("/" . self::$INTERNAL_TAG_PLACEHOLDER . '(.*?)' . self::$INTERNAL_TAG_PLACEHOLDER . "/", '&$1;', $data);
294
+        $data = str_replace('&nbsp;', ' ', $data);
295
+        if (!$treatAsCDATA) {
296 296
             //unix2dos
297
-            $data = str_replace( "\r\n", "\r", $data );
298
-            $data = str_replace( "\n", "\r", $data );
299
-            $data = str_replace( "\r", "\r\n", $data );
297
+            $data = str_replace("\r\n", "\r", $data);
298
+            $data = str_replace("\n", "\r", $data);
299
+            $data = str_replace("\r", "\r\n", $data);
300 300
         }
301 301
 
302 302
         //flush to disk
303
-        fwrite( $fp, $data );
303
+        fwrite($fp, $data);
304 304
     }
305 305
 
306 306
     /**
@@ -309,19 +309,19 @@  discard block
 block discarded – undo
309 309
      *
310 310
      * @return void
311 311
      */
312
-    protected function handleOpenUnit( string $name, array $attr ) {
312
+    protected function handleOpenUnit(string $name, array $attr) {
313 313
 
314 314
         // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)
315
-        if ( $this->tuTagName === $name ) {
315
+        if ($this->tuTagName === $name) {
316 316
             $this->inTU = true;
317 317
 
318 318
             // get id
319 319
             // trim to first 100 characters because this is the limit on Matecat's DB
320
-            $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 );
320
+            $this->currentTransUnitId = substr($attr['id'], 0, 100);
321 321
 
322 322
             // `translate` attribute can be only yes or no
323 323
             // current 'translate' attribute of the current trans-unit
324
-            $this->currentTransUnitIsTranslatable = empty( $attr[ 'translate' ] ) ? 'yes' : $attr[ 'translate' ];
324
+            $this->currentTransUnitIsTranslatable = empty($attr['translate']) ? 'yes' : $attr['translate'];
325 325
 
326 326
             $this->setLastTransUnitSegments();
327 327
 
@@ -335,15 +335,15 @@  discard block
 block discarded – undo
335 335
      *
336 336
      * @return string
337 337
      */
338
-    protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string {
338
+    protected function handleOpenXliffTag(string $name, array $attr, string $tag): string {
339 339
 
340 340
         // Add MateCat specific namespace.
341 341
         // Add trgLang
342
-        if ( $name === 'xliff' ) {
343
-            if ( !array_key_exists( 'xmlns:mtc', $attr ) ) {
342
+        if ($name === 'xliff') {
343
+            if (!array_key_exists('xmlns:mtc', $attr)) {
344 344
                 $tag .= ' xmlns:mtc="https://www.matecat.com" ';
345 345
             }
346
-            $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag );
346
+            $tag = preg_replace('/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag);
347 347
         }
348 348
 
349 349
         return $tag;
@@ -355,10 +355,10 @@  discard block
 block discarded – undo
355 355
      *
356 356
      * @return void
357 357
      */
358
-    protected function checkSetInTarget( string $name ) {
358
+    protected function checkSetInTarget(string $name) {
359 359
         // check if we are entering into a <target>
360
-        if ( 'target' === $name ) {
361
-            if ( $this->currentTransUnitIsTranslatable === 'no' ) {
360
+        if ('target' === $name) {
361
+            if ($this->currentTransUnitIsTranslatable === 'no') {
362 362
                 $this->inTarget = false;
363 363
             } else {
364 364
                 $this->inTarget = true;
@@ -371,8 +371,8 @@  discard block
 block discarded – undo
371 371
      *
372 372
      * @return void
373 373
      */
374
-    protected function setInBuffer( string $name ) {
375
-        if ( in_array( $name, $this->nodesToBuffer ) ) {
374
+    protected function setInBuffer(string $name) {
375
+        if (in_array($name, $this->nodesToBuffer)) {
376 376
             $this->bufferIsActive = true;
377 377
         }
378 378
     }
@@ -380,24 +380,24 @@  discard block
 block discarded – undo
380 380
     /**
381 381
      * @param array $seg
382 382
      */
383
-    protected function updateSegmentCounts( array $seg = [] ) {
383
+    protected function updateSegmentCounts(array $seg = []) {
384 384
 
385
-        $raw_word_count = $seg[ 'raw_word_count' ];
386
-        $eq_word_count  = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 );
385
+        $raw_word_count = $seg['raw_word_count'];
386
+        $eq_word_count  = (floor($seg['eq_word_count'] * 100) / 100);
387 387
 
388
-        $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [
388
+        $this->counts['segments_count_array'][$seg['sid']] = [
389 389
                 'raw_word_count' => $raw_word_count,
390 390
                 'eq_word_count'  => $eq_word_count,
391 391
         ];
392 392
 
393
-        $this->counts[ 'raw_word_count' ] += $raw_word_count;
394
-        $this->counts[ 'eq_word_count' ]  += $eq_word_count;
393
+        $this->counts['raw_word_count'] += $raw_word_count;
394
+        $this->counts['eq_word_count']  += $eq_word_count;
395 395
     }
396 396
 
397 397
     protected function resetCounts() {
398
-        $this->counts[ 'segments_count_array' ] = [];
399
-        $this->counts[ 'raw_word_count' ]       = 0;
400
-        $this->counts[ 'eq_word_count' ]        = 0;
398
+        $this->counts['segments_count_array'] = [];
399
+        $this->counts['raw_word_count']       = 0;
400
+        $this->counts['eq_word_count']        = 0;
401 401
     }
402 402
 
403 403
     /**
@@ -406,16 +406,16 @@  discard block
 block discarded – undo
406 406
      *
407 407
      * @return void
408 408
      */
409
-    protected function checkForSelfClosedTagAndFlush( $parser, string $tag ) {
409
+    protected function checkForSelfClosedTagAndFlush($parser, string $tag) {
410 410
 
411
-        $lastChar = $this->getLastCharacter( $parser );
411
+        $lastChar = $this->getLastCharacter($parser);
412 412
 
413 413
         //trim last space
414
-        $tag = rtrim( $tag );
414
+        $tag = rtrim($tag);
415 415
 
416 416
         //detect empty tag
417 417
         $this->isEmpty = $lastChar == '/';
418
-        if ( $this->isEmpty ) {
418
+        if ($this->isEmpty) {
419 419
             $tag .= $lastChar;
420 420
         }
421 421
 
@@ -423,11 +423,11 @@  discard block
 block discarded – undo
423 423
         $tag .= ">";
424 424
 
425 425
         //set a Buffer for the segSource Source tag
426
-        if ( $this->bufferIsActive ) { // we are opening a critical CDATA section
426
+        if ($this->bufferIsActive) { // we are opening a critical CDATA section
427 427
             //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer
428 428
             $this->CDATABuffer .= $tag;
429 429
         } else {
430
-            $this->postProcAndFlush( $this->outputFP, $tag );
430
+            $this->postProcAndFlush($this->outputFP, $tag);
431 431
         }
432 432
 
433 433
     }
@@ -452,18 +452,18 @@  discard block
 block discarded – undo
452 452
          */
453 453
         $this->lastTransUnit = [];
454 454
 
455
-        if ( !isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
455
+        if (!isset($this->transUnits[$this->currentTransUnitId])) {
456 456
             return;
457 457
         }
458 458
 
459
-        $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ];
459
+        $listOfSegmentsIds = $this->transUnits[$this->currentTransUnitId];
460 460
         $last_value        = null;
461
-        $segmentsCount     = count( $listOfSegmentsIds );
462
-        for ( $i = 0; $i < $segmentsCount; $i++ ) {
463
-            $id = $listOfSegmentsIds[ $i ];
464
-            if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) {
465
-                $last_value            = $listOfSegmentsIds[ $i ];
466
-                $this->lastTransUnit[] = $this->segments[ $id ];
461
+        $segmentsCount     = count($listOfSegmentsIds);
462
+        for ($i = 0; $i < $segmentsCount; $i++) {
463
+            $id = $listOfSegmentsIds[$i];
464
+            if (isset($this->segments[$id]) && ($i == 0 || $last_value + 1 == $listOfSegmentsIds[$i])) {
465
+                $last_value            = $listOfSegmentsIds[$i];
466
+                $this->lastTransUnit[] = $this->segments[$id];
467 467
             }
468 468
         }
469 469
 
@@ -473,8 +473,8 @@  discard block
 block discarded – undo
473 473
      * @return array
474 474
      */
475 475
     protected function getCurrentSegment(): array {
476
-        if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) {
477
-            return $this->segments[ $this->segmentInUnitPosition ];
476
+        if ($this->currentTransUnitIsTranslatable !== 'no' && isset($this->transUnits[$this->currentTransUnitId])) {
477
+            return $this->segments[$this->segmentInUnitPosition];
478 478
         }
479 479
 
480 480
         return [];
Please login to merge, or discard this patch.