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

StatusToStateAttribute::getState()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 55
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 29
c 1
b 0
f 0
nc 64
nop 3
dl 0
loc 55
rs 8.5226

How to fix   Long Method   

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
     * @param int         $xliffVersion
18
     * @param string|null $status
19
     * @param string|null $lastMrkState
20
     *
21
     * @return array
22
     */
23
    public static function getState(
24
            int     $xliffVersion,
25
            ?string $status = null,
26
            ?string $lastMrkState = null
27
    ): array {
28
29
        $status       = empty( $status ) ? TranslationStatus::STATUS_TRANSLATED : $status;
30
31
        $stateLevelsMap = [
32
                TranslationStatus::STATUS_APPROVED2  => 100,
33
                TranslationStatus::STATUS_APPROVED   => 90,
34
                TranslationStatus::STATUS_TRANSLATED => 80,
35
                TranslationStatus::STATUS_REJECTED   => 70,
36
                TranslationStatus::STATUS_DRAFT      => 60,
37
                TranslationStatus::STATUS_NEW        => 50
38
        ];
39
40
        $orderedValues = array_flip( $stateLevelsMap );
41
42
        // Define state mappings for different statuses
43
        $stateMap = [
44
                TranslationStatus::STATUS_APPROVED2  => [ "state=\"final\"", TranslationStatus::STATUS_APPROVED2 ],
45
                TranslationStatus::STATUS_APPROVED   => [
46
                        ( $xliffVersion === 2 ) ? "state=\"reviewed\"" : "state=\"signed-off\"",
47
                        TranslationStatus::STATUS_APPROVED
48
                ],
49
                TranslationStatus::STATUS_TRANSLATED => [ "state=\"translated\"", TranslationStatus::STATUS_TRANSLATED ],
50
                TranslationStatus::STATUS_REJECTED   => [
51
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"needs-review-translation\"",
52
                        TranslationStatus::STATUS_REJECTED
53
                ],
54
                TranslationStatus::STATUS_NEW        => [
55
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
56
                        TranslationStatus::STATUS_NEW
57
                ],
58
                TranslationStatus::STATUS_DRAFT      => [
59
                        ( $xliffVersion === 2 ) ? "state=\"initial\"" : "state=\"new\"",
60
                        TranslationStatus::STATUS_DRAFT
61
                ],
62
        ];
63
64
        // If status is null set the default as Translated.
65
        // This is the case when a segment is not shown in the cattool,
66
        // and the row in segment_translations does not exists.
67
        // ---> $seg[ 'status' ] is NULL
68
        // If lastMrkState is empty
69
        $minStatus = min(
70
                $stateLevelsMap[ $status ],
71
                ( $stateLevelsMap[ $lastMrkState ] ?? $stateLevelsMap[ TranslationStatus::STATUS_NEW ] )
72
        );
73
74
        // If the last mark state is set, get the minimum value, otherwise get the current state
75
        [ $state_prop, $lastMrkState ] = empty( $lastMrkState ) ? $stateMap[ $status ] : $stateMap[ $orderedValues[ $minStatus ] ];
76
77
        return [ $state_prop, $lastMrkState ];
78
79
    }
80
81
}