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