| Total Complexity | 48 |
| Total Lines | 318 |
| 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 ) { |
||
| 45 | |||
| 46 | $this->handleOpenUnit( $name, $attr ); |
||
| 47 | |||
| 48 | $this->trySetAltTrans( $name );; |
||
| 49 | $this->checkSetInTarget( $name ); |
||
| 50 | |||
| 51 | // open buffer |
||
| 52 | $this->setInBuffer( $name ); |
||
| 53 | |||
| 54 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
||
| 55 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
||
| 56 | if ( !$this->inTarget ) { |
||
| 57 | |||
| 58 | // We need bufferIsActive for not target nodes with currentTransUnitIsTranslatable = 'NO' |
||
| 59 | if($name === 'target' and $this->currentTransUnitIsTranslatable === 'no'){ |
||
|
|
|||
| 60 | $this->bufferIsActive = true; |
||
| 61 | } |
||
| 62 | |||
| 63 | $tag = ''; |
||
| 64 | |||
| 65 | // construct tag |
||
| 66 | $tag .= "<$name "; |
||
| 67 | |||
| 68 | foreach ( $attr as $k => $v ) { |
||
| 69 | |||
| 70 | //if tag name is file, we must replace the target-language attribute |
||
| 71 | if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
||
| 72 | //replace Target language with job language provided from constructor |
||
| 73 | $tag .= "$k=\"$this->targetLang\" "; |
||
| 74 | } else { |
||
| 75 | $tag .= "$k=\"$v\" "; |
||
| 76 | } |
||
| 77 | |||
| 78 | } |
||
| 79 | |||
| 80 | $seg = $this->getCurrentSegment(); |
||
| 81 | |||
| 82 | if ( $name === $this->tuTagName && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) { |
||
| 83 | |||
| 84 | // add `help-id` to xliff v.1* |
||
| 85 | if ( strpos( $tag, 'help-id' ) === false ) { |
||
| 86 | if ( !empty( $seg[ 'sid' ] ) ) { |
||
| 87 | $tag .= "help-id=\"{$seg[ 'sid' ]}\" "; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | } |
||
| 92 | |||
| 93 | $tag = $this->handleOpenXliffTag( $name, $attr, $tag ); |
||
| 94 | |||
| 95 | $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * @inheritDoc |
||
| 104 | */ |
||
| 105 | protected function tagClose( $parser, string $name ) { |
||
| 106 | $tag = ''; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * if is a tag within <target> or |
||
| 110 | * if it is an empty tag, do not add closing tag because we have already closed it in |
||
| 111 | * |
||
| 112 | * self::tagOpen method |
||
| 113 | */ |
||
| 114 | if ( !$this->isEmpty ) { |
||
| 115 | |||
| 116 | // write closing tag if is not a target |
||
| 117 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
||
| 118 | if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
||
| 119 | $tag = "</$name>"; |
||
| 120 | } |
||
| 121 | |||
| 122 | if ( 'target' == $name && !$this->inAltTrans ) { |
||
| 123 | |||
| 124 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
| 125 | |||
| 126 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
||
| 127 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
||
| 128 | $tag = $this->rebuildTarget(); |
||
| 129 | |||
| 130 | } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
||
| 131 | |||
| 132 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
||
| 133 | $this->bufferIsActive = false; |
||
| 134 | $tag = $this->CDATABuffer . "</$name>"; |
||
| 135 | $this->CDATABuffer = ""; |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->targetWasWritten = true; |
||
| 139 | // signal we are leaving a target |
||
| 140 | $this->inTarget = false; |
||
| 141 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
||
| 142 | |||
| 143 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
||
| 144 | |||
| 145 | $this->bufferIsActive = false; |
||
| 146 | $tag = $this->CDATABuffer . "</$name>"; |
||
| 147 | $this->CDATABuffer = ""; |
||
| 148 | |||
| 149 | //flush to the pointer |
||
| 150 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 151 | |||
| 152 | } elseif ( $name === $this->tuTagName ) { |
||
| 153 | |||
| 154 | $tag = ""; |
||
| 155 | |||
| 156 | // handling </trans-unit> closure |
||
| 157 | if ( !$this->targetWasWritten ) { |
||
| 158 | |||
| 159 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
| 160 | $tag = $this->rebuildTarget(); |
||
| 161 | } else { |
||
| 162 | $tag = $this->createTargetTag( "", "" ); |
||
| 163 | } |
||
| 164 | |||
| 165 | } |
||
| 166 | |||
| 167 | $tag .= "</$this->tuTagName>"; |
||
| 168 | $this->targetWasWritten = false; |
||
| 169 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 170 | |||
| 171 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
| 172 | $this->CDATABuffer .= "</$name>"; |
||
| 173 | // Do NOT Flush |
||
| 174 | } else { //generic tag closure do Nothing |
||
| 175 | // flush to pointer |
||
| 176 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 177 | } |
||
| 178 | |||
| 179 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
||
| 180 | |||
| 181 | $this->isEmpty = false; |
||
| 182 | $this->bufferIsActive = false; |
||
| 183 | $tag = $this->CDATABuffer; |
||
| 184 | $this->CDATABuffer = ""; |
||
| 185 | |||
| 186 | //flush to the pointer |
||
| 187 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 188 | |||
| 189 | } else { |
||
| 190 | //ok, nothing to be done; reset flag for next coming tag |
||
| 191 | $this->isEmpty = false; |
||
| 192 | } |
||
| 193 | |||
| 194 | // try to signal that we are leaving a target |
||
| 195 | $this->tryUnsetAltTrans( $name ); |
||
| 196 | |||
| 197 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
| 198 | if ( $this->tuTagName === $name ) { |
||
| 199 | $this->currentTransUnitIsTranslatable = null; |
||
| 200 | $this->inTU = false; |
||
| 201 | $this->hasWrittenCounts = false; |
||
| 202 | |||
| 203 | $this->resetCounts(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * prepare segment tagging for xliff insertion |
||
| 209 | * |
||
| 210 | * @param array $seg |
||
| 211 | * @param string $transUnitTranslation |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
||
| 216 | |||
| 217 | $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
||
| 218 | $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
||
| 219 | |||
| 220 | if ( $seg [ 'translation' ] == '' ) { |
||
| 221 | $translation = $segment; |
||
| 222 | } else { |
||
| 223 | if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
||
| 224 | $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
||
| 225 | if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
||
| 226 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
||
| 232 | |||
| 233 | return $transUnitTranslation; |
||
| 234 | } |
||
| 235 | |||
| 236 | protected function rebuildMarks( array $seg, string $translation ): string { |
||
| 237 | |||
| 238 | if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
||
| 239 | $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
||
| 240 | } |
||
| 241 | |||
| 242 | return $translation; |
||
| 243 | |||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * This function creates a <target> |
||
| 248 | * |
||
| 249 | * @param string $translation |
||
| 250 | * @param string $stateProp |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | protected function createTargetTag( string $translation, string $stateProp ): string { |
||
| 255 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
||
| 256 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
||
| 257 | $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>'; |
||
| 258 | |||
| 259 | return $tag; |
||
| 260 | |||
| 261 | } |
||
| 262 | |||
| 263 | protected function rebuildTarget(): string { |
||
| 323 | |||
| 324 | } |
||
| 325 | |||
| 326 | protected function getCurrentSegment(): array { |
||
| 332 | } |
||
| 333 | |||
| 334 | } |
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.