Conditions | 22 |
Paths | 56 |
Total Lines | 140 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
164 | protected function tagClose( $parser, string $name ) { |
||
165 | $tag = ''; |
||
166 | |||
167 | /** |
||
168 | * if is a tag within <target> or |
||
169 | * if it is an empty tag, do not add closing tag because we have already closed it in |
||
170 | * |
||
171 | * self::tagOpen method |
||
172 | */ |
||
173 | if ( !$this->isEmpty ) { |
||
174 | |||
175 | // write closing tag if is not a target |
||
176 | // EXCLUDE the target nodes with currentTransUnitIsTranslatable = 'NO' |
||
177 | if ( !$this->inTarget and $this->currentTransUnitIsTranslatable !== 'no' ) { |
||
178 | $tag = "</$name>"; |
||
179 | } |
||
180 | |||
181 | if ( 'target' == $name && !$this->inAltTrans ) { |
||
182 | |||
183 | if ( isset( $this->transUnits[ $this->currentTransUnitId ] ) ) { |
||
184 | |||
185 | $seg = $this->getCurrentSegment(); |
||
186 | |||
187 | // update counts |
||
188 | if ( !$this->hasWrittenCounts && !empty( $seg ) ) { |
||
189 | $this->updateSegmentCounts( $seg ); |
||
190 | } |
||
191 | |||
192 | // delete translations so the prepareSegment |
||
193 | // will put source content in target tag |
||
194 | if ( $this->sourceInTarget ) { |
||
195 | $seg[ 'translation' ] = ''; |
||
196 | $this->resetCounts(); |
||
197 | } |
||
198 | |||
199 | // append $translation |
||
200 | $translation = $this->prepareTranslation( $seg ); |
||
201 | |||
202 | //append translation |
||
203 | $tag = "<target>$translation</target>"; |
||
204 | |||
205 | } elseif( !empty($this->CDATABuffer) and $this->currentTransUnitIsTranslatable === 'no' ) { |
||
206 | |||
207 | // These are target nodes with currentTransUnitIsTranslatable = 'NO' |
||
208 | $this->bufferIsActive = false; |
||
209 | $tag = $this->CDATABuffer . "</$name>"; |
||
210 | $this->CDATABuffer = ""; |
||
211 | } |
||
212 | |||
213 | // signal we are leaving a target |
||
214 | $this->targetWasWritten = true; |
||
215 | $this->inTarget = false; |
||
216 | $this->postProcAndFlush( $this->outputFP, $tag, true ); |
||
217 | |||
218 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { // we are closing a critical CDATA section |
||
219 | |||
220 | $this->bufferIsActive = false; |
||
221 | |||
222 | // only for Xliff 2.* |
||
223 | // write here <mda:metaGroup> and <mda:meta> if already present in the <unit> |
||
224 | if ( 'mda:metadata' === $name && $this->unitContainsMda && !$this->hasWrittenCounts ) { |
||
225 | |||
226 | // we need to update counts here |
||
227 | $this->updateCounts(); |
||
228 | $this->hasWrittenCounts = true; |
||
229 | |||
230 | $tag = $this->CDATABuffer; |
||
231 | $tag .= $this->getWordCountGroupForXliffV2( false ); |
||
232 | $tag .= " </mda:metadata>"; |
||
233 | |||
234 | } else { |
||
235 | $tag = $this->CDATABuffer . "</$name>"; |
||
236 | } |
||
237 | |||
238 | $this->CDATABuffer = ""; |
||
239 | |||
240 | //flush to the pointer |
||
241 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
242 | |||
243 | } elseif ( 'segment' === $name ) { |
||
244 | |||
245 | // only for Xliff 2.* |
||
246 | // if segment has no <target> add it BEFORE </segment> |
||
247 | if ( !$this->targetWasWritten ) { |
||
248 | |||
249 | $seg = $this->getCurrentSegment(); |
||
250 | |||
251 | if ( isset( $seg[ 'translation' ] ) ) { |
||
252 | |||
253 | $translation = $this->prepareTranslation( $seg ); |
||
254 | // replace the tag |
||
255 | $tag = "<target>$translation</target>"; |
||
256 | |||
257 | $tag .= '</segment>'; |
||
258 | |||
259 | } |
||
260 | |||
261 | } |
||
262 | |||
263 | // update segmentPositionInTu |
||
264 | $this->segmentInUnitPosition++; |
||
265 | |||
266 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
267 | |||
268 | // we are leaving <segment>, reset $segmentHasTarget |
||
269 | $this->targetWasWritten = false; |
||
270 | |||
271 | } elseif ( $this->bufferIsActive ) { // this is a tag ( <g | <mrk ) inside a seg or seg-source tag |
||
272 | $this->CDATABuffer .= "</$name>"; |
||
273 | // Do NOT Flush |
||
274 | } else { //generic tag closure do Nothing |
||
275 | // flush to pointer |
||
276 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
277 | } |
||
278 | } elseif ( in_array( $name, $this->nodesToBuffer ) ) { |
||
279 | |||
280 | $this->isEmpty = false; |
||
281 | $this->bufferIsActive = false; |
||
282 | $tag = $this->CDATABuffer; |
||
283 | $this->CDATABuffer = ""; |
||
284 | |||
285 | //flush to the pointer |
||
286 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
287 | |||
288 | } else { |
||
289 | //ok, nothing to be done; reset flag for next coming tag |
||
290 | $this->isEmpty = false; |
||
291 | } |
||
292 | |||
293 | // try to signal that we are leaving a target |
||
294 | $this->tryUnsetAltTrans( $name ); |
||
295 | |||
296 | // check if we are leaving a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
297 | if ( $this->tuTagName === $name ) { |
||
298 | $this->currentTransUnitIsTranslatable = null; |
||
299 | $this->inTU = false; |
||
300 | $this->unitContainsMda = false; |
||
301 | $this->hasWrittenCounts = false; |
||
302 | |||
303 | $this->resetCounts(); |
||
304 | } |
||
383 | } |
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.