| Total Complexity | 49 | 
| Total Lines | 324 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like Xliff20 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 Xliff20, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 14 | class Xliff20 extends AbstractXliffReplacer { | 
            ||
| 15 | |||
| 16 | /**  | 
            ||
| 17 | * @var int  | 
            ||
| 18 | */  | 
            ||
| 19 | private int $mdaGroupCounter = 0;  | 
            ||
| 20 | /**  | 
            ||
| 21 | * @var bool  | 
            ||
| 22 | */  | 
            ||
| 23 | protected bool $unitContainsMda = false; // check if <unit> already contains a <mda:metadata> (forXliff v 2.*)  | 
            ||
| 24 | /**  | 
            ||
| 25 | * @var array  | 
            ||
| 26 | */  | 
            ||
| 27 | protected array $nodesToBuffer = [  | 
            ||
| 28 | 'source',  | 
            ||
| 29 | 'mda:metadata',  | 
            ||
| 30 | 'memsource:additionalTagData',  | 
            ||
| 31 | 'originalData',  | 
            ||
| 32 | 'note'  | 
            ||
| 33 | ];  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * @inheritDoc  | 
            ||
| 37 | */  | 
            ||
| 38 |     protected function tagOpen( $parser, string $name, array $attr ) { | 
            ||
| 39 | |||
| 40 | $this->handleOpenUnit( $name, $attr );  | 
            ||
| 41 | |||
| 42 |         if ( 'mda:metadata' === $name ) { | 
            ||
| 43 | $this->unitContainsMda = true;  | 
            ||
| 44 | }  | 
            ||
| 45 | |||
| 46 | $this->checkSetInTarget( $name );  | 
            ||
| 47 | |||
| 48 | // open buffer  | 
            ||
| 49 | $this->setInBuffer( $name );  | 
            ||
| 50 | |||
| 51 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit  | 
            ||
| 52 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction  | 
            ||
| 53 |         if ( !$this->inTarget ) { | 
            ||
| 54 | |||
| 55 | $tag = '';  | 
            ||
| 56 | |||
| 57 | //  | 
            ||
| 58 | // ============================================  | 
            ||
| 59 | // only for Xliff 2.*  | 
            ||
| 60 | // ============================================  | 
            ||
| 61 | //  | 
            ||
| 62 | // In xliff v2 we MUST add <mda:metadata> BEFORE <notes>/<originalData>/<segment>/<ignorable>  | 
            ||
| 63 | //  | 
            ||
| 64 | // As documentation says, <unit> contains:  | 
            ||
| 65 | //  | 
            ||
| 66 | // - elements from other namespaces, OPTIONAL  | 
            ||
| 67 | // - Zero or one <notes> elements followed by  | 
            ||
| 68 | // - Zero or one <originalData> element followed by  | 
            ||
| 69 | // - One or more <segment> or <ignorable> elements in any order.  | 
            ||
| 70 | //  | 
            ||
| 71 | // For more info please refer to:  | 
            ||
| 72 | //  | 
            ||
| 73 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit  | 
            ||
| 74 | //  | 
            ||
| 75 | if (  | 
            ||
| 76 | ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) &&  | 
            ||
| 77 | $this->unitContainsMda === false &&  | 
            ||
| 78 | !empty( $this->transUnits[ $this->currentTransUnitId ] ) &&  | 
            ||
| 79 | !$this->hasWrittenCounts  | 
            ||
| 80 |             ) { | 
            ||
| 81 | // we need to update counts here  | 
            ||
| 82 | $this->updateCounts();  | 
            ||
| 83 | $this->hasWrittenCounts = true;  | 
            ||
| 84 | $tag .= $this->getWordCountGroupForXliffV2();  | 
            ||
| 85 | $this->unitContainsMda = true;  | 
            ||
| 86 | }  | 
            ||
| 87 | |||
| 88 | // construct tag  | 
            ||
| 89 | $tag .= "<$name ";  | 
            ||
| 90 | |||
| 91 |             foreach ( $attr as $k => $v ) { | 
            ||
| 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' ) { | 
            ||
| 94 | $tag .= "$k=\"$v\" ";  | 
            ||
| 95 | }  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | $seg = $this->getCurrentSegment();  | 
            ||
| 99 | |||
| 100 |             if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 101 | |||
| 102 | // add `mtc:segment-id` to xliff v.2*  | 
            ||
| 103 |                 if ( strpos( $tag, 'mtc:segment-id' ) === false ) { | 
            ||
| 104 |                     $tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" "; | 
            ||
| 105 | }  | 
            ||
| 106 | |||
| 107 | }  | 
            ||
| 108 | |||
| 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' ] );  | 
            ||
| 112 | $tag .= $stateProp;  | 
            ||
| 113 | }  | 
            ||
| 114 | |||
| 115 | $tag = $this->handleOpenXliffTag( $name, $attr, $tag );  | 
            ||
| 116 | |||
| 117 | $this->checkForSelfClosedTagAndFlush( $parser, $tag );  | 
            ||
| 118 | |||
| 119 | }  | 
            ||
| 120 | |||
| 121 | }  | 
            ||
| 122 | |||
| 123 | /**  | 
            ||
| 124 | * @param string $name  | 
            ||
| 125 | * @param array $attr  | 
            ||
| 126 | * @param string $tag  | 
            ||
| 127 | *  | 
            ||
| 128 | * @return string  | 
            ||
| 129 | */  | 
            ||
| 130 |     protected function handleOpenXliffTag( string $name, array $attr, string $tag ): string { | 
            ||
| 138 | }  | 
            ||
| 139 | |||
| 140 | /**  | 
            ||
| 141 | * @inheritDoc  | 
            ||
| 142 | */  | 
            ||
| 143 |     protected function tagClose( $parser, string $name ) { | 
            ||
| 144 | $tag = '';  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * if is a tag within <target> or  | 
            ||
| 148 | * if it is an empty tag, do not add closing tag because we have already closed it in  | 
            ||
| 149 | *  | 
            ||
| 150 | * self::tagOpen method  | 
            ||
| 151 | */  | 
            ||
| 152 |         if ( !$this->isEmpty ) { | 
            ||
| 153 | |||
| 154 |             if ( !$this->inTarget ) { | 
            ||
| 155 | $tag = "</$name>";  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 |             if ( 'target' == $name ) { | 
            ||
| 159 | |||
| 160 |                 if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { | 
            ||
| 161 | |||
| 162 | $seg = $this->getCurrentSegment();  | 
            ||
| 163 | |||
| 164 | // update counts  | 
            ||
| 165 |                     if ( !$this->hasWrittenCounts && !empty( $seg ) ) { | 
            ||
| 166 | $this->updateSegmentCounts( $seg );  | 
            ||
| 167 | }  | 
            ||
| 168 | |||
| 169 | // delete translations so the prepareSegment  | 
            ||
| 170 | // will put source content in target tag  | 
            ||
| 171 |                     if ( $this->sourceInTarget ) { | 
            ||
| 172 | $seg[ 'translation' ] = '';  | 
            ||
| 173 | $this->resetCounts();  | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | // append $translation  | 
            ||
| 177 | $translation = $this->prepareTranslation( $seg );  | 
            ||
| 178 | |||
| 179 | //append translation  | 
            ||
| 180 | $tag = "<target>$translation</target>";  | 
            ||
| 181 | |||
| 182 | }  | 
            ||
| 183 | |||
| 184 | // signal we are leaving a target  | 
            ||
| 185 | $this->targetWasWritten = true;  | 
            ||
| 186 | $this->inTarget = false;  | 
            ||
| 187 | $this->postProcAndFlush( $this->outputFP, $tag, true );  | 
            ||
| 188 | |||
| 189 |             } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section | 
            ||
| 190 | |||
| 191 | $this->bufferIsActive = false;  | 
            ||
| 192 | |||
| 193 | // only for Xliff 2.*  | 
            ||
| 194 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit>  | 
            ||
| 195 |                 if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) { | 
            ||
| 196 | |||
| 197 | // we need to update counts here  | 
            ||
| 198 | $this->updateCounts();  | 
            ||
| 199 | $this->hasWrittenCounts = true;  | 
            ||
| 200 | |||
| 201 | $tag = $this->CDATABuffer;  | 
            ||
| 202 | $tag .= $this->getWordCountGroupForXliffV2( false );  | 
            ||
| 203 | $tag .= " </mda:metadata>";  | 
            ||
| 204 | |||
| 205 |                 } else { | 
            ||
| 206 | $tag = $this->CDATABuffer . "</$name>";  | 
            ||
| 207 | }  | 
            ||
| 208 | |||
| 209 | $this->CDATABuffer = "";  | 
            ||
| 210 | |||
| 211 | //flush to the pointer  | 
            ||
| 212 | $this->postProcAndFlush( $this->outputFP, $tag );  | 
            ||
| 213 | |||
| 214 |             } elseif ( 'segment' === $name ) { | 
            ||
| 215 | |||
| 216 | // only for Xliff 2.*  | 
            ||
| 217 | // if segment has no <target> add it BEFORE </segment>  | 
            ||
| 218 |                 if ( !$this->targetWasWritten ) { | 
            ||
| 219 | |||
| 220 | $seg = $this->getCurrentSegment();  | 
            ||
| 221 | |||
| 222 |                     if ( isset( $seg[ 'translation' ] ) ) { | 
            ||
| 223 | |||
| 224 | $translation = $this->prepareTranslation( $seg );  | 
            ||
| 225 | // replace the tag  | 
            ||
| 226 | $tag = "<target>$translation</target>";  | 
            ||
| 227 | |||
| 228 | $tag .= '</segment>';  | 
            ||
| 229 | |||
| 230 | }  | 
            ||
| 231 | |||
| 232 | }  | 
            ||
| 233 | |||
| 234 | // update segmentPositionInTu  | 
            ||
| 235 | $this->segmentInUnitPosition++;  | 
            ||
| 236 | |||
| 237 | $this->postProcAndFlush( $this->outputFP, $tag );  | 
            ||
| 238 | |||
| 239 | // we are leaving <segment>, reset $segmentHasTarget  | 
            ||
| 240 | $this->targetWasWritten = false;  | 
            ||
| 241 | |||
| 242 |             } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag | 
            ||
| 243 | $this->CDATABuffer .= "</$name>";  | 
            ||
| 244 | // Do NOT Flush  | 
            ||
| 245 |             } else { //generic tag closure do Nothing | 
            ||
| 246 | // flush to pointer  | 
            ||
| 247 | $this->postProcAndFlush( $this->outputFP, $tag );  | 
            ||
| 248 | }  | 
            ||
| 249 |         } else { | 
            ||
| 250 | //ok, nothing to be done; reset flag for next coming tag  | 
            ||
| 251 | $this->isEmpty = false;  | 
            ||
| 252 | }  | 
            ||
| 253 | |||
| 254 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*)  | 
            ||
| 255 |         if ( $this->tuTagName === $name ) { | 
            ||
| 256 | $this->currentTransUnitIsTranslatable = null;  | 
            ||
| 257 | $this->inTU = false;  | 
            ||
| 258 | $this->unitContainsMda = false;  | 
            ||
| 259 | $this->hasWrittenCounts = false;  | 
            ||
| 260 | |||
| 261 | $this->resetCounts();  | 
            ||
| 262 | }  | 
            ||
| 263 | }  | 
            ||
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * Update counts  | 
            ||
| 267 | */  | 
            ||
| 268 |     private function updateCounts() { | 
            ||
| 273 | }  | 
            ||
| 274 | |||
| 275 | }  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @param bool $withMetadataTag  | 
            ||
| 279 | *  | 
            ||
| 280 | * @return string  | 
            ||
| 281 | */  | 
            ||
| 282 |     private function getWordCountGroupForXliffV2( bool $withMetadataTag = true ): string { | 
            ||
| 310 | |||
| 311 | }  | 
            ||
| 312 | |||
| 313 | /**  | 
            ||
| 314 | * prepare segment tagging for xliff insertion  | 
            ||
| 315 | *  | 
            ||
| 316 | * @param array $seg  | 
            ||
| 317 | *  | 
            ||
| 318 | * @return string  | 
            ||
| 319 | */  | 
            ||
| 320 |     protected function prepareTranslation( array $seg ): string { | 
            ||
| 341 | }  | 
            
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
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.