Passed
Pull Request — master (#90)
by Domenico
02:53
created

XliffSdl::prepareTargetStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
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
            // needed to avoid multiple conf writing inside the same tag
42
            // because the "conf" attribute could be not present in the tag,
43
            // so the check on it's name is not enough
44
            $_sdlStatus_confWritten = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $_sdlStatus_confWritten is dead and can be removed.
Loading history...
45
46
            foreach ( $attr as $k => $v ) {
47
48
                // if tag name is file, we must replace the target-language attribute
49
                if ( $name == 'file' && $k == 'target-language' && !empty( $this->targetLang ) ) {
50
                    //replace Target language with job language provided from constructor
51
                    $tag .= "$k=\"$this->targetLang\" ";
52
                } else {
53
                    //normal tag flux, put attributes in it
54
                    // Warning, this is NOT an elseif
55
                    if ( $k != 'conf' ) {
56
                        //put also the current attribute in it if it is not a "conf" attribute
57
                        $tag .= "$k=\"$v\" ";
58
                    }
59
                }
60
            }
61
62
            $seg = $this->getCurrentSegment();
63
64
            if ( 'sdl:seg' == $name && !empty( $seg ) and isset( $seg[ 'sid' ] ) ) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning 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 have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces 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 with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
65
                $tag .= $this->prepareTargetStatuses( $seg );
66
            }
67
68
            $this->checkForSelfClosedTagAndFlush( $parser, $tag );
69
70
        }
71
72
    }
73
74
    /**
75
     * @param $segment
76
     *
77
     * @return string
78
     */
79
    protected function prepareTargetStatuses( $segment ): string {
80
        $statusMap = [
81
                'NEW'        => '',
82
                'DRAFT'      => 'Draft',
83
                'TRANSLATED' => 'Translated',
84
                'APPROVED'   => 'ApprovedTranslation',
85
                'APPROVED2'  => 'ApprovedSignOff',
86
                'REJECTED'   => 'RejectedTranslation',
87
        ];
88
89
        return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" ";
90
    }
91
92
    protected function rebuildMarks( array $seg, string $translation ): string {
93
94
        $trailingSpaces = str_repeat( ' ', Strings::getTheNumberOfTrailingSpaces( $translation ) );
95
96
        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
97
            if ( $this->targetLang === 'ja-JP' ) {
98
                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
99
            }
100
101
            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces;
102
        }
103
104
        return $translation;
105
106
    }
107
108
}