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 |
||
| 8 | class MailingList |
||
| 9 | { |
||
| 10 | |||
| 11 | /** @var Mailchimp */ |
||
| 12 | protected $api; |
||
| 13 | |||
| 14 | /** @var string */ |
||
| 15 | protected $id; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | protected $support; |
||
| 19 | |||
| 20 | public function __construct(Mailchimp $api, Support $support, string $id) |
||
| 26 | |||
| 27 | public function id() : string |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @throws \Exception |
||
| 34 | */ |
||
| 35 | public function exists() : bool |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @throws \Exception |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function isOnList(string $email) : bool |
|
|
1 ignored issue
–
show
|
|||
| 57 | { |
||
| 58 | try { |
||
| 59 | $this->api->get('lists/'.$this->id().'/members/'.$this->support->hashEmail($email)); |
||
| 60 | |||
| 61 | return true; |
||
| 62 | } catch (\Exception $e) { |
||
| 63 | # Email address isn't on this list |
||
| 64 | if (str_contains($e->getMessage(), 'Resource Not Found')) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | |||
| 68 | throw $e; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $email |
||
| 74 | * @param string $firstName |
||
| 75 | * @param string $lastName |
||
| 76 | * @param array|object $interests Properties/Keys are interest ids and values are boolean |
||
| 77 | * @param array $extras Additional fields to be passed to the Mailchimp API |
||
| 78 | * |
||
| 79 | * @throws \Exception |
||
| 80 | */ |
||
| 81 | public function subscribe( |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @link http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash |
||
| 113 | * |
||
| 114 | * @throws EmailAddressNotSubscribed |
||
| 115 | * @throws \Exception |
||
| 116 | */ |
||
| 117 | View Code Duplication | public function subscriberInfo(string $email) : Collection |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Updates a subscriber if the email address is already |
||
| 133 | * on the list, or create the subscriber |
||
| 134 | * |
||
| 135 | * @param string $email |
||
| 136 | * @param string $firstName |
||
| 137 | * @param string $lastName |
||
| 138 | * @param array|object $interests Properties/Keys are interest ids and values are boolean |
||
| 139 | * @param array $extras Additional fields to be passed to the Mailchimp API |
||
| 140 | * |
||
| 141 | * @link https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash |
||
| 142 | * |
||
| 143 | * @throws EmailAddressNotSubscribed |
||
| 144 | * @throws \Exception |
||
| 145 | */ |
||
| 146 | public function updateSubscriber( |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @throws EmailAddressNotSubscribed |
||
| 179 | * @throws \Exception |
||
| 180 | */ |
||
| 181 | public function unsubscribe(string $email) : bool |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @throws \Exception |
||
| 201 | */ |
||
| 202 | public function interestCategories() : array |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @throws \Exception |
||
| 211 | */ |
||
| 212 | public function interests(string $interestCategoryId) : array |
||
| 218 | } |
||
| 219 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..