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

StatusToStateAttribute::getState()   D

Complexity

Conditions 28
Paths 23

Size

Total Lines 61
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 40
nc 23
nop 4
dl 0
loc 61
rs 4.1666
c 1
b 0
f 0

How to fix   Long Method    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: 13:11
7
 *
8
 */
9
10
namespace Matecat\XliffParser\XliffReplacer;
11
12
use Matecat\XliffParser\Constants\TranslationStatus;
13
14
class StatusToStateAttribute {
15
16
17
    /**
18
     * @param string  $status
19
     * @param int     $xliffVersion
20
     * @param ?string $state_prop
21
     * @param ?string $lastMrkState
22
     *
23
     * @return array
24
     */
25
    public static function getState( string $status, int $xliffVersion, ?string $state_prop = '', ?string $lastMrkState = '' ): array {
26
27
        switch ( $status ) {
28
29
            case TranslationStatus::STATUS_FIXED:
30
            case TranslationStatus::STATUS_APPROVED2:
31
                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED2 ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
32
                    $state_prop   = "state=\"final\"";
33
                    $lastMrkState = TranslationStatus::STATUS_APPROVED2;
34
                }
35
                break;
36
            case TranslationStatus::STATUS_APPROVED:
37
                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_APPROVED ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
38
                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"";
39
                    $lastMrkState = TranslationStatus::STATUS_APPROVED;
40
                }
41
                break;
42
43
            case TranslationStatus::STATUS_TRANSLATED:
44
                if ( $lastMrkState == null || $lastMrkState == TranslationStatus::STATUS_TRANSLATED || $lastMrkState == TranslationStatus::STATUS_APPROVED ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
45
                    $state_prop   = "state=\"translated\"";
46
                    $lastMrkState = TranslationStatus::STATUS_TRANSLATED;
47
                }
48
                break;
49
50
            case TranslationStatus::STATUS_REJECTED:  // if there is a mark REJECTED and there is not a DRAFT, all the trans-unit is REJECTED. In V2 there is no way to mark
51
            case TranslationStatus::STATUS_REBUTTED:
52
                if ( ( $lastMrkState == null ) || ( $lastMrkState != TranslationStatus::STATUS_NEW || $lastMrkState != TranslationStatus::STATUS_DRAFT ) ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
53
                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"";
54
                    $lastMrkState = TranslationStatus::STATUS_REJECTED;
55
                }
56
                break;
57
58
            case TranslationStatus::STATUS_NEW:
59
                if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_NEW ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
60
                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
61
                    $lastMrkState = TranslationStatus::STATUS_NEW;
62
                }
63
                break;
64
65
            case TranslationStatus::STATUS_DRAFT:
66
                if ( ( $lastMrkState == null ) || $lastMrkState != TranslationStatus::STATUS_DRAFT ) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
67
                    $state_prop   = ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"";
68
                    $lastMrkState = TranslationStatus::STATUS_DRAFT;
69
                }
70
                break;
71
72
            default:
73
                // this is the case when a segment is not showed in cattool, so the row in
74
                // segment_translations does not exists and
75
                // ---> $seg[ 'status' ] is NULL
76
                if ( $lastMrkState == null ) { //this is the first MRK ID
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $lastMrkState of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
77
                    $state_prop   = "state=\"translated\"";
78
                    $lastMrkState = TranslationStatus::STATUS_TRANSLATED;
79
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
80
                    /* Do nothing and preserve the last state */
81
                }
82
                break;
83
        }
84
85
        return [ $state_prop, $lastMrkState ];
86
87
    }
88
89
}