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