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 |
||
7 | class ContactsAPI extends API |
||
8 | { |
||
9 | /** |
||
10 | * @var Type\FolderIdType |
||
11 | */ |
||
12 | protected $folderId; |
||
13 | |||
14 | /** |
||
15 | * @return Type\FolderIdType |
||
16 | */ |
||
17 | 5 | public function getFolderId() |
|
25 | |||
26 | /** |
||
27 | * @param Type\FolderIdType $folderId |
||
28 | */ |
||
29 | 5 | public function setFolderId($folderId) |
|
33 | |||
34 | /** |
||
35 | * @param Type\FolderIdType $folderId |
||
36 | * @param array $options |
||
37 | * @return Type\ContactItemType[] |
||
38 | */ |
||
39 | 2 | public function getContacts($folderId = null, $options = array()) |
|
61 | |||
62 | /** |
||
63 | * @param Type\ItemIdType $itemId |
||
64 | * @return Type\ContactItemType |
||
65 | */ |
||
66 | 3 | public function getContact($itemId) |
|
70 | |||
71 | /** |
||
72 | * @param $contacts |
||
73 | * @param array $options |
||
74 | * @return Type\ItemIdType[] |
||
75 | */ |
||
76 | 5 | View Code Duplication | public function createContacts($contacts, $options = array()) |
94 | |||
95 | 4 | View Code Duplication | public function updateContactItem(Type\ItemIdType $itemId, $changes) |
115 | } |
||
116 |
If you implement
__call
and 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
__call
is implemented by a parent class and only the child class knows which methods exist: