Passed
Push — master ( 9b7ff5...a0d60e )
by Jean-Christophe
10:08
created

affectsOneToManyFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\OrmUtils;
6
use Ubiquity\log\Logger;
7
use Ubiquity\orm\parser\Reflexion;
8
9
/**
10
 * Used by DAO class, realize relations assignments.
11
 * Ubiquity\orm\traits$DAORelationsAssignmentsTrait
12
 * This class is part of Ubiquity
13
 *
14
 * @author jcheron <[email protected]>
15
 * @version 1.0.2
16
 *
17
 */
18
trait DAORelationsAssignmentsTrait {
19
20 48
	protected static function setToMember($member, $instance, $value, $accessor) {
21 48
		$instance->$accessor ( $value );
22 48
		$instance->_rest [$member] = $value;
23 48
	}
24
25 48
	protected static function getAccessor($member, $instance, $part) {
26 48
		$accessor = "set" . ucfirst ( $member );
27 48
		if (method_exists ( $instance, $accessor )) {
28 48
			return $accessor;
29
		}
30
		$class = get_class ( $instance );
31
		Logger::warn ( "DAO", "Missing modifier " . $accessor . " in " . $class, $part );
32
		return false;
33
	}
34
35 45
	protected static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache) {
36 45
		if (\sizeof ( $manyToOneQueries ) > 0) {
37
			self::_affectsObjectsFromArray ( $manyToOneQueries, $included, function ($object, $member, $manyToOneObjects, $fkField, $accessor) {
38 23
				self::affectsManyToOneFromArray ( $object, $member, $manyToOneObjects, $fkField, $accessor );
39 23
			}, 'getManyToOne' );
40
		}
41 45
		if (\sizeof ( $oneToManyQueries ) > 0) {
42
			self::_affectsObjectsFromArray ( $oneToManyQueries, $included, function ($object, $member, $relationObjects, $fkField, $accessor, $class, $prop) use ($classPropKey) {
43 37
				self::affectsOneToManyFromArray ( $object, $member, $relationObjects, $fkField, $accessor, $class, $prop, $classPropKey );
44 37
			}, 'getOneToMany' );
45
		}
46 45
		if (\sizeof ( $manyToManyParsers ) > 0) {
47 23
			self::_affectsManyToManyObjectsFromArray ( $className, $manyToManyParsers, $objects, $included, $useCache );
48
		}
49 45
	}
50
51 23
	private static function affectsManyToOneFromArray($object, $member, $manyToOneObjects, $fkField, $accessor) {
52 23
		if (isset ( $object->$fkField )) {
53 23
			$value = $manyToOneObjects [$object->$fkField];
54 23
			self::setToMember ( $member, $object, $value, $accessor );
55
		}
56 23
	}
57
58
	/**
59
	 *
60
	 * @param object $instance
61
	 * @param string $member
62
	 * @param array $array
63
	 * @param string $mappedByAccessor
64
	 * @param string $class
65
	 * @param \ReflectionProperty $prop
66
	 */
67 37
	private static function affectsOneToManyFromArray($instance, $member, $array = null, $mappedByAccessor = null, $accessor = "", $class = "", $prop = null, $classPropKey = null) {
68 37
		$ret = [ ];
69 37
		self::_getOneToManyFromArray ( $ret, $array, Reflexion::getPropValue ( $instance, $classPropKey ), $mappedByAccessor, $prop );
70 37
		self::setToMember ( $member, $instance, $ret, $accessor );
71 37
	}
72
73 42
	private static function _affectsObjectsFromArray($queries, $included, $affectsCallback, $part, $useCache = NULL) {
74 42
		$includedNext = false;
75 42
		foreach ( $queries as $key => $pendingRelationsRequest ) {
76 42
			list ( $class, $member, $fkField, $fkClass ) = \explode ( "|", $key );
77 42
			if (is_array ( $included )) {
78 2
				$includedNext = self::_getIncludedNext ( $included, $member );
79
			}
80 42
			$objectsParsers = $pendingRelationsRequest->getObjectsConditionParsers ();
81 42
			$prop = null;
82 42
			if ('getOneToMany' === $part) {
83 37
				$prop = OrmUtils::getFirstPropKey ( $fkClass );
84 37
				$fkField = "get" . ucfirst ( $fkField );
85
			}
86 42
			foreach ( $objectsParsers as $objectsConditionParser ) {
87 42
				$objectsConditionParser->compileParts ();
88 42
				$relationObjects = self::_getAll ( self::getDb ( $class ), $class, $objectsConditionParser->getConditionParser (), $includedNext, $useCache );
89 42
				$objects = $objectsConditionParser->getObjects ();
90 42
				if ($accessor = self::getAccessor ( $member, current ( $objects ), $part )) {
91 42
					foreach ( $objects as $object ) {
92 42
						$affectsCallback ( $object, $member, $relationObjects, $fkField, $accessor, $class, $prop );
93
					}
94
				}
95
			}
96
		}
97 42
	}
98
99 23
	private static function _affectsManyToManyObjectsFromArray($objectsClass, $parsers, $objects, $included, $useCache = NULL) {
100 23
		$includedNext = false;
101 23
		$prop = OrmUtils::getFirstPropKey ( $objectsClass );
102 23
		foreach ( $parsers as $key => $parser ) {
103 23
			list ( $class, $member ) = \explode ( "|", $key );
104 23
			if (is_array ( $included )) {
105 3
				$includedNext = self::_getIncludedNext ( $included, $member );
106
			}
107 23
			$myPkValues = [ ];
108 23
			$cParser = self::generateManyToManyParser ( $parser, $myPkValues );
109 23
			$relationObjects = self::_getAll ( self::getDb ( $class ), $class, $cParser, $includedNext, $useCache );
110 23
			if ($accessor = self::getAccessor ( $member, current ( $objects ), 'getManyToMany' )) {
111 23
				foreach ( $objects as $object ) {
112 23
					$pkV = Reflexion::getPropValue ( $object, $prop );
113 23
					if (isset ( $myPkValues [$pkV] )) {
114 23
						$ret = self::getManyToManyFromArrayIds ( $class, $relationObjects, $myPkValues [$pkV] );
115 23
						self::setToMember ( $member, $object, $ret, $accessor );
116
					}
117
				}
118
			}
119
		}
120 23
	}
121
122 37
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
123 37
		foreach ( $array as $element ) {
124 33
			$elementRef = $element->$elementAccessor ();
125 33
			if (($elementRef == $fkv) || (\is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
126 33
				$ret [] = $element;
127
			}
128
		}
129 37
	}
130
}
131
132