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 |
||
17 | class TrashHandler extends AbstractHandler implements TrashHandlerInterface |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | public function loadTrashItem($id) |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | View Code Duplication | public function trashSubtree($locationId) |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function recover($trashedId, $newParentId) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function findTrashItems(Criterion $criterion = null, $offset = 0, $limit = null, array $sort = null) |
||
59 | { |
||
60 | $this->logger->logCall(__METHOD__, array('criterion' => $criterion ? get_class($criterion) : 'null')); |
||
61 | |||
62 | return $this->persistenceHandler->trashHandler()->findTrashItems($criterion, $offset, $limit, $sort); |
||
|
|||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function emptyTrash() |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function deleteTrashItem($trashedId) |
||
82 | } |
||
83 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.