| Total Complexity | 43 |
| Total Lines | 305 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Xliff12 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 Xliff12, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Xliff12 extends AbstractXliffReplacer { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected array $nodesToBuffer = [ |
||
| 20 | 'source', |
||
| 21 | 'seg-source', |
||
| 22 | 'note', |
||
| 23 | 'context-group' |
||
| 24 | ]; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected string $tuTagName = 'trans-unit'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected string $alternativeMatchesTag = 'alt-trans'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected string $namespace = "mtc"; // Custom namespace |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @inheritDoc |
||
| 43 | */ |
||
| 44 | protected function tagOpen( $parser, string $name, array $attr ) { |
||
| 91 | |||
| 92 | } |
||
| 93 | |||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @inheritDoc |
||
| 99 | */ |
||
| 100 | protected function tagClose( $parser, string $name ) { |
||
| 101 | $tag = ''; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * if is a tag within <target> or |
||
| 105 | * if it is an empty tag, do not add closing tag because we have already closed it in |
||
| 106 | * |
||
| 107 | * self::tagOpen method |
||
| 108 | */ |
||
| 109 | if ( !$this->isEmpty ) { |
||
| 110 | |||
| 111 | if ( !$this->inTarget ) { |
||
| 112 | $tag = "</$name>"; |
||
| 113 | } |
||
| 114 | |||
| 115 | if ( 'target' == $name && !$this->inAltTrans ) { |
||
| 116 | |||
| 117 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
| 118 | |||
| 119 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
||
| 120 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
||
| 121 | $tag = $this->rebuildTarget(); |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 | $this->targetWasWritten = true; |
||
| 126 | // signal we are leaving a target |
||
| 127 | $this->inTarget = false; |
||
| 128 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
||
| 129 | |||
| 130 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
||
| 131 | |||
| 132 | $this->bufferIsActive = false; |
||
| 133 | $tag = $this->CDATABuffer . "</$name>"; |
||
| 134 | $this->CDATABuffer = ""; |
||
| 135 | |||
| 136 | //flush to the pointer |
||
| 137 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 138 | |||
| 139 | } elseif ( $name === $this->tuTagName ) { |
||
| 140 | |||
| 141 | $tag = ""; |
||
| 142 | |||
| 143 | // handling </trans-unit> closure |
||
| 144 | if ( !$this->targetWasWritten ) { |
||
| 145 | |||
| 146 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
| 147 | $tag = $this->rebuildTarget(); |
||
| 148 | } else { |
||
| 149 | $tag = $this->createTargetTag( "", "" ); |
||
| 150 | } |
||
| 151 | |||
| 152 | } |
||
| 153 | |||
| 154 | $tag .= "</$this->tuTagName>"; |
||
| 155 | $this->targetWasWritten = false; |
||
| 156 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 157 | |||
| 158 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
| 159 | $this->CDATABuffer .= "</$name>"; |
||
| 160 | // Do NOT Flush |
||
| 161 | } else { //generic tag closure do Nothing |
||
| 162 | // flush to pointer |
||
| 163 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 164 | } |
||
| 165 | |||
| 166 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
||
| 167 | |||
| 168 | $this->isEmpty = false; |
||
| 169 | $this->bufferIsActive = false; |
||
| 170 | $tag = $this->CDATABuffer; |
||
| 171 | $this->CDATABuffer = ""; |
||
| 172 | |||
| 173 | //flush to the pointer |
||
| 174 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 175 | |||
| 176 | } else { |
||
| 177 | //ok, nothing to be done; reset flag for next coming tag |
||
| 178 | $this->isEmpty = false; |
||
| 179 | } |
||
| 180 | |||
| 181 | // try to signal that we are leaving a target |
||
| 182 | $this->tryUnsetAltTrans( $name ); |
||
| 183 | |||
| 184 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
| 185 | if ( $this->tuTagName === $name ) { |
||
| 186 | $this->currentTransUnitIsTranslatable = null; |
||
| 187 | $this->inTU = false; |
||
| 188 | $this->hasWrittenCounts = false; |
||
| 189 | |||
| 190 | $this->resetCounts(); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * prepare segment tagging for xliff insertion |
||
| 196 | * |
||
| 197 | * @param array $seg |
||
| 198 | * @param string $transUnitTranslation |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
||
| 203 | |||
| 204 | $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
||
| 205 | $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
||
| 206 | |||
| 207 | if ( $seg [ 'translation' ] == '' ) { |
||
| 208 | $translation = $segment; |
||
| 209 | } else { |
||
| 210 | if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
||
| 211 | $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
||
| 212 | if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
||
| 213 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
||
| 219 | |||
| 220 | return $transUnitTranslation; |
||
| 221 | } |
||
| 222 | |||
| 223 | protected function rebuildMarks( array $seg, string $translation ): string { |
||
| 224 | |||
| 225 | if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
||
| 226 | $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
||
| 227 | } |
||
| 228 | |||
| 229 | return $translation; |
||
| 230 | |||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * This function creates a <target> |
||
| 235 | * |
||
| 236 | * @param string $translation |
||
| 237 | * @param string $stateProp |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | private function createTargetTag( string $translation, string $stateProp ): string { |
||
| 242 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
||
| 243 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
||
| 244 | $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>'; |
||
| 245 | |||
| 246 | return $tag; |
||
| 247 | |||
| 248 | } |
||
| 249 | |||
| 250 | protected function rebuildTarget(): string { |
||
| 310 | |||
| 311 | } |
||
| 312 | |||
| 313 | protected function getCurrentSegment(): array { |
||
| 314 | if ( $this->currentTransUnitIsTranslatable !== 'no' && isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
| 321 | } |