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 |
||
| 12 | class MailboxManager |
||
| 13 | { |
||
| 14 | /** @var Registry */ |
||
| 15 | protected $registry; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param Registry $registry |
||
| 19 | */ |
||
| 20 | public function __construct(Registry $registry) |
||
| 21 | { |
||
| 22 | $this->registry = $registry; |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Returns a list of ids of mailboxes available to user logged under organization. |
||
| 27 | * |
||
| 28 | * @param User|integer $user User or user id |
||
| 29 | * @param Organization $organization |
||
| 30 | * |
||
| 31 | * @return array Array of ids |
||
| 32 | */ |
||
| 33 | View Code Duplication | public function findAvailableMailboxIds($user, $organization) |
|
| 34 | { |
||
| 35 | $mailboxes = $this->findAvailableMailboxes($user, $organization); |
||
| 36 | |||
| 37 | $ids = []; |
||
| 38 | foreach ($mailboxes as $mailbox) { |
||
| 39 | $ids[] = $mailbox->getId(); |
||
| 40 | } |
||
| 41 | |||
| 42 | return $ids; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Returns a list of mailboxes available to user logged under organization. |
||
| 47 | * |
||
| 48 | * @param User|integer $user User or user id |
||
| 49 | * @param Organization $organization|null |
||
|
|
|||
| 50 | * |
||
| 51 | * @return Collection|Mailbox[] Array or collection of Mailboxes |
||
| 52 | */ |
||
| 53 | public function findAvailableMailboxes($user, Organization $organization = null) |
||
| 54 | { |
||
| 55 | $qb = $this->registry->getRepository('OroEmailBundle:Mailbox') |
||
| 56 | ->createAvailableMailboxesQuery($user, $organization); |
||
| 57 | |||
| 58 | return $qb->getQuery()->getResult(); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param User $user |
||
| 63 | * @param Organization $organization |
||
| 64 | * |
||
| 65 | * @return EmailOrigin |
||
| 66 | */ |
||
| 67 | public function findAvailableOrigins(User $user, Organization $organization) |
||
| 75 | } |
||
| 76 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.