Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LanguageService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LanguageService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class LanguageService implements LanguageServiceInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var \eZ\Publish\API\Repository\Repository |
||
32 | */ |
||
33 | protected $repository; |
||
34 | |||
35 | /** |
||
36 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
37 | */ |
||
38 | protected $languageHandler; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $settings; |
||
44 | |||
45 | /** |
||
46 | * Setups service with reference to repository object that created it & corresponding handler. |
||
47 | * |
||
48 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
49 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
50 | * @param array $settings |
||
51 | */ |
||
52 | public function __construct(RepositoryInterface $repository, Handler $languageHandler, array $settings = array()) |
||
61 | |||
62 | /** |
||
63 | * Creates the a new Language in the content repository. |
||
64 | * |
||
65 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations |
||
66 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the languageCode already exists |
||
67 | * |
||
68 | * @param \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct $languageCreateStruct |
||
69 | * |
||
70 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
71 | */ |
||
72 | public function createLanguage(LanguageCreateStruct $languageCreateStruct) |
||
117 | |||
118 | /** |
||
119 | * Changes the name of the language in the content repository. |
||
120 | * |
||
121 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
122 | * is not string |
||
123 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations |
||
124 | * |
||
125 | * @param \eZ\Publish\API\Repository\Values\Content\Language $language |
||
126 | * @param string $newName |
||
127 | * |
||
128 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
129 | */ |
||
130 | public function updateLanguageName(Language $language, $newName) |
||
162 | |||
163 | /** |
||
164 | * Enables a language. |
||
165 | * |
||
166 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations |
||
167 | * |
||
168 | * @param \eZ\Publish\API\Repository\Values\Content\Language $language |
||
169 | * |
||
170 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
171 | */ |
||
172 | View Code Duplication | public function enableLanguage(Language $language) |
|
200 | |||
201 | /** |
||
202 | * Disables a language. |
||
203 | * |
||
204 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations |
||
205 | * |
||
206 | * @param \eZ\Publish\API\Repository\Values\Content\Language $language |
||
207 | * |
||
208 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
209 | */ |
||
210 | View Code Duplication | public function disableLanguage(Language $language) |
|
238 | |||
239 | /** |
||
240 | * Loads a Language from its language code ($languageCode). |
||
241 | * |
||
242 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument |
||
243 | * is not string |
||
244 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found |
||
245 | * |
||
246 | * @param string $languageCode |
||
247 | * |
||
248 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
249 | */ |
||
250 | public function loadLanguage($languageCode) |
||
260 | |||
261 | /** |
||
262 | * Loads all Languages. |
||
263 | * |
||
264 | * @return \eZ\Publish\API\Repository\Values\Content\Language[] |
||
265 | */ |
||
266 | public function loadLanguages() |
||
277 | |||
278 | /** |
||
279 | * Loads a Language by its id ($languageId). |
||
280 | * |
||
281 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found |
||
282 | * |
||
283 | * @param mixed $languageId |
||
284 | * |
||
285 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
286 | */ |
||
287 | public function loadLanguageById($languageId) |
||
293 | |||
294 | /** |
||
295 | * {@inheritdoc} |
||
296 | */ |
||
297 | public function loadLanguageListByCode(array $languageCodes): iterable |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function loadLanguageListById(array $languageIds): iterable |
||
323 | |||
324 | /** |
||
325 | * Deletes a language from content repository. |
||
326 | * |
||
327 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
328 | * if language can not be deleted |
||
329 | * because it is still assigned to some content / type / (...). |
||
330 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations |
||
331 | * |
||
332 | * @param \eZ\Publish\API\Repository\Values\Content\Language $language |
||
333 | */ |
||
334 | View Code Duplication | public function deleteLanguage(Language $language) |
|
354 | |||
355 | /** |
||
356 | * Returns a configured default language code. |
||
357 | * |
||
358 | * @return string |
||
359 | */ |
||
360 | public function getDefaultLanguageCode() |
||
364 | |||
365 | /** |
||
366 | * Returns a configured list of prioritized languageCodes. |
||
367 | * |
||
368 | * |
||
369 | * @return string[] |
||
370 | */ |
||
371 | public function getPrioritizedLanguageCodeList() |
||
375 | |||
376 | /** |
||
377 | * Instantiates an object to be used for creating languages. |
||
378 | * |
||
379 | * @return \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct |
||
380 | */ |
||
381 | public function newLanguageCreateStruct() |
||
385 | |||
386 | /** |
||
387 | * Builds Language domain object from ValueObject returned by Persistence API. |
||
388 | * |
||
389 | * @param \eZ\Publish\SPI\Persistence\Content\Language $spiLanguage |
||
390 | * |
||
391 | * @return \eZ\Publish\API\Repository\Values\Content\Language |
||
392 | */ |
||
393 | protected function buildDomainObject(SPILanguage $spiLanguage) |
||
404 | } |
||
405 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.