Passed
Branch fix-lack-of-untranslated-when-... (eeef8d)
by Domenico
04:05
created

prepareTranslation()   C

Complexity

Conditions 12
Paths 144

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 144
nop 2
dl 0
loc 37
rs 6.6
c 0
b 0
f 0

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
namespace Matecat\XliffParser\XliffReplacer;
4
5
use Matecat\XliffParser\Utils\Strings;
6
7
class oldSdlOldXliffSAXTranslationReplacer extends oldXliffSAXTranslationReplacer {
8
    protected $segmentInUnitPosition = "";
9
10
    /**
11
     * @inheritDoc
12
     */
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->currentTransUnitIsTranslatable = isset( $attr[ 'translate' ] ) ? $attr[ 'translate' ] : 'yes';
24
        }
25
26
        // check if we are entering into a <target>
27
        if ( 'target' == $name ) {
28
            if ( $this->currentTransUnitIsTranslatable === '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->segmentInUnitPosition = 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->segmentInUnitPosition ] );
69
70
                        //prepare for an eventual next cycle
71
                        $this->segmentInUnitPosition++;
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
            }
142
        }
143
    }
144
145
    /**
146
     * prepare segment tagging for xliff insertion
147
     *
148
     * @param array  $seg
149
     * @param string $transUnitTranslation
150
     *
151
     * @return string
152
     */
153
    protected function prepareTranslation( $seg, $transUnitTranslation = "" ) {
154
        $endTags = "";
155
156
        $segment     = Strings::removeDangerousChars( $seg [ 'segment' ] );
157
        $translation = Strings::removeDangerousChars( $seg [ 'translation' ] );
158
        $dataRefMap  = ( isset( $seg[ 'data_ref_map' ] ) && $seg[ 'data_ref_map' ] !== null ) ? Strings::jsonToArray( $seg[ 'data_ref_map' ] ) : [];
159
160
        if ( is_null( $seg [ 'translation' ] ) || $seg [ 'translation' ] == '' ) {
161
            $translation = $segment;
162
        } else {
163
            if ( $this->callback instanceof XliffReplacerCallbackInterface ) {
164
                $error = (isset($seg['error'])) ? $seg['error'] : null;
165
                if ( $this->callback->thereAreErrors( $seg[ 'sid' ], $segment, $translation, $dataRefMap, $error ) ) {
166
                    $translation = '|||UNTRANSLATED_CONTENT_START|||' . $segment . '|||UNTRANSLATED_CONTENT_END|||';
167
                }
168
            }
169
        }
170
171
        // for Trados the trailing spaces after </mrk> are meaningful
172
        // so we trim the translation from Matecat DB and add them after </mrk>
173
        $trailingSpaces = '';
174
        for ( $s = 0; $s < Strings::getTheNumberOfTrailingSpaces( $translation ); $s++ ) {
175
            $trailingSpaces .= ' ';
176
        }
177
178
        if ( $seg[ 'mrk_id' ] !== null && $seg[ 'mrk_id' ] != '' ) {
179
            if ( $this->targetLang === 'ja-JP' ) {
180
                $seg[ 'mrk_succ_tags' ] = ltrim( $seg[ 'mrk_succ_tags' ] );
181
            }
182
183
            $translation = "<mrk mid=\"" . $seg[ 'mrk_id' ] . "\" mtype=\"seg\">" . $seg[ 'mrk_prev_tags' ] . rtrim( $translation ) . $seg[ 'mrk_succ_tags' ] . "</mrk>" . $trailingSpaces;
184
        }
185
186
        // we need to trim succ_tags here because we already added the trailing spaces after </mrk>
187
        $transUnitTranslation .= $seg[ 'prev_tags' ] . $translation . $endTags . ltrim( $seg[ 'succ_tags' ] );
188
189
        return $transUnitTranslation;
190
    }
191
192
    /**
193
     * @param $segment
194
     *
195
     * @return string
196
     */
197
    protected function prepareTargetStatuses( $segment ) {
198
        $statusMap = [
199
                'NEW'        => '',
200
                'DRAFT'      => 'Draft',
201
                'TRANSLATED' => 'Translated',
202
                'APPROVED'   => 'ApprovedTranslation',
203
                'REJECTED'   => 'RejectedTranslation',
204
        ];
205
206
        return "conf=\"{$statusMap[ $segment[ 'status' ] ]}\" ";
207
    }
208
209
    /**
210
     * @param $seg
211
     * @param $state_prop
212
     * @param $lastMrkState
213
     *
214
     * @return array
215
     */
216
    protected function setTransUnitState( $seg, $state_prop, $lastMrkState ) {
0 ignored issues
show
Unused Code introduced by
The parameter $lastMrkState is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

216
    protected function setTransUnitState( $seg, $state_prop, /** @scrutinizer ignore-unused */ $lastMrkState ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $state_prop is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

216
    protected function setTransUnitState( $seg, /** @scrutinizer ignore-unused */ $state_prop, $lastMrkState ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $seg is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

216
    protected function setTransUnitState( /** @scrutinizer ignore-unused */ $seg, $state_prop, $lastMrkState ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
217
        return [ null, null ];
218
    }
219
220
    /**
221
     * @param $raw_word_count
222
     * @param $eq_word_count
223
     *
224
     * @return string
225
     */
226
    protected function getWordCountGroup( $raw_word_count, $eq_word_count ) {
227
        return '';
228
    }
229
}
230