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 ContactsAPI extends API |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Type\FolderIdType |
||
| 12 | */ |
||
| 13 | protected $folderId; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @return Type\FolderIdType |
||
| 17 | */ |
||
| 18 | 2 | public function getFolderId() |
|
| 19 | { |
||
| 20 | 2 | if (!$this->folderId) { |
|
| 21 | 2 | $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId(); |
|
|
|
|||
| 22 | } |
||
| 23 | |||
| 24 | 2 | return $this->folderId; |
|
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param Type\FolderIdType $folderId |
||
| 29 | */ |
||
| 30 | 2 | public function setFolderId($folderId) |
|
| 31 | { |
||
| 32 | 2 | $this->folderId = $folderId; |
|
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param Type\FolderIdType $folderId |
||
| 37 | * @param array $options |
||
| 38 | * @return Type\ContactItemType[] |
||
| 39 | */ |
||
| 40 | public function getContacts($folderId = null, $options = array()) |
||
| 41 | { |
||
| 42 | if (!$folderId) { |
||
| 43 | $folderId = $this->getFolderId(); |
||
| 44 | } |
||
| 45 | |||
| 46 | $request = array( |
||
| 47 | 'Traversal' => 'Shallow', |
||
| 48 | 'ItemShape' => array( |
||
| 49 | 'BaseShape' => 'AllProperties' |
||
| 50 | ), |
||
| 51 | 'ParentFolderIds' => array( |
||
| 52 | 'FolderId' => $folderId->toXmlObject() |
||
| 53 | ) |
||
| 54 | ); |
||
| 55 | |||
| 56 | $request = array_replace_recursive($request, $options); |
||
| 57 | |||
| 58 | $request = Type::buildFromArray($request); |
||
| 59 | |||
| 60 | return $this->getClient()->FindItem($request); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Type\ItemIdType $itemId |
||
| 65 | * @return Type\ContactItemType |
||
| 66 | */ |
||
| 67 | 2 | public function getContact($itemId) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param $contacts |
||
| 74 | * @param array $options |
||
| 75 | * @return Type\ItemIdType[] |
||
| 76 | */ |
||
| 77 | 2 | View Code Duplication | public function createContacts($contacts, $options = array()) |
| 95 | |||
| 96 | 1 | View Code Duplication | public function updateContactItem(Type\ItemIdType $itemId, $changes) |
| 116 | } |
||
| 117 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: