Conditions | 12 |
Paths | 2 |
Total Lines | 121 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
68 | public function formatTranslatableViolationList( |
||
69 | ConstraintViolationListInterface $violations, |
||
70 | $sortProperties = true, |
||
71 | $useAllLanguages = true, |
||
72 | array $targetLocales = [], |
||
73 | $domain = 'messages' |
||
74 | ) { |
||
75 | $violationList = iterator_to_array($violations, false); |
||
76 | $hasLocales = count($targetLocales) > 0; |
||
77 | |||
78 | if (!$useAllLanguages && $hasLocales || $useAllLanguages && !$hasLocales) { |
||
79 | throw new \InvalidArgumentException( |
||
80 | 'Wrong usage of $targetLocales: If all languages should be rendered, $targetLocales should be set, if not, $targetLocales should be an empty array!' |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | $fixtures = []; |
||
85 | array_walk( |
||
86 | $violationList, |
||
87 | function (ConstraintViolation $violation) use ( |
||
88 | $targetLocales, |
||
89 | $domain, |
||
90 | $useAllLanguages, |
||
91 | $sortProperties, |
||
92 | &$fixtures |
||
93 | ) { |
||
94 | // Every translation entry (whether sorted by properties in deeper levels or as top level) |
||
95 | // should provide a basic structure. |
||
96 | switch (true) { |
||
97 | // If sorted by $targetLanguages, it may look like this: |
||
98 | // |
||
99 | // [ |
||
100 | // 'de' => ['Deutscher Text'], |
||
101 | // 'en' => ['English text'], |
||
102 | // ] |
||
103 | // |
||
104 | // When sorting by property, the structure will be merged recursively into the property list: |
||
105 | // |
||
106 | // [ |
||
107 | // 'property' => [ |
||
108 | // // this is the structure merged into the property. |
||
109 | // 'de' => ['Deutscher Text'], |
||
110 | // 'en' => ['English text'], |
||
111 | // ] |
||
112 | // ] |
||
113 | case $useAllLanguages: |
||
114 | $structure = array_reduce( |
||
115 | $targetLocales, |
||
116 | function ($carry, $locale) use ($violation, $domain) { |
||
117 | if (!isset($carry[$locale])) { |
||
118 | $carry[$locale] = []; |
||
119 | } |
||
120 | |||
121 | if ($locale === $this->translator->getLocale()) { |
||
122 | $carry[$locale][] = $violation->getMessage(); |
||
123 | } else { |
||
124 | $messageTemplate = $violation->getMessageTemplate(); |
||
125 | $parameters = $violation->getParameters(); |
||
126 | |||
127 | if ($plural = $violation->getPlural()) { |
||
128 | try { |
||
129 | $message = $this->translator->transChoice( |
||
130 | $messageTemplate, |
||
131 | $plural, |
||
132 | $parameters, |
||
133 | $domain, |
||
134 | $locale |
||
135 | ); |
||
136 | } catch (\InvalidArgumentException $ex) { |
||
137 | // we do nothing here. |
||
138 | // If the pluralization fails, the default translation method will be used. |
||
139 | |||
140 | $message = $this->translator->trans( |
||
141 | $messageTemplate, |
||
142 | $parameters, |
||
143 | $domain, |
||
144 | $locale |
||
145 | ); |
||
146 | } |
||
147 | } else { |
||
148 | $message = $this->translator->trans( |
||
149 | $messageTemplate, |
||
150 | $parameters, |
||
151 | $domain, |
||
152 | $locale |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | $carry[$locale][] = $message; |
||
157 | } |
||
158 | |||
159 | return $carry; |
||
160 | }, |
||
161 | [] |
||
162 | ); |
||
163 | break; |
||
164 | default: |
||
165 | // If the default language is in use and nothing more, the structure looks like this: |
||
166 | // |
||
167 | // ['Message in the currently selected language'] |
||
168 | $structure = [$violation->getMessage()]; |
||
169 | } |
||
170 | |||
171 | if (!$sortProperties) { |
||
172 | $fixtures = array_merge_recursive($fixtures, $structure); |
||
173 | |||
174 | return; |
||
175 | } |
||
176 | |||
177 | $propertyPath = $violation->getPropertyPath(); |
||
178 | if (!isset($fixtures[$violation->getPropertyPath()])) { |
||
179 | $fixtures[$violation->getPropertyPath()] = []; |
||
180 | } |
||
181 | |||
182 | $fixtures[$propertyPath] = array_merge_recursive($fixtures[$propertyPath], $structure); |
||
183 | }, |
||
184 | [] |
||
185 | ); |
||
186 | |||
187 | return $fixtures; |
||
188 | } |
||
189 | } |
||
190 |