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 |
||
| 26 | class CommentManager implements CommentManagerInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var ThreadRepositoryInterface |
||
| 30 | */ |
||
| 31 | private $threadRepository; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var CommentRepositoryInterface |
||
| 35 | */ |
||
| 36 | private $commentRepository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var EventDispatcherInterface |
||
| 40 | */ |
||
| 41 | private $dispatcher; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param ThreadRepositoryInterface $threadRepository |
||
| 45 | * @param CommentRepositoryInterface $commentRepository |
||
| 46 | * @param EventDispatcherInterface $dispatcher |
||
| 47 | */ |
||
| 48 | 26 | public function __construct( |
|
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | 1 | public function findComments($type, $entityId, $page = 1, $pageSize = null) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 1 | public function findPublishedComments($type, $entityId, $page = 1, $pageSize = null) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 7 | public function addComment($type, $entityId, CommentInterface $comment, $threadTitle = null) |
|
| 78 | { |
||
| 79 | 7 | $thread = $this->threadRepository->findThread($type, $entityId); |
|
| 80 | 7 | if (!$thread) { |
|
| 81 | 5 | $thread = $this->threadRepository->createNew($type, $entityId); |
|
| 82 | } |
||
| 83 | |||
| 84 | 7 | if ($threadTitle) { |
|
|
|
|||
| 85 | 6 | $thread->setTitle($threadTitle); |
|
| 86 | } |
||
| 87 | |||
| 88 | 7 | $this->dispatcher->dispatch(Events::PRE_PERSIST_EVENT, new CommentEvent($type, $entityId, $comment, $thread)); |
|
| 89 | 7 | $this->commentRepository->persist($comment); |
|
| 90 | 7 | $thread = $thread->addComment($comment); |
|
| 91 | 7 | $this->dispatcher->dispatch(Events::POST_PERSIST_EVENT, new CommentEvent($type, $entityId, $comment, $thread)); |
|
| 92 | |||
| 93 | 7 | return $thread; |
|
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 2 | public function update(CommentInterface $comment) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * {@inheritdoc} |
||
| 112 | */ |
||
| 113 | 4 | public function delete($ids) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | 2 | public function updateThread(ThreadInterface $thread) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | 4 | public function deleteThreads($ids) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 3 | View Code Duplication | public function publish(CommentInterface $comment) |
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | 3 | View Code Duplication | public function unpublish(CommentInterface $comment) |
| 189 | |||
| 190 | /** |
||
| 191 | * Delete comment and raise pre/post events. |
||
| 192 | * |
||
| 193 | * @param CommentInterface $comment |
||
| 194 | */ |
||
| 195 | 4 | private function deleteComment(CommentInterface $comment) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Delete thread and raise pre/post events. |
||
| 210 | * |
||
| 211 | * @param ThreadInterface $thread |
||
| 212 | */ |
||
| 213 | 4 | private function deleteThread(ThreadInterface $thread) |
|
| 221 | } |
||
| 222 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: