Test Failed
Pull Request — master (#23)
by Jean-Christophe
12:28
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 40

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 11

Importance

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