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) |
|
| 20 | { |
||
| 21 | // we might get a proxy class, so we need to translate |
||
| 22 | 12 | $classname = ClassUtils::getRealClass($mgdschema_class); |
|
| 23 | 12 | $cmf = connection::get_em()->getMetadataFactory(); |
|
| 24 | 12 | if (!$cmf->hasMetadataFor($classname)) { |
|
| 25 | 6 | $classname = 'midgard:' . $mgdschema_class; |
|
| 26 | 6 | } |
|
| 27 | 12 | $this->cm = $cmf->getMetadataFor($classname); |
|
| 28 | 12 | } |
|
| 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) |
|
| 50 | { |
||
| 51 | 1 | if (!$this->cm->hasField($property)) { |
|
| 52 | 1 | return null; |
|
| 53 | } |
||
| 54 | 1 | $mapping = $this->cm->getFieldMapping($property); |
|
| 55 | 1 | return $mapping['midgard:description']; |
|
| 56 | } |
||
| 57 | |||
| 58 | 9 | public function get_mapping($property) |
|
| 59 | { |
||
| 60 | 9 | if (!$this->cm->hasField($property)) { |
|
| 61 | 8 | return null; |
|
| 62 | } |
||
| 63 | 4 | return $this->cm->getFieldMapping($property); |
|
| 64 | } |
||
| 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) |
|
| 73 | { |
||
| 74 | 7 | if ($this->cm->hasAssociation($property)) { |
|
| 75 | 6 | return true; |
|
| 76 | } |
||
| 77 | 2 | return $this->is_special_link($property); |
|
| 78 | } |
||
| 79 | |||
| 80 | 7 | public function is_special_link($property) |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns the classname for the link target |
||
| 91 | * |
||
| 92 | * @param string $property |
||
| 93 | * @return string|NULL |
||
| 94 | */ |
||
| 95 | 7 | public function get_link_name($property) |
|
| 96 | { |
||
| 97 | 7 | if ($this->cm->hasAssociation($property)) { |
|
| 98 | 6 | $mapping = $this->cm->getAssociationMapping($property); |
|
| 99 | 6 | return $mapping['midgard:link_name']; |
|
| 100 | } |
||
| 101 | 2 | $mapping = $this->get_mapping($property); |
|
| 102 | 2 | if (is_null($mapping)) { |
|
| 103 | 1 | return null; |
|
| 104 | } |
||
| 105 | 2 | if (isset($mapping["noidlink"]["target"])) { |
|
| 106 | 1 | return $mapping["noidlink"]["target"]; |
|
| 107 | } |
||
| 108 | 1 | return null; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Returns the target field name |
||
| 113 | * |
||
| 114 | * @param string $property |
||
| 115 | * @return string|NULL |
||
| 116 | */ |
||
| 117 | 2 | public function get_link_target($property) |
|
| 118 | { |
||
| 119 | 2 | if ($this->cm->hasAssociation($property)) { |
|
| 120 | 1 | $mapping = $this->cm->getAssociationMapping($property); |
|
| 121 | 1 | return $mapping['midgard:link_target']; |
|
| 122 | } |
||
| 123 | 2 | $mapping = $this->get_mapping($property); |
|
| 124 | 2 | if (is_null($mapping)) { |
|
| 125 | 1 | return null; |
|
| 126 | } |
||
| 127 | 2 | if (isset($mapping["noidlink"]["field"])) { |
|
| 128 | 1 | return $mapping["noidlink"]["field"]; |
|
| 129 | } |
||
| 130 | 1 | return null; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Returns field type constant |
||
| 135 | * |
||
| 136 | * @param string $property |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | 1 | public function get_midgard_type($property) |
|
| 149 | } |
||
| 150 | } |
||
| 151 |