Total Complexity | 46 |
Total Lines | 313 |
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 | // write closing tag if is not a target |
||
112 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
||
113 | if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
||
|
|||
114 | $tag = "</$name>"; |
||
115 | } |
||
116 | |||
117 | if ( 'target' == $name && !$this->inAltTrans ) { |
||
118 | |||
119 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
120 | |||
121 | // get translation of current segment, by indirect indexing: id -> positional index -> segment |
||
122 | // actually there may be more than one segment to that ID if there are two mrk of the same source segment |
||
123 | $tag = $this->rebuildTarget(); |
||
124 | |||
125 | } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
||
126 | |||
127 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
||
128 | $this->bufferIsActive = false; |
||
129 | $tag = $this->CDATABuffer . "</$name>"; |
||
130 | $this->CDATABuffer = ""; |
||
131 | } |
||
132 | |||
133 | $this->targetWasWritten = true; |
||
134 | // signal we are leaving a target |
||
135 | $this->inTarget = false; |
||
136 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
||
137 | |||
138 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
||
139 | |||
140 | $this->bufferIsActive = false; |
||
141 | $tag = $this->CDATABuffer . "</$name>"; |
||
142 | $this->CDATABuffer = ""; |
||
143 | |||
144 | //flush to the pointer |
||
145 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
146 | |||
147 | } elseif ( $name === $this->tuTagName ) { |
||
148 | |||
149 | $tag = ""; |
||
150 | |||
151 | // handling </trans-unit> closure |
||
152 | if ( !$this->targetWasWritten ) { |
||
153 | |||
154 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
155 | $tag = $this->rebuildTarget(); |
||
156 | } else { |
||
157 | $tag = $this->createTargetTag( "", "" ); |
||
158 | } |
||
159 | |||
160 | } |
||
161 | |||
162 | $tag .= "</$this->tuTagName>"; |
||
163 | $this->targetWasWritten = false; |
||
164 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
165 | |||
166 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
167 | $this->CDATABuffer .= "</$name>"; |
||
168 | // Do NOT Flush |
||
169 | } else { //generic tag closure do Nothing |
||
170 | // flush to pointer |
||
171 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
172 | } |
||
173 | |||
174 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
||
175 | |||
176 | $this->isEmpty = false; |
||
177 | $this->bufferIsActive = false; |
||
178 | $tag = $this->CDATABuffer; |
||
179 | $this->CDATABuffer = ""; |
||
180 | |||
181 | //flush to the pointer |
||
182 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
183 | |||
184 | } else { |
||
185 | //ok, nothing to be done; reset flag for next coming tag |
||
186 | $this->isEmpty = false; |
||
187 | } |
||
188 | |||
189 | // try to signal that we are leaving a target |
||
190 | $this->tryUnsetAltTrans( $name ); |
||
191 | |||
192 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
193 | if ( $this->tuTagName === $name ) { |
||
194 | $this->currentTransUnitIsTranslatable = null; |
||
195 | $this->inTU = false; |
||
196 | $this->hasWrittenCounts = false; |
||
197 | |||
198 | $this->resetCounts(); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * prepare segment tagging for xliff insertion |
||
204 | * |
||
205 | * @param array $seg |
||
206 | * @param string $transUnitTranslation |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | protected function prepareTranslation( array $seg, string $transUnitTranslation = "" ): string { |
||
211 | |||
212 | $segment = Strings::removeDangerousChars( $seg [ 'segment' ] ); |
||
213 | $translation = Strings::removeDangerousChars( $seg [ 'translation' ] ); |
||
214 | |||
215 | if ( $seg [ 'translation' ] == '' ) { |
||
216 | $translation = $segment; |
||
217 | } else { |
||
218 | if ( $this->callback instanceof XliffReplacerCallbackInterface ) { |
||
219 | $error = ( !empty( $seg[ 'error' ] ) ) ? $seg[ 'error' ] : null; |
||
220 | if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, [], $error ) ) { |
||
221 | $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||'; |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | $transUnitTranslation .= $seg[ 'prev_tags' ] . $this->rebuildMarks( $seg, $translation ) . ltrim( $seg[ 'succ_tags' ] ); |
||
227 | |||
228 | return $transUnitTranslation; |
||
229 | } |
||
230 | |||
231 | protected function rebuildMarks( array $seg, string $translation ): string { |
||
232 | |||
233 | if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) { |
||
234 | $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . $translation . $seg[ 'mrk_succ_tags' ] . "</mrk>"; |
||
235 | } |
||
236 | |||
237 | return $translation; |
||
238 | |||
239 | } |
||
240 | |||
241 | /** |
||
242 | * This function creates a <target> |
||
243 | * |
||
244 | * @param string $translation |
||
245 | * @param string $stateProp |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | protected function createTargetTag( string $translation, string $stateProp ): string { |
||
250 | $targetLang = ' xml:lang="' . $this->targetLang . '"'; |
||
251 | $tag = "<target $targetLang $stateProp>$translation</target>"; |
||
252 | $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>'; |
||
253 | |||
254 | return $tag; |
||
255 | |||
256 | } |
||
257 | |||
258 | protected function rebuildTarget(): string { |
||
318 | |||
319 | } |
||
320 | |||
321 | protected function getCurrentSegment(): array { |
||
327 | } |
||
328 | |||
329 | } |
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
die
introduces 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 withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.