Conditions | 12 |
Paths | 120 |
Total Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
34 | public function loadArray(array $array, Translations $translations = null): Translations |
||
35 | { |
||
36 | if (!$translations) { |
||
37 | $translations = $this->createTranslations(); |
||
38 | } |
||
39 | |||
40 | $messages = $array['messages'] ?? []; |
||
41 | |||
42 | foreach ($messages as $context => $contextTranslations) { |
||
43 | if ($context === '') { |
||
44 | $context = null; |
||
45 | } |
||
46 | |||
47 | foreach ($contextTranslations as $original => $value) { |
||
48 | //Headers |
||
49 | if ($context === null && $original === '') { |
||
50 | $string = is_array($value) ? array_shift($value) : $value; |
||
51 | $headers = $translations->getHeaders(); |
||
52 | |||
53 | foreach (static::parseHeaders($string) as $name => $value) { |
||
|
|||
54 | $headers->set($name, $value); |
||
55 | } |
||
56 | continue; |
||
57 | } |
||
58 | |||
59 | $translation = $this->createTranslation($context, $original); |
||
60 | $translations->add($translation); |
||
61 | |||
62 | if (is_array($value)) { |
||
63 | $translation->translate(array_shift($value)); |
||
64 | $translation->translatePlural(...$value); |
||
65 | } else { |
||
66 | $translation->translate($value); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if (!empty($array['domain'])) { |
||
72 | $translations->setDomain($array['domain']); |
||
73 | } |
||
74 | |||
75 | if (!empty($array['plural-forms'])) { |
||
76 | $translations->getHeaders()->set(Headers::HEADER_PLURAL, $array['plural-forms']); |
||
77 | } |
||
78 | |||
79 | return $translations; |
||
80 | } |
||
81 | } |
||
82 |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.