| Conditions | 4 |
| Paths | 4 |
| Total Lines | 58 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 11 |
| CRAP Score | 4.0092 |
| Changes | 4 | ||
| 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 |
||
| 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 | } |
||
| 90 |