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:
1 | <?php |
||
36 | class LanguageManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface |
||
37 | { |
||
38 | /** |
||
39 | * @var Repository |
||
40 | */ |
||
41 | private $repository; |
||
42 | |||
43 | /** |
||
44 | * @var LoggerInterface Logger |
||
45 | */ |
||
46 | private $logger; |
||
47 | |||
48 | /** |
||
49 | * @var LanguageService Language service |
||
50 | */ |
||
51 | private $languageService; |
||
52 | |||
53 | /** |
||
54 | * @param Repository $repository |
||
55 | */ |
||
56 | 6 | public function __construct(Repository $repository) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 22 | public function setLogger(LoggerInterface $logger) |
|
66 | { |
||
67 | 22 | $this->logger = $logger; |
|
68 | 22 | } |
|
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 37 | View Code Duplication | public function find(ValueObject $object) |
|
|||
74 | { |
||
75 | try { |
||
76 | 37 | if (isset($object->data['code'])) { |
|
77 | 37 | $language = $this->languageService->loadLanguage($object->data['code']); |
|
78 | 36 | } |
|
79 | 37 | } catch (NotFoundException $notFoundException) { |
|
80 | // We'll throw our own exception later instead. |
||
81 | } |
||
82 | |||
83 | 37 | if (!isset($language)) { |
|
84 | 3 | throw new ObjectNotFoundException(Language::class, array('code')); |
|
85 | } |
||
86 | |||
87 | 36 | return $language; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 36 | public function create(ObjectInterface $object) |
|
94 | { |
||
95 | 36 | if (!$object instanceof LanguageObject) { |
|
96 | 1 | throw new UnsupportedObjectOperationException(LanguageObject::class, get_class($object)); |
|
97 | } |
||
98 | |||
99 | try { |
||
100 | 35 | $language = $this->find($object); |
|
101 | 33 | $this->languageService->enableLanguage($language); |
|
102 | 35 | } catch (NotFoundException $notFoundException) { |
|
103 | 2 | $languageCreateStruct = $this->languageService->newLanguageCreateStruct(); |
|
104 | 2 | $languageCreateStruct->languageCode = $object->data['code']; |
|
105 | 2 | $languageCreateStruct->name = $object->data['name']; |
|
106 | 2 | $language = $this->languageService->createLanguage($languageCreateStruct); |
|
107 | } |
||
108 | |||
109 | 35 | $object->getMapper()->languageToObject($language); |
|
110 | |||
111 | 35 | return $object; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 2 | public function update(ObjectInterface $object) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 3 | public function createOrUpdate(ObjectInterface $object) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 4 | public function remove(ObjectInterface $object) |
|
173 | } |
||
174 |
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.