Passed
Push — master ( cd421c...e31be6 )
by Domenico
02:52
created

XliffSdl::tagOpen()   B

Complexity

Conditions 11
Paths 18

Size

Total Lines 45
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 17
c 1
b 0
f 0
nc 18
nop 3
dl 0
loc 45
rs 7.3166

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * @author hashashiyyin [email protected] / [email protected]
5
 * Date: 02/08/24
6
 * Time: 19:04
7
 *
8
 */
9
10
namespace Matecat\XliffParser\XliffReplacer;
11
12
use Matecat\XliffParser\Utils\Strings;
13
14
class XliffSdl extends Xliff12 {
15
16
    /**
17
     * @inheritDoc
18
     */
19
    protected function tagOpen( $parser, string $name, array $attr ) {
20
21
        $this->handleOpenUnit( $name, $attr );
22
23
        // check if we are entering into a <target>
24
        $this->checkSetInTarget( $name );
25
26
        // reset Marker positions
27
        if ( 'sdl:seg-defs' == $name ) {
28
            $this->segmentInUnitPosition = 0;
29
        }
30
31
        // open buffer
32
        $this->setInBuffer( $name );
33
34
        // check if we are inside a <target>, obviously this happen only if there are targets inside the trans-unit
35
        // <target> must be stripped to be replaced, so this check avoids <target> reconstruction
36
        if ( !$this->inTarget ) {
37
38
            // costruct tag
39
            $tag = "<$name ";
40
41
            foreach ( $attr as $k => $v ) {
42
43
                // if tag name is file, we must replace the target-language attribute
44
                if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) {
45
                    //replace Target language with job language provided from constructor
46
                    $tag .= "$k=\"$this->targetLang\" ";
47
                } else {
48
                    //normal tag flux, put attributes in it
49
                    // Warning, this is NOT an elseif
50
                    if ( $k != 'conf' ) {
51
                        //put also the current attribute in it if it is not a "conf" attribute
52
                        $tag .= "$k=\"$v\" ";
53
                    }
54
                }
55
            }
56
57
            $seg = $this->getCurrentSegment();
58
59
            if ( 'sdl:seg' == $name && !empty( $seg ) && isset( $seg[ 'sid' ] ) ) {
60
                $tag .= $this->prepareTargetStatuses( $seg );
61
            }
62
63
            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
64
65
        }
66
67
    }
68
69
    /**
70
     * @param $segment
71
     *
72
     * @return string
73
     */
74
    protected function prepareTargetStatuses( $segment ): string {
75
        $statusMap = [
76
                'NEW'        => '',
77
                'DRAFT'      => 'Draft',
78
                'TRANSLATED' => 'Translated',
79
                'APPROVED'   => 'ApprovedTranslation',
80
                'APPROVED2'  => 'ApprovedSignOff',
81
                'REJECTED'   => 'RejectedTranslation',
82
        ];
83
84
        return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" ";
85
    }
86
87
    protected function rebuildMarks( array $seg, string $translation ): string {
88
89
        $trailingSpaces = str_repeat( ' ', Strings::getTheNumberOfTrailingSpaces( $translation ) );
90
91
        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
92
            if ( $this->targetLang === 'ja-JP' ) {
93
                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
94
            }
95
96
            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces;
97
        }
98
99
        return $translation;
100
101
    }
102
103
}