| Conditions | 7 |
| Paths | 64 |
| Total Lines | 55 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | |||
| 81 | } |