Passed
Push — master ( e1ca0a...c1379c )
by Jean-Christophe
04:54
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 36

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 30
ccs 22
cts 22
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 36
nop 10
crap 11

How to fix   Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\db\SqlUtils;
6
use Ubiquity\events\DAOEvents;
7
use Ubiquity\events\EventsManager;
8
use Ubiquity\log\Logger;
9
use Ubiquity\orm\OrmUtils;
10
use Ubiquity\orm\parser\ConditionParser;
11
use Ubiquity\orm\parser\Reflexion;
12
13
/**
14
 *
15
 * @author jc
16
 * @property \Ubiquity\db\Database $db
17
 *
18
 */
19
trait DAOCoreTrait {
20
21
	abstract protected static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache);
22
23
	abstract protected static function prepareManyToMany(&$ret, $instance, $member, $annot = null);
24
25
	abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray);
26
27
	abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null);
28
29
	abstract protected static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields);
30
31
	abstract protected static function getIncludedForStep($included);
32
33 10
	private static function _getOneToManyFromArray(&$ret, $array, $fkv, $elementAccessor, $prop) {
34 10
		foreach ( $array as $element ) {
35 9
			$elementRef = $element->$elementAccessor ();
36 9
			if (($elementRef == $fkv) || (is_object ( $elementRef ) && Reflexion::getPropValue ( $elementRef, $prop ) == $fkv)) {
37 9
				$ret [] = $element;
38
			}
39
		}
40 10
	}
41
42
	/*
43
	 * private static function _getOneToManyFromArray($array, $fkv, $mappedBy,$prop) {
44
	 * $elementAccessor="get" . ucfirst($mappedBy);
45
	 * return array_filter($array,function($element) use($elementAccessor,$fkv,$prop){
46
	 * $elementRef=$element->$elementAccessor();
47
	 * return ($elementRef == $fkv) || (is_object($elementRef) && Reflexion::getPropValue($elementRef,$prop) == $fkv);
48
	 * });
49
	 * }
50
	 */
51
	private static function getManyToManyFromArray($instance, $array, $class, $parser) {
52
		$ret = [ ];
53
		$continue = true;
54
		$accessorToMember = "get" . ucfirst ( $parser->getInversedBy () );
55
		$myPkAccessor = "get" . ucfirst ( $parser->getMyPk () );
56
57
		if (! method_exists ( $instance, $myPkAccessor )) {
58
			Logger::warn ( "DAO", "L'accesseur au membre clé primaire " . $myPkAccessor . " est manquant pour " . $class, "ManyToMany" );
59
		}
60
		if (sizeof ( $array ) > 0) {
61
			$continue = method_exists ( current ( $array ), $accessorToMember );
62
		}
63
		if ($continue) {
64
			foreach ( $array as $targetEntityInstance ) {
65
				$instances = $targetEntityInstance->$accessorToMember ();
66
				if (is_array ( $instances )) {
67
					foreach ( $instances as $inst ) {
68
						if ($inst->$myPkAccessor () == $instance->$myPkAccessor ())
69
							array_push ( $ret, $targetEntityInstance );
70
					}
71
				}
72
			}
73
		} else {
74
			Logger::warn ( "DAO", "L'accesseur au membre " . $parser->getInversedBy () . " est manquant pour " . $parser->getTargetEntity (), "ManyToMany" );
75
		}
76
		return $ret;
77
	}
78
79 11
	protected static function _getOne($className, ConditionParser $conditionParser, $included, $useCache) {
80 11
		$conditionParser->limitOne ();
81 11
		$retour = self::_getAll ( $className, $conditionParser, $included, $useCache );
82 11
		if (sizeof ( $retour ) < 1) {
83 2
			return null;
84
		}
85 11
		$result = \current ( $retour );
86 11
		EventsManager::trigger ( DAOEvents::GET_ONE, $result, $className );
87 11
		return $result;
88
	}
89
90
	/**
91
	 *
92
	 * @param string $className
93
	 * @param ConditionParser $conditionParser
94
	 * @param boolean|array $included
95
	 * @param boolean|null $useCache
96
	 * @return array
97
	 */
98 18
	protected static function _getAll($className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
99 18
		$included = self::getIncludedForStep ( $included );
100 18
		$objects = array ();
101 18
		$invertedJoinColumns = null;
102 18
		$oneToManyFields = null;
103 18
		$manyToManyFields = null;
104
105 18
		$metaDatas = OrmUtils::getModelMetadata ( $className );
106 18
		$tableName = $metaDatas ["#tableName"];
107 18
		$hasIncluded = $included || (is_array ( $included ) && sizeof ( $included ) > 0);
108 18
		if ($hasIncluded) {
109 11
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
110
		}
111 18
		$condition = SqlUtils::checkWhere ( $conditionParser->getCondition () );
112 18
		$members = \array_diff ( $metaDatas ["#fieldNames"], $metaDatas ["#notSerializable"] );
113 18
		$query = self::$db->prepareAndExecute ( $tableName, $condition, $members, $conditionParser->getParams (), $useCache );
114 18
		$oneToManyQueries = [ ];
115 18
		$manyToOneQueries = [ ];
116 18
		$manyToManyParsers = [ ];
117 18
		$propsKeys = OrmUtils::getPropKeys ( $className );
118 18
		$accessors = OrmUtils::getAccessors ( $className, $members );
119 18
		$fields = array_flip ( $members );
120 18
		foreach ( $query as $row ) {
121 18
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToOneQueries, $manyToManyParsers, $accessors, $fields );
122 18
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
123 18
			$objects [$key] = $object;
124
		}
125 18
		if ($hasIncluded) {
126 11
			$classPropKey = OrmUtils::getFirstPropKey ( $className );
127 11
			self::_affectsRelationObjects ( $className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
128
		}
129 18
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
130 18
		return $objects;
131
	}
132
133
	/**
134
	 *
135
	 * @param array $row
136
	 * @param string $className
137
	 * @param array $invertedJoinColumns
138
	 * @param array $oneToManyFields
139
	 * @param array $oneToManyQueries
140
	 * @param array $manyToOneQueries
141
	 * @param array $manyToManyParsers
142
	 * @param array $accessors
143
	 * @param array $fields
144
	 * @return object
145
	 */
146 18
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToOneQueries, &$manyToManyParsers, &$accessors, &$fields) {
147 18
		$o = new $className ();
148 18
		foreach ( $row as $k => $v ) {
149 18
			if (isset ( $fields [$k] )) {
150 18
				if (isset ( $accessors [$k] )) {
151 18
					$accesseur = $accessors [$k];
152 18
					$o->$accesseur ( $v );
153 17
				} elseif (isset ( $accessors [$fields [$k]] )) {
154 17
					$accesseur = $accessors [$fields [$k]];
155 17
					$o->$accesseur ( $v );
156
				}
157
			}
158 18
			$o->_rest [$k] = $v;
159 18
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
160 6
				$fk = "_" . $k;
161 6
				$o->$fk = $v;
162 13
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
163
			}
164
		}
165 18
		if (isset ( $oneToManyFields )) {
166 10
			foreach ( $oneToManyFields as $k => $annot ) {
167 10
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
168
			}
169
		}
170 18
		if (isset ( $manyToManyFields )) {
171 5
			foreach ( $manyToManyFields as $k => $annot ) {
172 5
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
173
			}
174
		}
175 18
		return $o;
176
	}
177
178 1
	private static function parseKey(&$keyValues, $className) {
179 1
		if (! is_array ( $keyValues )) {
180 1
			if (strrpos ( $keyValues, "=" ) === false && strrpos ( $keyValues, ">" ) === false && strrpos ( $keyValues, "<" ) === false) {
181 1
				$keyValues = "`" . OrmUtils::getFirstKey ( $className ) . "`='" . $keyValues . "'";
182
			}
183
		}
184 1
	}
185
}
186
187