Conditions | 37 |
Paths | 108 |
Total Lines | 252 |
Code Lines | 104 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
221 | protected function tagClose( $parser, $name ) { |
||
222 | $tag = ''; |
||
223 | |||
224 | /** |
||
225 | * if is a tag within <target> or |
||
226 | * if it is an empty tag, do not add closing tag because we have already closed it in |
||
227 | * |
||
228 | * self::tagOpen method |
||
229 | */ |
||
230 | if ( !$this->isEmpty ) { |
||
231 | |||
232 | if ( !$this->inTarget ) { |
||
233 | $tag = "</$name>"; |
||
234 | } |
||
235 | |||
236 | if ( 'target' == $name ) { |
||
237 | |||
238 | if ( $this->currentTransUnitIsTranslatable === 'no' ) { |
||
239 | // do nothing |
||
240 | } elseif ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
241 | |||
242 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
||
243 | // actually there may be more that one segment to that ID if there are two mrk of the same source segment |
||
244 | |||
245 | $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
||
246 | |||
247 | // $currentSegmentId |
||
248 | if ( !empty( $listOfSegmentsIds ) ) { |
||
249 | $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
||
250 | } |
||
251 | |||
252 | /* |
||
253 | * At the end of every cycle the segment grouping information is lost: unset( 'matecat|' . $this->currentId ) |
||
254 | * |
||
255 | * We need to take the info about the last segment parsed |
||
256 | * ( normally more than 1 db row because of mrk tags ) |
||
257 | * |
||
258 | * So, copy the current segment data group to an another structure to take the last one segment |
||
259 | * for the next tagOpen ( possible sdl:seg-defs ) |
||
260 | * |
||
261 | */ |
||
262 | |||
263 | $this->lastTransUnit = []; |
||
264 | |||
265 | $last_value = null; |
||
266 | $segmentsCount = count( $listOfSegmentsIds ); |
||
267 | for ( $i = 0; $i < $segmentsCount; $i++ ) { |
||
268 | $id = $listOfSegmentsIds[ $i ]; |
||
269 | if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
||
270 | $last_value = $listOfSegmentsIds[ $i ]; |
||
271 | $this->lastTransUnit[] = $this->segments[ $id ]; |
||
272 | } |
||
273 | } |
||
274 | |||
275 | // init translation and state |
||
276 | $translation = ''; |
||
277 | $lastMrkState = null; |
||
278 | $stateProp = ''; |
||
279 | |||
280 | // we must reset the lastMrkId found because this is a new segment. |
||
281 | $lastMrkId = -1; |
||
282 | |||
283 | if ( $this->xliffVersion === 2 ) { |
||
284 | $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
||
285 | |||
286 | // update counts |
||
287 | if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
||
288 | $this->updateSegmentCounts( $seg ); |
||
289 | } |
||
290 | |||
291 | // delete translations so the prepareSegment |
||
292 | // will put source content in target tag |
||
293 | if ( $this->sourceInTarget ) { |
||
294 | $seg[ 'translation' ] = ''; |
||
295 | $this->resetCounts(); |
||
296 | } |
||
297 | |||
298 | // append $translation |
||
299 | $translation = $this->prepareTranslation( $seg, $translation ); |
||
300 | |||
301 | [ $stateProp, ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion ); |
||
302 | |||
303 | } else { |
||
304 | foreach ( $listOfSegmentsIds as $pos => $id ) { |
||
305 | |||
306 | /* |
||
307 | * This routine works to respect the positional orders of markers. |
||
308 | * In every cycle we check if the mrk of the segment is below or equal the last one. |
||
309 | * When this is true, means that the mrk id belongs to the next segment with the same internal_id |
||
310 | * so we MUST stop to apply markers and translations |
||
311 | * and stop to add eq_word_count |
||
312 | * |
||
313 | * Begin: |
||
314 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
||
315 | * If it is null leave it NULL |
||
316 | */ |
||
317 | if ( (int)$this->segments[ $id ][ "mrk_id" ] < 0 && $this->segments[ $id ][ "mrk_id" ] !== null ) { |
||
318 | $this->segments[ $id ][ "mrk_id" ] = 0; |
||
319 | } |
||
320 | |||
321 | /* |
||
322 | * WARNING: |
||
323 | * For those seg-source that doesn't have a mrk ( having a mrk id === null ) |
||
324 | * ( null <= -1 ) === true |
||
325 | * so, cast to int |
||
326 | */ |
||
327 | if ( (int)$this->segments[ $id ][ "mrk_id" ] <= $lastMrkId ) { |
||
328 | break; |
||
329 | } |
||
330 | |||
331 | // set $this->currentSegment |
||
332 | $seg = $this->segments[ $id ]; |
||
333 | |||
334 | // update counts |
||
335 | if ( !empty( $seg ) ) { |
||
336 | $this->updateSegmentCounts( $seg ); |
||
337 | } |
||
338 | |||
339 | // delete translations so the prepareSegment |
||
340 | // will put source content in target tag |
||
341 | if ( $this->sourceInTarget ) { |
||
342 | $seg[ 'translation' ] = ''; |
||
343 | $this->resetCounts(); |
||
344 | } |
||
345 | |||
346 | // append $translation |
||
347 | $translation = $this->prepareTranslation( $seg, $translation ); |
||
348 | |||
349 | // for xliff 2 we need $this->transUnits[ $this->currentId ] [ $pos ] for populating metadata |
||
350 | |||
351 | unset( $this->transUnits[ $this->currentTransUnitId ] [ $pos ] ); |
||
352 | |||
353 | $lastMrkId = $this->segments[ $id ][ "mrk_id" ]; |
||
354 | |||
355 | [ $stateProp, $lastMrkState ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion, $stateProp, $lastMrkState ); |
||
356 | |||
357 | } |
||
358 | } |
||
359 | |||
360 | //append translation |
||
361 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
362 | |||
363 | } |
||
364 | |||
365 | // signal we are leaving a target |
||
366 | $this->targetWasWritten = true; |
||
367 | $this->inTarget = false; |
||
368 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
||
369 | } elseif ( in_array( $name, $this->nodesToCopy ) && !$this->inTarget ) { // we are closing a critical CDATA section |
||
370 | |||
371 | $this->bufferIsActive = false; |
||
372 | |||
373 | // only for Xliff 2.* |
||
374 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
||
375 | if ( 'mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts ) { |
||
376 | |||
377 | // we need to update counts here |
||
378 | $this->updateCounts(); |
||
379 | $this->hasWrittenCounts = true; |
||
380 | |||
381 | $tag = $this->CDATABuffer; |
||
382 | $tag .= $this->getWordCountGroupForXliffV2( false ); |
||
383 | $tag .= " </mda:metadata>"; |
||
384 | |||
385 | } else { |
||
386 | $tag = $this->CDATABuffer . "</$name>"; |
||
387 | } |
||
388 | |||
389 | $this->CDATABuffer = ""; |
||
390 | |||
391 | //flush to pointer |
||
392 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
393 | } elseif ( 'segment' === $name ) { |
||
394 | |||
395 | // only for Xliff 2.* |
||
396 | // if segment has no <target> add it BEFORE </segment> |
||
397 | if ( !$this->targetWasWritten ) { |
||
398 | |||
399 | $seg = $this->getCurrentSegment(); |
||
400 | |||
401 | if ( isset( $seg[ 'translation' ] ) ) { |
||
402 | |||
403 | $translation = $this->prepareTranslation( $seg ); |
||
404 | [ $stateProp, ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion ); |
||
405 | |||
406 | // replace the tag |
||
407 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
408 | |||
409 | $tag .= '</segment>'; |
||
410 | |||
411 | } |
||
412 | |||
413 | } |
||
414 | |||
415 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
416 | |||
417 | // we are leaving <segment>, reset $segmentHasTarget |
||
418 | $this->targetWasWritten = false; |
||
419 | |||
420 | } elseif ( $name === 'trans-unit' ) { |
||
421 | |||
422 | // only for Xliff 1.* |
||
423 | // handling </trans-unit> closure |
||
424 | if ( !$this->targetWasWritten ) { |
||
425 | |||
426 | $seg = $this->getCurrentSegment(); |
||
427 | |||
428 | if ( isset( $seg[ 'translation' ] ) ) { |
||
429 | $translation = $this->prepareTranslation( $seg ); |
||
430 | [ $stateProp, ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion ); |
||
431 | |||
432 | // replace the tag |
||
433 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
434 | $tag .= '</trans-unit>'; |
||
435 | |||
436 | } |
||
437 | |||
438 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
439 | |||
440 | } else { |
||
441 | $this->postProcAndFlush( $this->outputFP, '</trans-unit>' ); |
||
442 | $this->targetWasWritten = false; |
||
443 | } |
||
444 | |||
445 | |||
446 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
447 | $this->CDATABuffer .= "</$name>"; |
||
448 | // Do NOT Flush |
||
449 | } else { //generic tag closure do Nothing |
||
450 | // flush to pointer |
||
451 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
452 | } |
||
453 | } elseif ( $this->CDATABuffer === '<note/>' && $this->bufferIsActive === true ) { |
||
454 | $this->postProcAndFlush( $this->outputFP, '<note/>' ); |
||
455 | $this->bufferIsActive = false; |
||
456 | $this->CDATABuffer = ''; |
||
457 | $this->isEmpty = false; |
||
458 | } else { |
||
459 | //ok, nothing to be done; reset flag for next coming tag |
||
460 | $this->isEmpty = false; |
||
461 | } |
||
462 | |||
463 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
464 | if ( $this->tuTagName === $name ) { |
||
465 | $this->currentTransUnitIsTranslatable = null; |
||
466 | $this->inTU = false; |
||
467 | $this->segmentPositionInTu = -1; |
||
468 | $this->unitContainsMda = false; |
||
469 | $this->hasWrittenCounts = false; |
||
470 | $this->sourceAttributes = []; |
||
471 | |||
472 | $this->resetCounts(); |
||
473 | } |
||
696 |