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 |
||
9 | class StringType implements IType |
||
10 | { |
||
11 | /** |
||
12 | * Gets the type code |
||
13 | * Note: implementation of IType::getTypeCode |
||
14 | * |
||
15 | * @return TypeCode |
||
16 | */ |
||
17 | public function getTypeCode() |
||
21 | |||
22 | /** |
||
23 | * Checks this type (String) is compatible with another type |
||
24 | * Note: implementation of IType::isCompatibleWith |
||
25 | * |
||
26 | * @param IType $type Type to check compatibility |
||
27 | * |
||
28 | * @return boolean |
||
29 | */ |
||
30 | public function isCompatibleWith(IType $type) |
||
34 | |||
35 | /** |
||
36 | * Validate a value in Astoria uri is in a format for this type |
||
37 | * Note: implementation of IType::validate |
||
38 | * |
||
39 | * @param string $value The value to validate |
||
40 | * @param string &$outValue The stripped form of $value that can |
||
41 | * be used in PHP expressions |
||
42 | * |
||
43 | * @return boolean |
||
44 | */ |
||
45 | public function validate($value, &$outValue) |
||
54 | |||
55 | /** |
||
56 | * Gets full name of this type in EDM namespace |
||
57 | * Note: implementation of IType::getFullTypeName |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getFullTypeName() |
||
65 | |||
66 | /** |
||
67 | * Converts the given string value to string type. |
||
68 | * |
||
69 | * @param string $stringValue value to convert. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | View Code Duplication | public function convert($stringValue) |
|
91 | |||
92 | /** |
||
93 | * Convert the given value to a form that can be used in OData uri. |
||
94 | * Note: The calling function should not pass null value, as this |
||
95 | * function will not perform any check for nullability |
||
96 | * |
||
97 | * @param mixed $value value to convert. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function convertToOData($value) |
||
105 | } |
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.