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 |
||
37 | class IdentityDictionary extends Dictionary |
||
38 | { |
||
39 | |||
40 | /** |
||
41 | * This method returns the element that has the passed |
||
42 | * key as a reference (has to be an object) from the |
||
43 | * Dictionary. |
||
44 | * |
||
45 | * @param object $key Holds the key to the key of the element to return |
||
46 | * |
||
47 | * @return mixed The requested value |
||
48 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
49 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL |
||
50 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
||
51 | */ |
||
52 | View Code Duplication | public function get($key) |
|
73 | |||
74 | /** |
||
75 | * This method checks if the element that has the passed |
||
76 | * key as a reference (has to be an object) exists in |
||
77 | * the Dictionary. |
||
78 | * |
||
79 | * @param object $key Holds the reference to the key of the element that should exists in the Dictionary |
||
80 | * |
||
81 | * @return boolean Returns TRUE if an element with the passed key exists in the Dictionary |
||
82 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
83 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
84 | */ |
||
85 | View Code Duplication | public function exists($key) |
|
104 | |||
105 | /** |
||
106 | * This method removes the element that has the passed |
||
107 | * key as a reference (has to be an object) from the |
||
108 | * Dictionary. |
||
109 | * |
||
110 | * @param object $key Holds the reference of the key of the element to remove |
||
111 | * |
||
112 | * @return void |
||
113 | * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an object |
||
114 | * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL |
||
115 | * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Dictionary |
||
116 | */ |
||
117 | View Code Duplication | public function remove($key) |
|
138 | } |
||
139 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.