| Conditions | 28 |
| Paths | 4620 |
| Total Lines | 115 |
| Code Lines | 42 |
| 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 |
||
| 35 | protected function tagOpen( $parser, $name, $attr ) { |
||
| 36 | |||
| 37 | $this->handleOpenUnit( $name, $attr ); |
||
| 38 | |||
| 39 | if ( 'mda:metadata' === $name ) { |
||
| 40 | $this->unitContainsMda = true; |
||
| 41 | } |
||
| 42 | |||
| 43 | // check if we are entering into a <target> |
||
| 44 | if ( 'target' === $name ) { |
||
| 45 | |||
| 46 | if ( $this->currentTransUnitIsTranslatable === 'no' ) { |
||
| 47 | $this->inTarget = false; |
||
| 48 | } else { |
||
| 49 | $this->inTarget = true; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | // open buffer |
||
| 54 | if ( in_array( $name, $this->nodesToBuffer ) ) { |
||
| 55 | $this->bufferIsActive = true; |
||
| 56 | } |
||
| 57 | |||
| 58 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
||
| 59 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
||
| 60 | if ( !$this->inTarget ) { |
||
| 61 | |||
| 62 | $tag = ''; |
||
| 63 | |||
| 64 | // |
||
| 65 | // ============================================ |
||
| 66 | // only for Xliff 2.* |
||
| 67 | // ============================================ |
||
| 68 | // |
||
| 69 | // In xliff v2 we MUST add <mda:metadata> BEFORE <notes>/<originalData>/<segment>/<ignorable> |
||
| 70 | // |
||
| 71 | // As documentation says, <unit> contains: |
||
| 72 | // |
||
| 73 | // - elements from other namespaces, OPTIONAL |
||
| 74 | // - Zero or one <notes> elements followed by |
||
| 75 | // - Zero or one <originalData> element followed by |
||
| 76 | // - One or more <segment> or <ignorable> elements in any order. |
||
| 77 | // |
||
| 78 | // For more info please refer to: |
||
| 79 | // |
||
| 80 | // http://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html#unit |
||
| 81 | // |
||
| 82 | if ( |
||
| 83 | ( $name === 'notes' || $name === 'originalData' || $name === 'segment' || $name === 'ignorable' ) && |
||
| 84 | $this->unitContainsMda === false && |
||
| 85 | !empty( $this->transUnits[ $this->currentTransUnitId ] ) && |
||
| 86 | !$this->hasWrittenCounts |
||
| 87 | ) { |
||
| 88 | // we need to update counts here |
||
| 89 | $this->updateCounts(); |
||
| 90 | $this->hasWrittenCounts = true; |
||
| 91 | $tag .= $this->getWordCountGroupForXliffV2(); |
||
| 92 | $this->unitContainsMda = true; |
||
| 93 | } |
||
| 94 | |||
| 95 | // construct tag |
||
| 96 | $tag .= "<$name "; |
||
| 97 | |||
| 98 | foreach ( $attr as $k => $v ) { |
||
| 99 | |||
| 100 | //if tag name is file, we must replace the target-language attribute |
||
| 101 | if ( $name === 'file' && $k === 'target-language' && !empty( $this->targetLang ) ) { |
||
| 102 | //replace Target language with job language provided from constructor |
||
| 103 | $tag .= "$k=\"$this->targetLang\" "; |
||
| 104 | } else { |
||
| 105 | |||
| 106 | //normal tag flux, put attributes in it but skip for translation state and set the right value for the attribute |
||
| 107 | if ( $k != 'state' ) { |
||
| 108 | $tag .= "$k=\"$v\" "; |
||
| 109 | } |
||
| 110 | |||
| 111 | } |
||
| 112 | |||
| 113 | } |
||
| 114 | |||
| 115 | $seg = $this->getCurrentSegment(); |
||
| 116 | |||
| 117 | if ( $name === $this->tuTagName and !empty( $seg ) and isset( $seg[ 'sid' ] ) ) { |
||
|
|
|||
| 118 | |||
| 119 | // add `help-id` to xliff v.1* |
||
| 120 | // add `mtc:segment-id` to xliff v.2* |
||
| 121 | if ( strpos( $tag, 'mtc:segment-id' ) === false ) { |
||
| 122 | $tag .= "mtc:segment-id=\"{$seg[ 'sid' ]}\" "; |
||
| 123 | } |
||
| 124 | |||
| 125 | } |
||
| 126 | |||
| 127 | // replace state for xliff v2 |
||
| 128 | if ( 'segment' === $name ) { // add state to segment in Xliff v2 |
||
| 129 | [ $stateProp, ] = StatusToStateAttribute::getState( $seg[ 'status' ], $this->xliffVersion ); |
||
| 130 | $tag .= $stateProp; |
||
| 131 | } |
||
| 132 | |||
| 133 | |||
| 134 | // add oasis xliff 20 namespace |
||
| 135 | if ( $name === 'xliff' && !array_key_exists( 'xmlns:mda', $attr ) ) { |
||
| 136 | $tag .= 'xmlns:mda="urn:oasis:names:tc:xliff:metadata:2.0"'; |
||
| 137 | } |
||
| 138 | |||
| 139 | // add MateCat specific namespace, we want maybe add non-XLIFF attributes |
||
| 140 | if ( $name === 'xliff' && !array_key_exists( 'xmlns:mtc', $attr ) ) { |
||
| 141 | $tag .= ' xmlns:mtc="https://www.matecat.com" '; |
||
| 142 | } |
||
| 143 | |||
| 144 | // trgLang |
||
| 145 | if ( $name === 'xliff' ) { |
||
| 146 | $tag = preg_replace( '/trgLang="(.*?)"/', 'trgLang="' . $this->targetLang . '"', $tag ); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->checkForSelfClosedTagAndFlush( $parser, $tag ); |
||
| 150 | |||
| 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.