| Conditions | 32 |
| Paths | 3474 |
| Total Lines | 128 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 13 | protected function tagOpen( $parser, $name, $attr ) { |
||
| 14 | // check if we are entering into a <trans-unit> (xliff v1.*) or <unit> (xliff v2.*) |
||
| 15 | if ( $this->tuTagName === $name ) { |
||
| 16 | $this->inTU = true; |
||
| 17 | |||
| 18 | // get id |
||
| 19 | // trim to first 100 characters because this is the limit on Matecat's DB |
||
| 20 | $this->currentTransUnitId = substr( $attr[ 'id' ], 0, 100 ); |
||
| 21 | |||
| 22 | // current 'translate' attribute of the current trans-unit |
||
| 23 | $this->currentTransUnitTranslate = isset( $attr[ 'translate' ] ) ? $attr[ 'translate' ] : 'yes'; |
||
| 24 | } |
||
| 25 | |||
| 26 | // check if we are entering into a <target> |
||
| 27 | if ( 'target' == $name ) { |
||
| 28 | if ( $this->currentTransUnitTranslate === 'no' ) { |
||
| 29 | $this->inTarget = false; |
||
| 30 | } else { |
||
| 31 | $this->inTarget = true; |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | // reset Marker positions |
||
| 36 | if ( 'sdl:seg-defs' == $name ) { |
||
| 37 | $this->markerPos = 0; |
||
| 38 | } |
||
| 39 | |||
| 40 | // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit |
||
| 41 | // <target> must be stripped to be replaced, so this check avoids <target> reconstruction |
||
| 42 | if ( !$this->inTarget ) { |
||
| 43 | |||
| 44 | // costruct tag |
||
| 45 | $tag = "<$name "; |
||
| 46 | |||
| 47 | // needed to avoid multiple conf writing inside the same tag |
||
| 48 | // because the "conf" attribute could be not present in the tag, |
||
| 49 | // so the check on it's name is not enough |
||
| 50 | $_sdlStatus_confWritten = false; |
||
| 51 | |||
| 52 | foreach ( $attr as $k => $v ) { |
||
| 53 | |||
| 54 | // if tag name is file, we must replace the target-language attribute |
||
| 55 | if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) { |
||
| 56 | //replace Target language with job language provided from constructor |
||
| 57 | $tag .= "$k=\"$this->targetLang\" "; |
||
| 58 | |||
| 59 | if ( null !== $this->logger ) { |
||
| 60 | $this->logger->debug( $k . " => " . $this->targetLang ); |
||
| 61 | } |
||
| 62 | } elseif ( 'sdl:seg' == $name ) { |
||
| 63 | |||
| 64 | // write the confidence level for this segment ( Translated, Draft, etc. ) |
||
| 65 | if ( isset( $this->segments[ 'matecat|' . $this->currentTransUnitId ] ) && $_sdlStatus_confWritten === false ) { |
||
| 66 | |||
| 67 | // append definition attribute |
||
| 68 | $tag .= $this->prepareTargetStatuses( $this->lastTransUnit[ $this->markerPos ] ); |
||
| 69 | |||
| 70 | //prepare for an eventual next cycle |
||
| 71 | $this->markerPos++; |
||
| 72 | $_sdlStatus_confWritten = true; |
||
| 73 | } |
||
| 74 | |||
| 75 | // Warning, this is NOT an elseif |
||
| 76 | if ( $k != 'conf' ) { |
||
| 77 | //put also the current attribute in it if it is not a "conf" attribute |
||
| 78 | $tag .= "$k=\"$v\" "; |
||
| 79 | } |
||
| 80 | } else { |
||
| 81 | //normal tag flux, put attributes in it |
||
| 82 | $tag .= "$k=\"$v\" "; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | // this logic helps detecting empty tags |
||
| 87 | // get current position of SAX pointer in all the stream of data is has read so far: |
||
| 88 | // it points at the end of current tag |
||
| 89 | $idx = xml_get_current_byte_index( $parser ); |
||
| 90 | |||
| 91 | // check whether the bounds of current tag are entirely in current buffer || the end of the current tag |
||
| 92 | // is outside current buffer (in the latter case, it's in next buffer to be read by the while loop); |
||
| 93 | // this check is necessary because we may have truncated a tag in half with current read, |
||
| 94 | // and the other half may be encountered in the next buffer it will be passed |
||
| 95 | if ( isset( $this->currentBuffer[ $idx - $this->offset ] ) ) { |
||
| 96 | // if this tag entire lenght fitted in the buffer, the last char must be the last |
||
| 97 | // symbol before the '>'; if it's an empty tag, it is assumed that it's a '/' |
||
| 98 | $tmp_offset = $idx - $this->offset; |
||
| 99 | $lastChar = $this->currentBuffer[ $tmp_offset ]; |
||
| 100 | } else { |
||
| 101 | //if it's out, simple use the last character of the chunk |
||
| 102 | $tmp_offset = $this->len - 1; |
||
| 103 | $lastChar = $this->currentBuffer[ $tmp_offset ]; |
||
| 104 | } |
||
| 105 | |||
| 106 | // trim last space |
||
| 107 | $tag = rtrim( $tag ); |
||
| 108 | |||
| 109 | // detect empty tag |
||
| 110 | $this->isEmpty = ( $lastChar == '/' || $name == 'x' ); |
||
| 111 | if ( $this->isEmpty ) { |
||
| 112 | $tag .= '/'; |
||
| 113 | } |
||
| 114 | |||
| 115 | // add tag ending |
||
| 116 | $tag .= ">"; |
||
| 117 | |||
| 118 | // set a a Buffer for the segSource Source tag |
||
| 119 | if ( 'source' == $name |
||
| 120 | || 'seg-source' === $name |
||
| 121 | || $this->bufferIsActive |
||
| 122 | || 'value' === $name |
||
| 123 | || 'bpt' === $name |
||
| 124 | || 'ept' === $name |
||
| 125 | || 'ph' === $name |
||
| 126 | || 'st' === $name |
||
| 127 | || 'note' === $name |
||
| 128 | || 'context' === $name ) { // we are opening a critical CDATA section |
||
| 129 | |||
| 130 | // WARNING BECAUSE SOURCE AND SEG-SOURCE TAGS CAN BE EMPTY IN SOME CASES!!!!! |
||
| 131 | // so check for isEmpty also in conjunction with name |
||
| 132 | if ( $this->isEmpty && ( 'source' == $name || 'seg-source' == $name ) ) { |
||
| 133 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 134 | } else { |
||
| 135 | //these are NOT source/seg-source/value empty tags, THERE IS A CONTENT, write it in buffer |
||
| 136 | $this->bufferIsActive = true; |
||
| 137 | $this->CDATABuffer .= $tag; |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | $this->postProcAndFlush( $this->outputFP, $tag ); |
||
| 141 | } |
||
| 230 |