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 |
||
| 34 | class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var Repository |
||
| 38 | */ |
||
| 39 | private $repository; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var LoggerInterface |
||
| 43 | */ |
||
| 44 | private $logger; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var UserService |
||
| 48 | */ |
||
| 49 | private $userService; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var ContentService |
||
| 53 | */ |
||
| 54 | private $contentService; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ContentTypeService |
||
| 58 | */ |
||
| 59 | private $contentTypeService; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param Repository $repository |
||
| 63 | */ |
||
| 64 | 6 | public function __construct(Repository $repository) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | 22 | public function setLogger(LoggerInterface $logger) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Load a UserGroup by remote_id or id. |
||
| 82 | * |
||
| 83 | * @param ValueObject $object |
||
| 84 | * @param bool $throwException |
||
| 85 | * |
||
| 86 | * @return UserGroup|false |
||
| 87 | * |
||
| 88 | * @throws NotFoundException |
||
| 89 | */ |
||
| 90 | 15 | public function find(ValueObject $object, $throwException = false) |
|
| 91 | 1 | { |
|
| 92 | try { |
||
| 93 | 15 | if (isset($object->data['remote_id'])) { |
|
| 94 | 3 | $contentObject = $this->contentService->loadContentByRemoteId($object->data['remote_id']); |
|
| 95 | 2 | $userGroup = $this->userService->loadUserGroup($contentObject->contentInfo->id); |
|
| 96 | 15 | } elseif ($object->getProperty('id')) { |
|
| 97 | 15 | $userGroup = $this->userService->loadUserGroup($object->getProperty('id')); |
|
| 98 | 15 | } |
|
| 99 | 15 | } catch (NotFoundException $notFoundException) { |
|
| 100 | 3 | $exception = $notFoundException; |
|
| 101 | } |
||
| 102 | |||
| 103 | 15 | if (!isset($userGroup)) { |
|
| 104 | 15 | if (isset($exception) && $throwException) { |
|
| 105 | 1 | throw $exception; |
|
| 106 | } |
||
| 107 | |||
| 108 | 15 | return false; |
|
| 109 | } |
||
| 110 | |||
| 111 | 15 | return $userGroup; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Shortcut to get UserGroup by id, mainly to get parent by Id. |
||
| 116 | * |
||
| 117 | * @param int $id |
||
| 118 | * @param bool $throwException |
||
| 119 | * |
||
| 120 | * @return UserGroup|false |
||
| 121 | * |
||
| 122 | * @throws NotFoundException |
||
| 123 | */ |
||
| 124 | 15 | public function findById($id, $throwException = false) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | 15 | public function create(ObjectInterface $object) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | 4 | public function update(ObjectInterface $object) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * {@inheritdoc} |
||
| 189 | */ |
||
| 190 | 15 | public function createOrUpdate(ObjectInterface $object) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * {@inheritdoc} |
||
| 205 | */ |
||
| 206 | 2 | View Code Duplication | public function remove(ObjectInterface $object) |
| 218 | } |
||
| 219 |
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.