Code Duplication    Length = 35-35 lines in 2 locations

model/DataObject.php 1 location

@@ 3072-3106 (lines=35) @@
3069
	 * @param $fieldName string
3070
	 * @return string | null - will return null on a missing value
3071
	 */
3072
	public function relField($fieldName) {
3073
		$component = $this;
3074
3075
		// We're dealing with relations here so we traverse the dot syntax
3076
		if(strpos($fieldName, '.') !== false) {
3077
			$relations = explode('.', $fieldName);
3078
			$fieldName = array_pop($relations);
3079
			foreach($relations as $relation) {
3080
				// Inspect $component for element $relation
3081
				if($component->hasMethod($relation)) {
3082
					// Check nested method
3083
					$component = $component->$relation();
3084
				} elseif($component instanceof SS_List) {
3085
					// Select adjacent relation from DataList
3086
					$component = $component->relation($relation);
3087
				} elseif($component instanceof DataObject
3088
					&& ($dbObject = $component->dbObject($relation))
3089
				) {
3090
					// Select db object
3091
					$component = $dbObject;
3092
				} else {
3093
					user_error("$relation is not a relation/field on ".get_class($component), E_USER_ERROR);
3094
				}
3095
			}
3096
		}
3097
3098
		// Bail if the component is null
3099
		if(!$component) {
3100
			return null;
3101
		}
3102
		if($component->hasMethod($fieldName)) {
3103
			return $component->$fieldName();
3104
		}
3105
		return $component->$fieldName;
3106
	}
3107
3108
	/**
3109
	 * Temporary hack to return an association name, based on class, to get around the mangle

model/versioning/Versioned.php 1 location

@@ 2529-2563 (lines=35) @@
2526
	 * @param $fieldName string
2527
	 * @return string | null - will return null on a missing value
2528
	 */
2529
	public function relField($fieldName) {
2530
		$component = $this;
2531
2532
		// We're dealing with relations here so we traverse the dot syntax
2533
		if(strpos($fieldName, '.') !== false) {
2534
			$relations = explode('.', $fieldName);
2535
			$fieldName = array_pop($relations);
2536
			foreach($relations as $relation) {
2537
				// Inspect $component for element $relation
2538
				if($component->hasMethod($relation)) {
2539
					// Check nested method
2540
						$component = $component->$relation();
2541
				} elseif($component instanceof SS_List) {
2542
					// Select adjacent relation from DataList
2543
						$component = $component->relation($relation);
2544
				} elseif($component instanceof DataObject
2545
					&& ($dbObject = $component->dbObject($relation))
2546
				) {
2547
					// Select db object
2548
					$component = $dbObject;
2549
				} else {
2550
					user_error("$relation is not a relation/field on ".get_class($component), E_USER_ERROR);
2551
				}
2552
			}
2553
		}
2554
2555
		// Bail if the component is null
2556
		if(!$component) {
2557
			return null;
2558
		}
2559
		if ($component->hasMethod($fieldName)) {
2560
			return $component->$fieldName();
2561
		}
2562
		return $component->$fieldName;
2563
	}
2564
}
2565