LocaleHelper::transform()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 4
eloc 32
c 4
b 0
f 0
nc 4
nop 1
dl 0
loc 58
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.408

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
 * Mollie locale helper.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2022 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
/**
14
 * Title: Mollie locale helper
15
 * Description:
16
 * Copyright: 2005-2022 Pronamic
17
 * Company: Pronamic
18
 *
19
 * @author  Remco Tolsma
20
 * @version 2.0.9
21
 * @since   1.0.0
22
 */
23
class LocaleHelper {
24
	/**
25
	 * Get Mollie locale by the specified WordPress locale.
26
	 *
27
	 * @param string|null $locale Locale string (en_US) to transform to Mollie locale.
28
	 * @return string|null
29
	 */
30 12
	public static function transform( $locale ) {
31 12
		if ( ! \is_string( $locale ) ) {
32 1
			return null;
33
		}
34
35
		/**
36
		 * Supported locales.
37
		 *
38
		 * @var array<int, string>
39
		 */
40
		$supported = array(
41 11
			Locales::EN_US,
42
			Locales::NL_NL,
43
			Locales::NL_BE,
44
			Locales::FR_FR,
45
			Locales::FR_BE,
46
			Locales::DE_DE,
47
			Locales::DE_AT,
48
			Locales::DE_CH,
49
			Locales::ES_ES,
50
			Locales::CA_ES,
51
			Locales::PT_PT,
52
			Locales::IT_IT,
53
			Locales::NB_NO,
54
			Locales::SV_SE,
55
			Locales::FI_FI,
56
			Locales::DA_DK,
57
			Locales::IS_IS,
58
			Locales::HU_HU,
59
			Locales::PL_PL,
60
			Locales::LV_LV,
61
			Locales::LT_LT,
62
		);
63
64
		// Lowercase.
65 11
		$locale = \strtolower( $locale );
66
67
		// Is supported?
68 11
		$supported_lowercase = \array_map( 'strtolower', $supported );
69
70 11
		$search = \array_search( $locale, $supported_lowercase, true );
71
72
		// Locale not supported.
73 11
		if ( false === $search ) {
74 8
			return null;
75
		}
76
77
		/**
78
		 * As with all internal PHP functions as of 5.3.0, `array_search()`
79
		 * returns `NULL` if invalid parameters are passed to it.
80
		 *
81
		 * @link https://www.php.net/array_search
82
		 */
83 3
		if ( \array_key_exists( $search, $supported ) ) {
84 3
			return $supported[ $search ];
85
		}
86
87
		return null;
88
	}
89
}
90