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