| Total Complexity | 166 |
| Total Lines | 234 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TranslatorTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TranslatorTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | trait TranslatorTrait |
||
| 22 | { |
||
| 23 | private $locale; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | public function setLocale(string $locale) |
||
| 29 | { |
||
| 30 | $this->locale = $locale; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * {@inheritdoc} |
||
| 35 | */ |
||
| 36 | public function getLocale() |
||
| 37 | { |
||
| 38 | return $this->locale ?: \Locale::getDefault(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string |
||
| 45 | { |
||
| 46 | if (null === $id || '' === $id) { |
||
| 47 | return ''; |
||
| 48 | } |
||
| 49 | |||
| 50 | if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) { |
||
| 51 | return strtr($id, $parameters); |
||
| 52 | } |
||
| 53 | |||
| 54 | $number = (float) $parameters['%count%']; |
||
| 55 | $locale = $locale ?: $this->getLocale(); |
||
| 56 | |||
| 57 | $parts = []; |
||
| 58 | if (preg_match('/^\|++$/', $id)) { |
||
| 59 | $parts = explode('|', $id); |
||
| 60 | } elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) { |
||
| 61 | $parts = $matches[0]; |
||
| 62 | } |
||
| 63 | |||
| 64 | $intervalRegexp = <<<'EOF' |
||
| 65 | /^(?P<interval> |
||
| 66 | ({\s* |
||
| 67 | (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*) |
||
| 68 | \s*}) |
||
| 69 | |||
| 70 | | |
||
| 71 | |||
| 72 | (?P<left_delimiter>[\[\]]) |
||
| 73 | \s* |
||
| 74 | (?P<left>-Inf|\-?\d+(\.\d+)?) |
||
| 75 | \s*,\s* |
||
| 76 | (?P<right>\+?Inf|\-?\d+(\.\d+)?) |
||
| 77 | \s* |
||
| 78 | (?P<right_delimiter>[\[\]]) |
||
| 79 | )\s*(?P<message>.*?)$/xs |
||
| 80 | EOF; |
||
| 81 | |||
| 82 | $standardRules = []; |
||
| 83 | foreach ($parts as $part) { |
||
| 84 | $part = trim(str_replace('||', '|', $part)); |
||
| 85 | |||
| 86 | // try to match an explicit rule, then fallback to the standard ones |
||
| 87 | if (preg_match($intervalRegexp, $part, $matches)) { |
||
| 88 | if ($matches[2]) { |
||
| 89 | foreach (explode(',', $matches[3]) as $n) { |
||
| 90 | if ($number == $n) { |
||
| 91 | return strtr($matches['message'], $parameters); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | $leftNumber = '-Inf' === $matches['left'] ? -INF : (float) $matches['left']; |
||
| 96 | $rightNumber = is_numeric($matches['right']) ? (float) $matches['right'] : INF; |
||
| 97 | |||
| 98 | if (('[' === $matches['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) |
||
| 99 | && (']' === $matches['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber) |
||
| 100 | ) { |
||
| 101 | return strtr($matches['message'], $parameters); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } elseif (preg_match('/^\w+\:\s*(.*?)$/', $part, $matches)) { |
||
| 105 | $standardRules[] = $matches[1]; |
||
| 106 | } else { |
||
| 107 | $standardRules[] = $part; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $position = $this->getPluralizationRule($number, $locale); |
||
| 112 | |||
| 113 | if (!isset($standardRules[$position])) { |
||
| 114 | // when there's exactly one rule given, and that rule is a standard |
||
| 115 | // rule, use this rule |
||
| 116 | if (1 === \count($parts) && isset($standardRules[0])) { |
||
| 117 | return strtr($standardRules[0], $parameters); |
||
| 118 | } |
||
| 119 | |||
| 120 | $message = sprintf('Unable to choose a translation for "%s" with locale "%s" for value "%d". Double check that this translation has the correct plural options (e.g. "There is one apple|There are %%count%% apples").', $id, $locale, $number); |
||
| 121 | |||
| 122 | if (class_exists(InvalidArgumentException::class)) { |
||
| 123 | throw new InvalidArgumentException($message); |
||
| 124 | } |
||
| 125 | |||
| 126 | throw new \InvalidArgumentException($message); |
||
| 127 | } |
||
| 128 | |||
| 129 | return strtr($standardRules[$position], $parameters); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns the plural position to use for the given locale and number. |
||
| 134 | * |
||
| 135 | * The plural rules are derived from code of the Zend Framework (2010-09-25), |
||
| 136 | * which is subject to the new BSD license (http://framework.zend.com/license/new-bsd). |
||
| 137 | * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
||
| 138 | */ |
||
| 139 | private function getPluralizationRule(int $number, string $locale): int |
||
| 140 | { |
||
| 141 | switch ('pt_BR' !== $locale && \strlen($locale) > 3 ? substr($locale, 0, strrpos($locale, '_')) : $locale) { |
||
| 142 | case 'af': |
||
| 143 | case 'bn': |
||
| 144 | case 'bg': |
||
| 145 | case 'ca': |
||
| 146 | case 'da': |
||
| 147 | case 'de': |
||
| 148 | case 'el': |
||
| 149 | case 'en': |
||
| 150 | case 'eo': |
||
| 151 | case 'es': |
||
| 152 | case 'et': |
||
| 153 | case 'eu': |
||
| 154 | case 'fa': |
||
| 155 | case 'fi': |
||
| 156 | case 'fo': |
||
| 157 | case 'fur': |
||
| 158 | case 'fy': |
||
| 159 | case 'gl': |
||
| 160 | case 'gu': |
||
| 161 | case 'ha': |
||
| 162 | case 'he': |
||
| 163 | case 'hu': |
||
| 164 | case 'is': |
||
| 165 | case 'it': |
||
| 166 | case 'ku': |
||
| 167 | case 'lb': |
||
| 168 | case 'ml': |
||
| 169 | case 'mn': |
||
| 170 | case 'mr': |
||
| 171 | case 'nah': |
||
| 172 | case 'nb': |
||
| 173 | case 'ne': |
||
| 174 | case 'nl': |
||
| 175 | case 'nn': |
||
| 176 | case 'no': |
||
| 177 | case 'oc': |
||
| 178 | case 'om': |
||
| 179 | case 'or': |
||
| 180 | case 'pa': |
||
| 181 | case 'pap': |
||
| 182 | case 'ps': |
||
| 183 | case 'pt': |
||
| 184 | case 'so': |
||
| 185 | case 'sq': |
||
| 186 | case 'sv': |
||
| 187 | case 'sw': |
||
| 188 | case 'ta': |
||
| 189 | case 'te': |
||
| 190 | case 'tk': |
||
| 191 | case 'ur': |
||
| 192 | case 'zu': |
||
| 193 | return (1 == $number) ? 0 : 1; |
||
| 194 | |||
| 195 | case 'am': |
||
| 196 | case 'bh': |
||
| 197 | case 'fil': |
||
| 198 | case 'fr': |
||
| 199 | case 'gun': |
||
| 200 | case 'hi': |
||
| 201 | case 'hy': |
||
| 202 | case 'ln': |
||
| 203 | case 'mg': |
||
| 204 | case 'nso': |
||
| 205 | case 'pt_BR': |
||
| 206 | case 'ti': |
||
| 207 | case 'wa': |
||
| 208 | return ((0 == $number) || (1 == $number)) ? 0 : 1; |
||
| 209 | |||
| 210 | case 'be': |
||
| 211 | case 'bs': |
||
| 212 | case 'hr': |
||
| 213 | case 'ru': |
||
| 214 | case 'sh': |
||
| 215 | case 'sr': |
||
| 216 | case 'uk': |
||
| 217 | return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); |
||
| 218 | |||
| 219 | case 'cs': |
||
| 220 | case 'sk': |
||
| 221 | return (1 == $number) ? 0 : ((($number >= 2) && ($number <= 4)) ? 1 : 2); |
||
| 222 | |||
| 223 | case 'ga': |
||
| 224 | return (1 == $number) ? 0 : ((2 == $number) ? 1 : 2); |
||
| 225 | |||
| 226 | case 'lt': |
||
| 227 | return ((1 == $number % 10) && (11 != $number % 100)) ? 0 : ((($number % 10 >= 2) && (($number % 100 < 10) || ($number % 100 >= 20))) ? 1 : 2); |
||
| 228 | |||
| 229 | case 'sl': |
||
| 230 | return (1 == $number % 100) ? 0 : ((2 == $number % 100) ? 1 : (((3 == $number % 100) || (4 == $number % 100)) ? 2 : 3)); |
||
| 231 | |||
| 232 | case 'mk': |
||
| 233 | return (1 == $number % 10) ? 0 : 1; |
||
| 234 | |||
| 235 | case 'mt': |
||
| 236 | return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 1) && ($number % 100 < 11))) ? 1 : ((($number % 100 > 10) && ($number % 100 < 20)) ? 2 : 3)); |
||
| 237 | |||
| 238 | case 'lv': |
||
| 239 | return (0 == $number) ? 0 : (((1 == $number % 10) && (11 != $number % 100)) ? 1 : 2); |
||
| 240 | |||
| 241 | case 'pl': |
||
| 242 | return (1 == $number) ? 0 : ((($number % 10 >= 2) && ($number % 10 <= 4) && (($number % 100 < 12) || ($number % 100 > 14))) ? 1 : 2); |
||
| 243 | |||
| 244 | case 'cy': |
||
| 245 | return (1 == $number) ? 0 : ((2 == $number) ? 1 : (((8 == $number) || (11 == $number)) ? 2 : 3)); |
||
| 246 | |||
| 247 | case 'ro': |
||
| 248 | return (1 == $number) ? 0 : (((0 == $number) || (($number % 100 > 0) && ($number % 100 < 20))) ? 1 : 2); |
||
| 249 | |||
| 250 | case 'ar': |
||
| 251 | return (0 == $number) ? 0 : ((1 == $number) ? 1 : ((2 == $number) ? 2 : ((($number % 100 >= 3) && ($number % 100 <= 10)) ? 3 : ((($number % 100 >= 11) && ($number % 100 <= 99)) ? 4 : 5)))); |
||
| 252 | |||
| 253 | default: |
||
| 254 | return 0; |
||
| 255 | } |
||
| 258 |