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