Total Complexity | 170 |
Total Lines | 850 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like XliffSAXTranslationReplacer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use XliffSAXTranslationReplacer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class XliffSAXTranslationReplacer extends AbstractXliffReplacer { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | private $mdaGroupCounter = 0; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $nodesToCopy = [ |
||
19 | 'source', |
||
20 | 'mda:metadata', |
||
21 | 'memsource:additionalTagData', |
||
22 | 'originalData', |
||
23 | 'seg-source', |
||
24 | 'value', |
||
25 | 'bpt', |
||
26 | 'ept', |
||
27 | 'ph', |
||
28 | 'st', |
||
29 | 'note', |
||
30 | 'context', |
||
31 | 'context-group' |
||
32 | ]; |
||
33 | |||
34 | public function replaceTranslation() { |
||
97 | |||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @inheritDoc |
||
102 | */ |
||
103 | protected function tagOpen( $parser, $name, $attr ) { |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | protected function tagClose( $parser, $name ) { |
||
306 | $tag = ''; |
||
307 | |||
308 | /** |
||
309 | * if is a tag within <target> or |
||
310 | * if it is an empty tag, do not add closing tag because we have already closed it in |
||
311 | * |
||
312 | * self::tagOpen method |
||
313 | */ |
||
314 | if ( !$this->isEmpty && !( $this->inTarget && $name !== 'target' ) ) { |
||
315 | |||
316 | if ( !$this->inTarget ) { |
||
317 | $tag = "</$name>"; |
||
318 | } |
||
319 | |||
320 | if ( 'target' == $name ) { |
||
321 | |||
322 | if ( $this->currentTransUnitTranslate === 'no' ) { |
||
323 | // do nothing |
||
324 | } elseif ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
325 | |||
326 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
||
327 | // actually there may be more that one segment to that ID if there are two mrk of the same source segment |
||
328 | |||
329 | $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
||
330 | |||
331 | // $currentSegmentId |
||
332 | if ( !empty( $listOfSegmentsIds ) ) { |
||
333 | $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
||
334 | } |
||
335 | |||
336 | /* |
||
337 | * At the end of every cycle the segment grouping information is lost: unset( 'matecat|' . $this->currentId ) |
||
338 | * |
||
339 | * We need to take the info about the last segment parsed |
||
340 | * ( normally more than 1 db row because of mrk tags ) |
||
341 | * |
||
342 | * So, copy the current segment data group to an another structure to take the last one segment |
||
343 | * for the next tagOpen ( possible sdl:seg-defs ) |
||
344 | * |
||
345 | */ |
||
346 | |||
347 | $this->lastTransUnit = []; |
||
348 | |||
349 | $last_value = null; |
||
350 | $segmentsCount = count( $listOfSegmentsIds ); |
||
351 | for ( $i = 0; $i < $segmentsCount; $i++ ) { |
||
352 | $id = $listOfSegmentsIds[ $i ]; |
||
353 | if ( isset( $this->segments[ $id ] ) && ( $i == 0 || $last_value + 1 == $listOfSegmentsIds[ $i ] ) ) { |
||
354 | $last_value = $listOfSegmentsIds[ $i ]; |
||
355 | $this->lastTransUnit[] = $this->segments[ $id ]; |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // init translation and state |
||
360 | $translation = ''; |
||
361 | $lastMrkState = null; |
||
362 | $stateProp = ''; |
||
363 | |||
364 | // we must reset the lastMrkId found because this is a new segment. |
||
365 | $lastMrkId = -1; |
||
366 | |||
367 | if ( $this->xliffVersion === 2 ) { |
||
368 | $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
||
369 | |||
370 | // update counts |
||
371 | if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
||
372 | $this->updateSegmentCounts( $seg ); |
||
373 | } |
||
374 | |||
375 | // delete translations so the prepareSegment |
||
376 | // will put source content in target tag |
||
377 | if ( $this->sourceInTarget ) { |
||
378 | $seg[ 'translation' ] = ''; |
||
379 | $this->resetCounts(); |
||
380 | } |
||
381 | |||
382 | // append $translation |
||
383 | $translation = $this->prepareTranslation( $seg, $translation ); |
||
384 | |||
385 | [ $stateProp, ] = $this->setTransUnitState( $seg, $stateProp, null ); |
||
386 | } else { |
||
387 | foreach ( $listOfSegmentsIds as $pos => $id ) { |
||
388 | |||
389 | /* |
||
390 | * This routine works to respect the positional orders of markers. |
||
391 | * In every cycle we check if the mrk of the segment is below or equal the last one. |
||
392 | * When this is true, means that the mrk id belongs to the next segment with the same internal_id |
||
393 | * so we MUST stop to apply markers and translations |
||
394 | * and stop to add eq_word_count |
||
395 | * |
||
396 | * Begin: |
||
397 | * pre-assign zero to the new mrk if this is the first one ( in this segment ) |
||
398 | * If it is null leave it NULL |
||
399 | */ |
||
400 | if ( (int)$this->segments[ $id ][ "mrk_id" ] < 0 && $this->segments[ $id ][ "mrk_id" ] !== null ) { |
||
401 | $this->segments[ $id ][ "mrk_id" ] = 0; |
||
402 | } |
||
403 | |||
404 | /* |
||
405 | * WARNING: |
||
406 | * For those seg-source that doesn't have a mrk ( having a mrk id === null ) |
||
407 | * ( null <= -1 ) === true |
||
408 | * so, cast to int |
||
409 | */ |
||
410 | if ( (int)$this->segments[ $id ][ "mrk_id" ] <= $lastMrkId ) { |
||
411 | break; |
||
412 | } |
||
413 | |||
414 | // set $this->currentSegment |
||
415 | $seg = $this->segments[ $id ]; |
||
416 | |||
417 | // update counts |
||
418 | if ( !empty( $seg ) ) { |
||
419 | $this->updateSegmentCounts( $seg ); |
||
420 | } |
||
421 | |||
422 | // delete translations so the prepareSegment |
||
423 | // will put source content in target tag |
||
424 | if ( $this->sourceInTarget ) { |
||
425 | $seg[ 'translation' ] = ''; |
||
426 | $this->resetCounts(); |
||
427 | } |
||
428 | |||
429 | // append $translation |
||
430 | $translation = $this->prepareTranslation( $seg, $translation ); |
||
431 | |||
432 | // for xliff 2 we need $this->transUnits[ $this->currentId ] [ $pos ] for populating metadata |
||
433 | |||
434 | unset( $this->transUnits[ $this->currentTransUnitId ] [ $pos ] ); |
||
435 | |||
436 | $lastMrkId = $this->segments[ $id ][ "mrk_id" ]; |
||
437 | |||
438 | [ $stateProp, $lastMrkState ] = $this->setTransUnitState( $seg, $stateProp, $lastMrkState ); |
||
439 | } |
||
440 | } |
||
441 | |||
442 | //append translation |
||
443 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
444 | |||
445 | } |
||
446 | |||
447 | // signal we are leaving a target |
||
448 | $this->targetWasWritten = true; |
||
449 | $this->inTarget = false; |
||
450 | $this->postProcAndFlush( $this->outputFP, $tag, $treatAsCDATA = true ); |
||
451 | } elseif ( in_array( $name, $this->nodesToCopy ) ) { // we are closing a critical CDATA section |
||
452 | |||
453 | $this->bufferIsActive = false; |
||
454 | |||
455 | // only for Xliff 2.* |
||
456 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
||
457 | if ( 'mda:metadata' === $name && $this->unitContainsMda && $this->xliffVersion === 2 && !$this->hasWrittenCounts ) { |
||
458 | |||
459 | // we need to update counts here |
||
460 | $this->updateCounts(); |
||
461 | $this->hasWrittenCounts = true; |
||
462 | |||
463 | $tag = $this->CDATABuffer; |
||
464 | $tag .= $this->getWordCountGroupForXliffV2( false ); |
||
465 | $tag .= " </mda:metadata>"; |
||
466 | |||
467 | } else { |
||
468 | $tag = $this->CDATABuffer . "</$name>"; |
||
469 | } |
||
470 | |||
471 | $this->CDATABuffer = ""; |
||
472 | |||
473 | //flush to pointer |
||
474 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
475 | } elseif ( 'segment' === $name ) { |
||
476 | |||
477 | // only for Xliff 2.* |
||
478 | // if segment has no <target> add it BEFORE </segment> |
||
479 | if ( $this->xliffVersion === 2 && !$this->targetWasWritten ) { |
||
480 | |||
481 | $seg = $this->getCurrentSegment(); |
||
482 | |||
483 | if ( isset( $seg[ 'translation' ] ) ) { |
||
484 | |||
485 | $translation = $this->prepareTranslation( $seg ); |
||
486 | [ $stateProp, ] = $this->setTransUnitState( $seg, '', null ); |
||
487 | |||
488 | // replace the tag |
||
489 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
490 | |||
491 | $tag .= '</segment>'; |
||
492 | |||
493 | } |
||
494 | |||
495 | } |
||
496 | |||
497 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
498 | |||
499 | // we are leaving <segment>, reset $segmentHasTarget |
||
500 | $this->targetWasWritten = false; |
||
501 | |||
502 | } elseif ( $name === 'trans-unit' ) { |
||
503 | |||
504 | // only for Xliff 1.* |
||
505 | // handling </trans-unit> closure |
||
506 | if ( !$this->targetWasWritten ) { |
||
507 | |||
508 | $seg = $this->getCurrentSegment(); |
||
509 | |||
510 | if ( isset( $seg[ 'translation' ] ) ) { |
||
511 | $translation = $this->prepareTranslation( $seg ); |
||
512 | [ $stateProp, ] = $this->setTransUnitState( $seg, '', null ); |
||
513 | |||
514 | // replace the tag |
||
515 | $tag = $this->createTargetTag( $translation, $stateProp ); |
||
516 | $tag .= '</trans-unit>'; |
||
517 | |||
518 | } |
||
519 | |||
520 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
521 | |||
522 | } else { |
||
523 | $this->postProcAndFlush( $this->outputFP, '</trans-unit>' ); |
||
524 | $this->targetWasWritten = false; |
||
525 | } |
||
526 | |||
527 | |||
528 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
529 | $this->CDATABuffer .= "</$name>"; |
||
530 | // Do NOT Flush |
||
531 | } else { //generic tag closure do Nothing |
||
532 | // flush to pointer |
||
533 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
534 | } |
||
535 | } elseif ( $this->CDATABuffer === '<note/>' && $this->bufferIsActive === true ) { |
||
536 | $this->postProcAndFlush( $this->outputFP, '<note/>' ); |
||
537 | $this->bufferIsActive = false; |
||
538 | $this->CDATABuffer = ''; |
||
539 | $this->isEmpty = false; |
||
540 | } else { |
||
541 | //ok, nothing to be done; reset flag for next coming tag |
||
542 | $this->isEmpty = false; |
||
543 | } |
||
544 | |||
545 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
546 | if ( $this->tuTagName === $name ) { |
||
547 | $this->currentTransUnitTranslate = null; |
||
548 | $this->inTU = false; |
||
549 | $this->segmentPositionInTu = -1; |
||
550 | $this->unitContainsMda = false; |
||
551 | $this->hasWrittenCounts = false; |
||
552 | $this->sourceAttributes = []; |
||
553 | |||
554 | $this->resetCounts(); |
||
555 | } |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Set the current segment array (with segment id and trans-unit id) |
||
560 | * |
||
561 | * @param array $listOfSegmentsIds |
||
562 | */ |
||
563 | private function setCurrentSegmentArray( array $listOfSegmentsIds = [] ) { |
||
579 | ]; |
||
580 | } |
||
581 | } |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * Update counts |
||
586 | */ |
||
587 | private function updateCounts() { |
||
588 | // populate counts |
||
589 | $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
||
590 | |||
591 | // $currentSegmentId |
||
592 | if ( !empty( $listOfSegmentsIds ) ) { |
||
593 | $this->setCurrentSegmentArray( $listOfSegmentsIds ); |
||
594 | } |
||
595 | |||
596 | if ( $this->xliffVersion === 2 ) { |
||
597 | $seg = $this->segments[ $this->currentSegmentArray[ 'sid' ] ]; |
||
598 | if ( !empty( $seg ) ) { |
||
599 | $this->updateSegmentCounts( $seg ); |
||
600 | } |
||
601 | } else { |
||
602 | foreach ( $listOfSegmentsIds as $pos => $id ) { |
||
603 | $seg = $this->segments[ $id ]; |
||
604 | if ( !empty( $seg ) ) { |
||
605 | $this->updateSegmentCounts( $seg ); |
||
606 | } |
||
607 | } |
||
608 | } |
||
609 | |||
610 | $this->currentSegmentArray = []; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * @param array $seg |
||
615 | */ |
||
616 | private function updateSegmentCounts( array $seg = [] ) { |
||
617 | |||
618 | $raw_word_count = $seg[ 'raw_word_count' ]; |
||
619 | $eq_word_count = ( floor( $seg[ 'eq_word_count' ] * 100 ) / 100 ); |
||
620 | |||
621 | |||
622 | $listOfSegmentsIds = $this->transUnits[ $this->currentTransUnitId ]; |
||
623 | |||
624 | $this->counts[ 'segments_count_array' ][ $seg[ 'sid' ] ] = [ |
||
625 | 'raw_word_count' => $raw_word_count, |
||
626 | 'eq_word_count' => $eq_word_count, |
||
627 | ]; |
||
628 | |||
629 | $this->counts[ 'raw_word_count' ] += $raw_word_count; |
||
630 | $this->counts[ 'eq_word_count' ] += $eq_word_count; |
||
631 | } |
||
632 | |||
633 | private function resetCounts() { |
||
634 | $this->counts[ 'segments_count_array' ] = []; |
||
635 | $this->counts[ 'raw_word_count' ] = 0; |
||
636 | $this->counts[ 'eq_word_count' ] = 0; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * prepare segment tagging for xliff insertion |
||
641 | * |
||
642 | * @param array $seg |
||
643 | * @param string $transUnitTranslation |
||
644 | * |
||
645 | * @return string |
||
646 | */ |
||
647 | protected function prepareTranslation( $seg, $transUnitTranslation = "" ) { |
||
648 | $endTags = ""; |
||
649 | |||
650 | $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
||
651 | $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
||
652 | $dataRefMap = ( isset( $seg[ 'data_ref_map' ] ) ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : []; |
||
653 | |||
654 | if ( $seg [ 'translation' ] == '' ) { |
||
655 | $translation = $segment; |
||
656 | } else { |
||
657 | if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
||
658 | $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
||
659 | if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) { |
||
660 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
||
661 | } |
||
662 | } |
||
663 | } |
||
664 | |||
665 | // for xliff v2 we ignore the marks on purpose |
||
666 | if ( $this->xliffVersion === 2 ) { |
||
667 | return $translation; |
||
668 | } |
||
669 | |||
670 | if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
||
671 | if ( $this->targetLang === 'ja-JP' ) { |
||
672 | $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] ); |
||
673 | } |
||
674 | |||
675 | $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
||
676 | } |
||
677 | |||
678 | $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . $seg[ 'succ_tags' ]; |
||
679 | |||
680 | return $transUnitTranslation; |
||
681 | } |
||
682 | |||
683 | |||
684 | /** |
||
685 | * @param $raw_word_count |
||
686 | * @param $eq_word_count |
||
687 | * |
||
688 | * @return string |
||
689 | */ |
||
690 | private function getWordCountGroup( $raw_word_count, $eq_word_count ) { |
||
691 | return "\n<count-group name=\"$this->currentTransUnitId\"><count count-type=\"x-matecat-raw\">$raw_word_count</count><count count-type=\"x-matecat-weighted\">$eq_word_count</count></count-group>"; |
||
692 | } |
||
693 | |||
694 | /** |
||
695 | * @return array |
||
696 | */ |
||
697 | private function getCurrentSegment() { |
||
698 | if ( $this->currentTransUnitTranslate === 'yes' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
699 | $index = $this->transUnits[ $this->currentTransUnitId ][ $this->segmentPositionInTu ]; |
||
700 | |||
701 | if ( isset( $this->segments[ $index ] ) ) { |
||
702 | return $this->segments[ $index ]; |
||
703 | } |
||
704 | } |
||
705 | |||
706 | return []; |
||
707 | } |
||
708 | |||
709 | /** |
||
710 | * This function creates a <target> |
||
711 | * |
||
712 | * @param $translation |
||
713 | * @param $stateProp |
||
714 | * |
||
715 | * @return string |
||
716 | */ |
||
717 | private function createTargetTag( $translation, $stateProp ) { |
||
718 | |||
719 | $targetLang = ''; |
||
720 | if ( $this->xliffVersion === 1 ) { |
||
721 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
||
722 | } |
||
723 | |||
724 | switch ( $this->xliffVersion ) { |
||
725 | case 1: |
||
726 | default: |
||
727 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
||
728 | |||
729 | // if it's a Trados file don't append count group |
||
730 | if ( get_class( $this ) !== SdlXliffSAXTranslationReplacer::class ) { |
||
731 | $tag .= $this->getWordCountGroup( $this->counts[ 'raw_word_count' ], $this->counts[ 'eq_word_count' ] ); |
||
732 | } |
||
733 | |||
734 | return $tag; |
||
735 | |||
736 | case 2: |
||
737 | return "<target>$translation</target>"; |
||
738 | } |
||
739 | |||
740 | } |
||
741 | |||
742 | /** |
||
743 | * @param bool $withMetadataTag |
||
744 | * |
||
745 | * @return string |
||
746 | */ |
||
747 | private function getWordCountGroupForXliffV2( $withMetadataTag = true ) { |
||
778 | |||
779 | } |
||
780 | |||
781 | /** |
||
782 | * @param $seg |
||
783 | * @param $state_prop |
||
784 | * @param $lastMrkState |
||
785 | * |
||
786 | * @return array |
||
787 | */ |
||
788 | private function setTransUnitState( $seg, $state_prop, $lastMrkState ) { |
||
789 | switch ( $seg[ 'status' ] ) { |
||
790 | |||
791 | case TranslationStatus::STATUS_FIXED: |
||
792 | case TranslationStatus::STATUS_APPROVED2: |
||
793 | if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED2 ) { |
||
794 | $state_prop = "state=\"final\""; |
||
795 | $lastMrkState = TranslationStatus::STATUS_APPROVED2; |
||
796 | } |
||
797 | break; |
||
798 | case TranslationStatus::STATUS_APPROVED: |
||
799 | if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
||
800 | $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\""; |
||
801 | $lastMrkState = TranslationStatus::STATUS_APPROVED; |
||
802 | } |
||
803 | break; |
||
804 | |||
805 | case TranslationStatus::STATUS_TRANSLATED: |
||
806 | if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED ) { |
||
807 | $state_prop = "state=\"translated\""; |
||
808 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
||
809 | } |
||
810 | break; |
||
811 | |||
812 | case TranslationStatus::STATUS_REJECTED: // if there is a mark REJECTED and there is not a DRAFT, all the trans-unit is REJECTED. In V2 there is no way to mark |
||
813 | case TranslationStatus::STATUS_REBUTTED: |
||
814 | if ( ( $lastMrkState == null ) || ( $lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT ) ) { |
||
815 | $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\""; |
||
816 | $lastMrkState = TranslationStatus::STATUS_REJECTED; |
||
817 | } |
||
818 | break; |
||
819 | |||
820 | case TranslationStatus::STATUS_NEW: |
||
821 | if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_NEW ) { |
||
822 | $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\""; |
||
823 | $lastMrkState = TranslationStatus::STATUS_NEW; |
||
824 | } |
||
825 | break; |
||
826 | |||
827 | case TranslationStatus::STATUS_DRAFT: |
||
828 | if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) { |
||
829 | $state_prop = ( $this->xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\""; |
||
830 | $lastMrkState = TranslationStatus::STATUS_DRAFT; |
||
831 | } |
||
832 | break; |
||
833 | |||
834 | default: |
||
835 | // this is the case when a segment is not showed in cattool, so the row in |
||
836 | // segment_translations does not exists and |
||
837 | // ---> $seg[ 'status' ] is NULL |
||
838 | if ( $lastMrkState == null ) { //this is the first MRK ID |
||
839 | $state_prop = "state=\"translated\""; |
||
840 | $lastMrkState = TranslationStatus::STATUS_TRANSLATED; |
||
841 | } else { |
||
842 | /* Do nothing and preserve the last state */ |
||
843 | } |
||
844 | break; |
||
845 | } |
||
846 | |||
847 | return [ $state_prop, $lastMrkState ]; |
||
848 | } |
||
849 | |||
850 | /** |
||
851 | * @inheritDoc |
||
852 | */ |
||
853 | protected function characterData( $parser, $data ) { |
||
859 | } |
||
860 | } |
||
861 | } |
||
862 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.