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