Conditions | 11 |
Paths | 17 |
Total Lines | 50 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
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 |
||
83 | public function translate($message, ...$parameters): string |
||
84 | { |
||
85 | // html and empty are returned without processing |
||
86 | if (empty($message)) { |
||
87 | return (string)$message; |
||
88 | } |
||
89 | |||
90 | // expand parameters |
||
91 | if (empty($parameters) && is_array($message) && is_numeric($message[1] ?? null)) { |
||
92 | $parameters[] = $message[1]; |
||
93 | } |
||
94 | |||
95 | // get message and plural if any |
||
96 | $form = null; |
||
97 | $message = $this->getMessage($message, $form); |
||
98 | // check message to be string |
||
99 | if (!is_string($message)) { |
||
100 | return $this->warn('Expected string|array|object::__toString, but %s given.', gettype($message)); |
||
101 | } |
||
102 | |||
103 | // process plural if any |
||
104 | $result = $message; |
||
105 | if ($translation = $this->catalogue->get($message)) { |
||
106 | // plural |
||
107 | if (is_array($translation)) { |
||
108 | if (!array_key_exists($form, $translation) || $form === null) { |
||
109 | $this->warn( |
||
110 | 'Plural form not defined. (message: %s, form: %s)', |
||
111 | (string)$message, |
||
112 | (string)$form |
||
113 | ); |
||
114 | end($translation); |
||
115 | $form = key($translation); |
||
116 | } |
||
117 | |||
118 | $result = $translation[$form]; |
||
119 | } else { |
||
120 | $result = $translation; |
||
121 | } |
||
122 | |||
123 | if ($parameters) { |
||
124 | $result = ($this->normalizeCallback)($result); |
||
125 | $result = @vsprintf($result, $parameters); |
||
126 | // Intentionally @ as argument count can mismatch |
||
127 | } |
||
128 | } else { |
||
129 | $this->untranslated((string)$message); |
||
130 | } |
||
131 | |||
132 | return $result; |
||
133 | } |
||
186 |