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