| Total Complexity | 70 |
| Total Lines | 395 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DataRefReplacer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DataRefReplacer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class DataRefReplacer { |
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $map; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * DataRefReplacer constructor. |
||
| 27 | * |
||
| 28 | * @param array $map |
||
| 29 | */ |
||
| 30 | public function __construct( array $map = null ) { |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * This function inserts a new attribute called 'equiv-text' from dataRef contained in <ph>, <sc>, <ec>, <pc> tags against the provided map array |
||
| 36 | * |
||
| 37 | * For a complete reference see: |
||
| 38 | * |
||
| 39 | * Http://docs.oasis-open.org/xliff/xliff-core/v2.1/os/xliff-core-v2.1-os.html#dataref |
||
| 40 | * |
||
| 41 | * @param string $string |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function replace( $string ) { |
||
| 46 | |||
| 47 | // if map is empty |
||
| 48 | // or the string has not a dataRef attribute |
||
| 49 | // return string as is |
||
| 50 | if ( empty( $this->map ) || !$this->hasAnyDataRefAttribute( $string ) ) { |
||
| 51 | return $string; |
||
| 52 | } |
||
| 53 | |||
| 54 | // try not to throw exception for wrong segments with opening tags and no closing |
||
| 55 | try { |
||
| 56 | |||
| 57 | $html = XmlParser::parse( $string, true ); |
||
| 58 | |||
| 59 | $dataRefEndMap = []; |
||
| 60 | |||
| 61 | foreach ( $html as $node ) { |
||
| 62 | |||
| 63 | // 1. Replace <ph>|<sc>|<ec> tags |
||
| 64 | $string = $this->recursiveTransformDataRefToPhTag( $node, $string ); |
||
|
|
|||
| 65 | |||
| 66 | // 2. Replace self-closed <pc dataRefStart="xyz" /> tags |
||
| 67 | $string = $this->recursiveReplaceSelfClosedPcTags( $node, $string ); |
||
| 68 | |||
| 69 | // 3. Build the DataRefEndMap needed by replaceClosingPcTags function |
||
| 70 | // (needed for correct handling of </pc> closing tags) |
||
| 71 | // make this inline with one foreach cycle |
||
| 72 | $this->extractDataRefMapRecursively( $node, $dataRefEndMap ); |
||
| 73 | |||
| 74 | } |
||
| 75 | |||
| 76 | // 4. replace pc tags |
||
| 77 | $string = $this->replaceOpeningPcTags( null, $string ); |
||
| 78 | $string = $this->replaceClosingPcTags( $string, $dataRefEndMap ); |
||
| 79 | |||
| 80 | } catch ( Exception $ignore ) { |
||
| 81 | } finally { |
||
| 82 | return $string; |
||
| 83 | } |
||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $string |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | private function hasAnyDataRefAttribute( $string ) { |
||
| 93 | return (bool)preg_match( '/(dataRef|dataRefStart|dataRefEnd)=[\'"].*?[\'"]/', $string ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * This function adds equiv-text attribute to <ph>, <ec>, and <sc> tags. |
||
| 98 | * |
||
| 99 | * Please note that <ec> and <sc> tags are converted to <ph> tags (needed by Matecat); |
||
| 100 | * in this case, another special attribute (dataType) is added just before equiv-text |
||
| 101 | * |
||
| 102 | * If there is no id tag, it will be copied from dataRef attribute |
||
| 103 | * |
||
| 104 | * @param object $node |
||
| 105 | * @param string $string |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | private function recursiveTransformDataRefToPhTag( $node, $string ) { |
||
| 110 | |||
| 111 | if ( $node->has_children ) { |
||
| 112 | |||
| 113 | foreach ( $node->inner_html as $childNode ) { |
||
| 114 | $string = $this->recursiveTransformDataRefToPhTag( $childNode, $string ); |
||
| 115 | } |
||
| 116 | |||
| 117 | } else { |
||
| 118 | |||
| 119 | switch ( $node->tagName ) { |
||
| 120 | case 'ph': |
||
| 121 | case 'sc': |
||
| 122 | case 'ec': |
||
| 123 | break; |
||
| 124 | default: |
||
| 125 | return $string; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( !isset( $node->attributes[ 'dataRef' ] ) ) { |
||
| 129 | return $string; |
||
| 130 | } |
||
| 131 | |||
| 132 | // if isset a value in the map calculate base64 encoded value |
||
| 133 | // otherwise skip |
||
| 134 | if ( !in_array( $node->attributes[ 'dataRef' ], array_keys( $this->map ) ) ) { |
||
| 135 | return $string; |
||
| 136 | } |
||
| 137 | |||
| 138 | $dataRefName = $node->attributes[ 'dataRef' ]; // map identifier. Eg: source1 |
||
| 139 | $dataRefValue = $this->map[ $dataRefName ]; // map identifier. Eg: source1 |
||
| 140 | |||
| 141 | // check if is null or an empty string, in this case, convert it to NULL string |
||
| 142 | if ( is_null( $dataRefValue ) || $dataRefValue === '' ) { |
||
| 143 | $this->map[ $dataRefName ] = 'NULL'; |
||
| 144 | } |
||
| 145 | |||
| 146 | |||
| 147 | $newTag = [ '<ph' ]; |
||
| 148 | |||
| 149 | // if there is no id copy it from dataRef |
||
| 150 | if ( !isset( $node->attributes[ 'id' ] ) ) { |
||
| 151 | $newTag[] = 'id="' . $dataRefName . '"'; |
||
| 152 | $newTag[] = 'x-removeId="true"'; |
||
| 153 | } else { |
||
| 154 | $newTag[] = 'id="' . $node->attributes[ 'id' ] . '"'; |
||
| 155 | } |
||
| 156 | |||
| 157 | // introduce dataType for <ec>/<sc> tag handling |
||
| 158 | if ( $node->tagName === 'ec' ) { |
||
| 159 | $newTag[] = 'ctype="' . CTypeEnum::EC_DATA_REF . '"'; |
||
| 160 | } elseif ( $node->tagName === 'sc' ) { |
||
| 161 | $newTag[] = 'ctype="' . CTypeEnum::SC_DATA_REF . '"'; |
||
| 162 | } else { |
||
| 163 | $newTag[] = 'ctype="' . CTypeEnum::PH_DATA_REF . '"'; |
||
| 164 | } |
||
| 165 | |||
| 166 | $newTag[] = 'equiv-text="base64:' . base64_encode( $dataRefValue ) . '"'; |
||
| 167 | $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
||
| 168 | |||
| 169 | return str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | return $string; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $node |
||
| 178 | * @param $string |
||
| 179 | * |
||
| 180 | * @return mixed |
||
| 181 | * @throws DOMException |
||
| 182 | * @throws InvalidXmlException |
||
| 183 | * @throws XmlParsingException |
||
| 184 | */ |
||
| 185 | private function recursiveReplaceSelfClosedPcTags( $node, $string ) { |
||
| 186 | |||
| 187 | if ( $node->has_children ) { |
||
| 188 | |||
| 189 | foreach ( $node->inner_html as $childNode ) { |
||
| 190 | $string = $this->recursiveReplaceSelfClosedPcTags( $childNode, $string ); |
||
| 191 | } |
||
| 192 | |||
| 193 | } elseif ( $node->tagName == 'pc' && $node->self_closed === true ) { |
||
| 194 | |||
| 195 | if ( isset( $node->attributes[ 'dataRefStart' ] ) && array_key_exists( $node->attributes[ 'dataRefStart' ], $this->map ) ) { |
||
| 196 | |||
| 197 | $newTag = [ '<ph' ]; |
||
| 198 | |||
| 199 | if ( isset( $node->attributes[ 'id' ] ) ) { |
||
| 200 | $newTag[] = 'id="' . $node->attributes[ 'id' ] . '_1"'; |
||
| 201 | } |
||
| 202 | |||
| 203 | $newTag[] = 'ctype="' . CTypeEnum::PC_SELF_CLOSE_DATA_REF . '"'; |
||
| 204 | $newTag[] = 'equiv-text="base64:' . base64_encode( $this->map[ $node->attributes[ 'dataRefStart' ] ] ) . '"'; |
||
| 205 | $newTag[] = 'x-orig="' . base64_encode( $node->node ) . '"'; |
||
| 206 | |||
| 207 | $string = str_replace( $node->node, implode( " ", $newTag ) . '/>', $string ); |
||
| 208 | } |
||
| 209 | |||
| 210 | } |
||
| 211 | |||
| 212 | return $string; |
||
| 213 | |||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Extract (recursively) the dataRefEnd map from single nodes |
||
| 218 | * |
||
| 219 | * @param object $node |
||
| 220 | * @param $dataRefEndMap |
||
| 221 | */ |
||
| 222 | private function extractDataRefMapRecursively( $node, &$dataRefEndMap ) { |
||
| 244 | ]; |
||
| 245 | |||
| 246 | } |
||
| 247 | |||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Replace opening <pc> tags with correct reference in the $string |
||
| 252 | * |
||
| 253 | * @param string $string |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | * @throws DOMException |
||
| 257 | * @throws InvalidXmlException |
||
| 258 | * @throws XmlParsingException |
||
| 259 | */ |
||
| 260 | private function replaceOpeningPcTags( $node, $string ) { |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Replace closing </pc> tags with correct reference in the $string |
||
| 303 | * thanks to $dataRefEndMap |
||
| 304 | * |
||
| 305 | * @param string $string |
||
| 306 | * @param array $dataRefEndMap |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | private function replaceClosingPcTags( $string, $dataRefEndMap = [] ) { |
||
| 311 | |||
| 312 | preg_match_all( '|</pc>|iu', $string, $closingPcMatches, PREG_OFFSET_CAPTURE ); |
||
| 313 | $delta = 0; |
||
| 314 | |||
| 315 | foreach ( $closingPcMatches[ 0 ] as $index => $match ) { |
||
| 316 | |||
| 317 | $offset = $match[ 1 ]; |
||
| 318 | $length = 5; // strlen of '</pc>' |
||
| 319 | |||
| 320 | $attr = isset( $dataRefEndMap[ $index ] ) ? $dataRefEndMap[ $index ] : null; |
||
| 321 | |||
| 322 | if ( !empty( $attr ) && isset( $attr[ 'dataRefEnd' ] ) ) { |
||
| 323 | |||
| 324 | $endValue = !empty( $this->map[ $attr[ 'dataRefEnd' ] ] ) ? $this->map[ $attr[ 'dataRefEnd' ] ] : 'NULL'; |
||
| 325 | |||
| 326 | $newTag = [ '<ph' ]; |
||
| 327 | |||
| 328 | if ( isset( $attr[ 'id' ] ) ) { |
||
| 329 | $newTag[] = 'id="' . $attr[ 'id' ] . '_2"'; |
||
| 330 | } |
||
| 331 | |||
| 332 | $newTag[] = 'ctype="' . CTypeEnum::PC_CLOSE_DATA_REF . '"'; |
||
| 333 | $newTag[] = 'equiv-text="base64:' . base64_encode( $endValue ) . '"'; |
||
| 334 | $newTag[] = 'x-orig="' . base64_encode( '</pc>' ) . '"'; |
||
| 335 | |||
| 336 | // conversion for opening <pc> tag |
||
| 337 | $completeTag = implode( " ", $newTag ) . '/>'; |
||
| 338 | $realOffset = ( $delta === 0 ) ? $offset : ( $offset + $delta ); |
||
| 339 | $string = substr_replace( $string, $completeTag, $realOffset, $length ); |
||
| 340 | $delta = $delta + strlen( $completeTag ) - $length; |
||
| 341 | |||
| 342 | } |
||
| 343 | |||
| 344 | } |
||
| 345 | |||
| 346 | return $string; |
||
| 347 | |||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $string |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | * @throws DOMException |
||
| 355 | * @throws InvalidXmlException |
||
| 356 | * @throws XmlParsingException |
||
| 357 | */ |
||
| 358 | public function restore( $string ) { |
||
| 359 | |||
| 360 | // if map is empty return string as is |
||
| 361 | if ( empty( $this->map ) ) { |
||
| 362 | return $string; |
||
| 363 | } |
||
| 364 | |||
| 365 | // replace eventual empty equiv-text="" |
||
| 366 | // $string = str_replace( ' equiv-text=""', '', $string ); |
||
| 367 | $html = XmlParser::parse( $string, true ); |
||
| 368 | |||
| 369 | foreach ( $html as $node ) { |
||
| 370 | $string = $this->recursiveRestoreOriginalTags( $node, $string ); |
||
| 371 | } |
||
| 372 | |||
| 373 | return $string; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param object $node |
||
| 378 | * @param $string |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | private function recursiveRestoreOriginalTags( $node, $string ) { |
||
| 414 | |||
| 415 | } |
||
| 416 | |||
| 417 | } |