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 |
||
| 12 | class midgard_reflection_property |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var midgard\portable\mapping\classmetadata |
||
| 16 | */ |
||
| 17 | private $cm; |
||
| 18 | |||
| 19 | 12 | public function __construct($mgdschema_class) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $property The property name |
||
| 32 | * @param boolean $metadata Check metadata properties instead |
||
| 33 | * @return boolean Indicating existence |
||
| 34 | */ |
||
| 35 | 1 | public function property_exists($property, $metadata = false) |
|
| 36 | { |
||
| 37 | 1 | if ($metadata) { |
|
| 38 | 1 | $property = 'metadata_' . $property; |
|
| 39 | 1 | } |
|
| 40 | 1 | return ($this->cm->hasField($property) || $this->cm->hasAssociation($property) || array_key_exists($property, $this->cm->midgard['field_aliases'])); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns field's description, if any |
||
| 45 | * |
||
| 46 | * @param string $property |
||
| 47 | * @return string|NULL |
||
| 48 | */ |
||
| 49 | 1 | public function description($property) |
|
| 57 | |||
| 58 | 9 | public function get_mapping($property) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Is this field a link or not |
||
| 68 | * |
||
| 69 | * @param string $property |
||
| 70 | * @return boolean |
||
| 71 | */ |
||
| 72 | 7 | public function is_link($property) |
|
| 79 | |||
| 80 | 7 | public function is_special_link($property) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the classname for the link target |
||
| 91 | * |
||
| 92 | * @param string $property |
||
| 93 | * @return string|NULL |
||
| 94 | */ |
||
| 95 | 7 | View Code Duplication | public function get_link_name($property) |
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the target field name |
||
| 113 | * |
||
| 114 | * @param string $property |
||
| 115 | * @return string|NULL |
||
| 116 | */ |
||
| 117 | 2 | View Code Duplication | public function get_link_target($property) |
| 132 | |||
| 133 | /** |
||
| 134 | * Returns field type constant |
||
| 135 | * |
||
| 136 | * @param string $property |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | 1 | public function get_midgard_type($property) |
|
| 150 | } |
||
| 151 |
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.