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 |
||
| 42 | class PageController extends Controller { |
||
| 43 | /** @var int */ |
||
| 44 | const PAGE_LIMIT = 5; |
||
| 45 | |||
| 46 | /** @var IJobList */ |
||
| 47 | protected $jobList; |
||
| 48 | |||
| 49 | /** @var IDBConnection */ |
||
| 50 | protected $connection; |
||
| 51 | |||
| 52 | /** @var IGroupManager */ |
||
| 53 | protected $groupManager; |
||
| 54 | |||
| 55 | /** @var IUserManager */ |
||
| 56 | protected $userManager; |
||
| 57 | |||
| 58 | /** @var IL10N */ |
||
| 59 | protected $l; |
||
| 60 | |||
| 61 | /** @var Manager */ |
||
| 62 | protected $manager; |
||
| 63 | |||
| 64 | /** @var IConfig */ |
||
| 65 | protected $config; |
||
| 66 | |||
| 67 | /** @var IUserSession */ |
||
| 68 | protected $userSession; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param string $AppName |
||
| 72 | * @param IRequest $request |
||
| 73 | * @param IDBConnection $connection |
||
| 74 | * @param IGroupManager $groupManager |
||
| 75 | * @param IUserManager $userManager |
||
| 76 | * @param IJobList $jobList |
||
| 77 | * @param IL10N $l |
||
| 78 | * @param Manager $manager |
||
| 79 | * @param IConfig $config |
||
| 80 | * @param IUserSession $userSession |
||
| 81 | */ |
||
| 82 | 24 | public function __construct($AppName, |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @NoAdminRequired |
||
| 106 | * @NoCSRFRequired |
||
| 107 | * |
||
| 108 | * @param int $offset |
||
| 109 | * @return JSONResponse |
||
| 110 | */ |
||
| 111 | 6 | public function get($offset = 0) { |
|
| 112 | 6 | $rows = $this->manager->getAnnouncements(self::PAGE_LIMIT, $offset); |
|
| 113 | |||
| 114 | 6 | $announcements = []; |
|
| 115 | 6 | foreach ($rows as $row) { |
|
| 116 | 3 | $displayName = $row['author']; |
|
| 117 | 3 | $user = $this->userManager->get($displayName); |
|
| 118 | 3 | if ($user instanceof IUser) { |
|
|
|
|||
| 119 | 1 | $displayName = $user->getDisplayName(); |
|
| 120 | } |
||
| 121 | |||
| 122 | 3 | $row['author_id'] = $row['author']; |
|
| 123 | 3 | $row['author'] = $displayName; |
|
| 124 | |||
| 125 | 3 | $announcements[] = $row; |
|
| 126 | } |
||
| 127 | |||
| 128 | 6 | return new JSONResponse($announcements); |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @NoAdminRequired |
||
| 133 | * |
||
| 134 | * @param string $subject |
||
| 135 | * @param string $message |
||
| 136 | * @param string[] $groups, |
||
| 137 | * @param bool $activities |
||
| 138 | * @param bool $notifications |
||
| 139 | * @param bool $comments |
||
| 140 | * @return JSONResponse |
||
| 141 | */ |
||
| 142 | 8 | public function add($subject, $message, array $groups, $activities, $notifications, $comments) { |
|
| 143 | 8 | if (!$this->manager->checkIsAdmin()) { |
|
| 144 | 1 | return new JSONResponse( |
|
| 145 | 1 | ['message' => 'Logged in user must be an admin'], |
|
| 146 | 1 | Http::STATUS_FORBIDDEN |
|
| 147 | ); |
||
| 148 | } |
||
| 149 | |||
| 150 | 7 | $timeStamp = time(); |
|
| 151 | try { |
||
| 152 | 7 | $announcement = $this->manager->announce($subject, $message, $this->userSession->getUser()->getUID(), $timeStamp, $groups, $comments); |
|
| 153 | 2 | } catch (\InvalidArgumentException $e) { |
|
| 154 | 2 | return new JSONResponse( |
|
| 155 | 2 | ['error' => (string)$this->l->t('The subject is too long or empty')], |
|
| 156 | 2 | Http::STATUS_BAD_REQUEST |
|
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | 5 | if ($activities || $notifications) { |
|
| 161 | 3 | $this->jobList->add('OCA\AnnouncementCenter\BackgroundJob', [ |
|
| 162 | 3 | 'id' => $announcement['id'], |
|
| 163 | 3 | 'activities' => $activities, |
|
| 164 | 3 | 'notifications' => $notifications, |
|
| 165 | ]); |
||
| 166 | } |
||
| 167 | |||
| 168 | 5 | $announcement['notifications'] = $notifications; |
|
| 169 | 5 | $announcement['author_id'] = $announcement['author']; |
|
| 170 | 5 | $announcement['author'] = $this->userManager->get($announcement['author_id'])->getDisplayName(); |
|
| 171 | |||
| 172 | 5 | return new JSONResponse($announcement); |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @NoAdminRequired |
||
| 177 | * |
||
| 178 | * @param int $id |
||
| 179 | * @return Response |
||
| 180 | */ |
||
| 181 | 2 | View Code Duplication | public function delete($id) { |
| 193 | |||
| 194 | /** |
||
| 195 | * @NoAdminRequired |
||
| 196 | * |
||
| 197 | * @param int $id |
||
| 198 | * @return Response |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function removeNotifications($id) { |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @NoAdminRequired |
||
| 215 | * @NoCSRFRequired |
||
| 216 | * |
||
| 217 | * @param int $announcement |
||
| 218 | * @return TemplateResponse |
||
| 219 | */ |
||
| 220 | 3 | public function index($announcement = 0) { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @NoAdminRequired |
||
| 235 | * |
||
| 236 | * @param string $pattern |
||
| 237 | * @return JSONResponse |
||
| 238 | */ |
||
| 239 | 3 | public function searchGroups($pattern) { |
|
| 255 | } |
||
| 256 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.