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 |
||
35 | class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface |
||
36 | { |
||
37 | /** |
||
38 | * @var Repository |
||
39 | */ |
||
40 | private $repository; |
||
41 | |||
42 | /** |
||
43 | * @var LoggerInterface |
||
44 | */ |
||
45 | private $logger; |
||
46 | |||
47 | /** |
||
48 | * @var UserService |
||
49 | */ |
||
50 | private $userService; |
||
51 | |||
52 | /** |
||
53 | * @var ContentService |
||
54 | */ |
||
55 | private $contentService; |
||
56 | |||
57 | /** |
||
58 | * @var ContentTypeService |
||
59 | */ |
||
60 | private $contentTypeService; |
||
61 | |||
62 | /** |
||
63 | * @param Repository $repository |
||
64 | */ |
||
65 | 6 | public function __construct(Repository $repository) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 22 | public function setLogger(LoggerInterface $logger) |
|
77 | { |
||
78 | 22 | $this->logger = $logger; |
|
79 | 22 | } |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 16 | public function find(ValueObject $object, $throwException = false) |
|
85 | { |
||
86 | try { |
||
87 | 16 | if (isset($object->data['remote_id'])) { |
|
88 | 4 | $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']); |
|
89 | 2 | $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id); |
|
90 | 16 | } elseif ($object->getProperty('id')) { |
|
91 | 16 | $userGroup = $this->userService->loadUserGroup($object->getProperty('id')); |
|
92 | 16 | } |
|
93 | 16 | } catch (NotFoundException $notFoundException) { |
|
94 | // We'll throw our own exception later instead. |
||
95 | } |
||
96 | |||
97 | 16 | if (!isset($userGroup)) { |
|
98 | 16 | throw new ObjectNotFoundException(UserGroup::class, array('remote_id', 'id')); |
|
99 | } |
||
100 | |||
101 | 16 | return $userGroup; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Shortcut to get UserGroup by id, mainly to get parent by Id. |
||
106 | * |
||
107 | * @param int $id |
||
108 | * @param bool $throwException |
||
109 | * |
||
110 | * @return UserGroup|false |
||
111 | * |
||
112 | * @throws NotFoundException |
||
113 | */ |
||
114 | 16 | public function findById($id, $throwException = false) |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 16 | public function create(ObjectInterface $object) |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 4 | public function update(ObjectInterface $object) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 16 | public function createOrUpdate(ObjectInterface $object) |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 3 | View Code Duplication | public function remove(ObjectInterface $object) |
213 | } |
||
214 |
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.