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 |
||
| 6 | class DataObject |
||
|
|
|||
| 7 | { |
||
| 8 | /** |
||
| 9 | * Preferred order of properties, to be set by subclasses |
||
| 10 | */ |
||
| 11 | protected $order; |
||
| 12 | /** |
||
| 13 | * Model instance which created this object |
||
| 14 | */ |
||
| 15 | protected $model; |
||
| 16 | /** |
||
| 17 | * EasyRdf resource representing this object |
||
| 18 | */ |
||
| 19 | protected $resource; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Initializes the DataObject |
||
| 23 | * @param Model $model |
||
| 24 | * @param EasyRdf_Resource $resource |
||
| 25 | */ |
||
| 26 | View Code Duplication | public function __construct($model, $resource) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Generates and makes a query into a external vocabulary for an exact |
||
| 39 | * match for a particular concept. |
||
| 40 | * @param Vocabulary $exvoc external vocabulary to query |
||
| 41 | * @param string $exuri resource URI |
||
| 42 | * @param string $lang language of label to query for |
||
| 43 | * @return EasyRdf_Literal label, or null if not found in vocabulary |
||
| 44 | */ |
||
| 45 | protected function getExternalLabel($exvoc, $exuri, $lang) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Generates and makes a query into a external vocabulary for the notation of an exact |
||
| 58 | * match for a particular concept. |
||
| 59 | * @param Vocabulary $exvoc external vocabulary to query |
||
| 60 | * @param string $exuri resource URI |
||
| 61 | */ |
||
| 62 | protected function getExternalNotation($exvoc, $exuri) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sorting the result list to a arbitrary order defined below in mycompare() |
||
| 74 | * @param array $sortable |
||
| 75 | */ |
||
| 76 | protected function arbitrarySort($sortable) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Compares the given objects and returns -1 or 1 depending which ought to be first. |
||
| 93 | * $order defines the priorities of the different properties possible in the array. |
||
| 94 | * @param string $a the first item to be compared |
||
| 95 | * @param string $b the second item to be compared |
||
| 96 | */ |
||
| 97 | protected function mycompare($a, $b) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Getter function to retrieve the ui language from the locale. |
||
| 124 | */ |
||
| 125 | public function getEnvLang() |
||
| 130 | } |
||
| 131 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.