Passed
Push — master ( e48b7c...1886e0 )
by Jean-Christophe
25:18 queued 16:06
created

DAOCoreTrait::loadObjectFromRow()   B

Complexity

Conditions 11
Paths 40

Size

Total Lines 31
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 31
ccs 22
cts 22
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\orm\OrmUtils;
9
use Ubiquity\orm\parser\ConditionParser;
10
use Ubiquity\orm\parser\Reflexion;
11
use Ubiquity\db\Database;
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.1.1
20
 *
21
 * @property array $db
22
 * @property boolean $useTransformers
23
 * @property string $transformerOp
24
 *
25
 */
26
trait DAOCoreTrait {
27
	protected static $accessors = [ ];
28
	protected static $fields = [ ];
29
30
	abstract protected static function _affectsRelationObjects($className, $classPropKey, $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache): void;
31
32
	abstract protected static function prepareManyToMany(&$ret, $instance, $member, $annot = null);
33
34
	abstract protected static function prepareManyToOne(&$ret, $instance, $value, $fkField, $annotationArray);
35
36
	abstract protected static function prepareOneToMany(&$ret, $instance, $member, $annot = null);
37
38
	abstract protected static function _initRelationFields($included, $metaDatas, &$invertedJoinColumns, &$oneToManyFields, &$manyToManyFields): void;
39
40
	abstract protected static function getIncludedForStep($included);
41
42
	abstract protected static function getDb($model);
43
44 16
	protected static function getClass_($instance) {
45 16
		if (is_object ( $instance )) {
46 13
			return get_class ( $instance );
47
		}
48 3
		return $instance [0];
49
	}
50
51
	protected static function getInstance_($instance) {
52
		if (\is_object ( $instance )) {
53
			return $instance;
54
		}
55
		return $instance [0];
56
	}
57
58
	protected static function getValue_($instance, $member) {
59
		if (\is_object ( $instance )) {
60
			return Reflexion::getMemberValue ( $instance, $member );
61
		}
62
		return $instance [1];
63
	}
64
65 11
	protected static function getFirstKeyValue_($instance) {
66 11
		if (\is_object ( $instance )) {
67 9
			return OrmUtils::getFirstKeyValue ( $instance );
68
		}
69 2
		return $instance [1];
70
	}
71
72 39
	protected static function _getOne(Database $db, $className, ConditionParser $conditionParser, $included, $useCache) {
73 39
		$conditionParser->limitOne ();
74 39
		$included = self::getIncludedForStep ( $included );
75 39
		$object = null;
76 39
		$invertedJoinColumns = null;
77 39
		$oneToManyFields = null;
78 39
		$manyToManyFields = null;
79
80 39
		$metaDatas = OrmUtils::getModelMetadata ( $className );
81 39
		$tableName = $metaDatas ['#tableName'];
82 39
		$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0);
83 39
		if ($hasIncluded) {
84 27
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
85
		}
86 39
		$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ];
87 39
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
88 39
		if ($query && \sizeof ( $query ) > 0) {
89 39
			$oneToManyQueries = [ ];
90 39
			$manyToOneQueries = [ ];
91 39
			$manyToManyParsers = [ ];
92 39
			$accessors = $metaDatas ['#accessors'];
93 39
			$object = self::loadObjectFromRow ( \current ( $query ), $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers );
94 39
			if ($hasIncluded) {
95 27
				self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, [ $object ], $included, $useCache );
96
			}
97 39
			EventsManager::trigger ( DAOEvents::GET_ONE, $object, $className );
98
		}
99 39
		return $object;
100
	}
101
102
	/**
103
	 *
104
	 * @param Database $db
105
	 * @param string $className
106
	 * @param ConditionParser $conditionParser
107
	 * @param boolean|array $included
108
	 * @param boolean|null $useCache
109
	 * @return array
110
	 */
111 56
	protected static function _getAll(Database $db, $className, ConditionParser $conditionParser, $included = true, $useCache = NULL) {
112 56
		$included = self::getIncludedForStep ( $included );
113 56
		$objects = array ();
114 56
		$invertedJoinColumns = null;
115 56
		$oneToManyFields = null;
116 56
		$manyToManyFields = null;
117
118 56
		$metaDatas = OrmUtils::getModelMetadata ( $className );
119 56
		$tableName = $metaDatas ['#tableName'];
120 56
		$hasIncluded = $included || (\is_array ( $included ) && \sizeof ( $included ) > 0);
121 56
		if ($hasIncluded) {
122 24
			self::_initRelationFields ( $included, $metaDatas, $invertedJoinColumns, $oneToManyFields, $manyToManyFields );
123
		}
124 56
		$transformers = $metaDatas ['#transformers'] [self::$transformerOp] ?? [ ];
125 56
		$query = $db->prepareAndExecute ( $tableName, SqlUtils::checkWhere ( $conditionParser->getCondition () ), self::getFieldList ( $tableName, $metaDatas ), $conditionParser->getParams (), $useCache );
126 56
		$oneToManyQueries = [ ];
127 56
		$manyToOneQueries = [ ];
128 56
		$manyToManyParsers = [ ];
129 56
		$propsKeys = OrmUtils::getPropKeys ( $className );
130 56
		$accessors = $metaDatas ['#accessors'];
131 56
		foreach ( $query as $row ) {
132 56
			$object = self::loadObjectFromRow ( $row, $className, $invertedJoinColumns, $manyToOneQueries, $oneToManyFields, $manyToManyFields, $oneToManyQueries, $manyToManyParsers, $accessors, $transformers );
133 56
			$key = OrmUtils::getPropKeyValues ( $object, $propsKeys );
134 56
			$objects [$key] = $object;
135
		}
136 56
		if ($hasIncluded) {
137 24
			self::_affectsRelationObjects ( $className, OrmUtils::getFirstPropKey ( $className ), $manyToOneQueries, $oneToManyQueries, $manyToManyParsers, $objects, $included, $useCache );
138
		}
139 56
		EventsManager::trigger ( DAOEvents::GET_ALL, $objects, $className );
140 56
		return $objects;
141
	}
142
143 62
	protected static function getFieldList($tableName, $metaDatas) {
144 62
		return self::$fields [$tableName] ?? (self::$fields [$tableName] = SqlUtils::getFieldList ( \array_diff ( $metaDatas ['#fieldNames'], $metaDatas ['#notSerializable'] ), $tableName ));
145
	}
146
147
	/**
148
	 *
149
	 * @param array $row
150
	 * @param string $className
151
	 * @param array $invertedJoinColumns
152
	 * @param array $manyToOneQueries
153
	 * @param array $accessors
154
	 * @return object
155
	 */
156 62
	private static function loadObjectFromRow($row, $className, &$invertedJoinColumns, &$manyToOneQueries, &$oneToManyFields, &$manyToManyFields, &$oneToManyQueries, &$manyToManyParsers, &$accessors, &$transformers) {
157 62
		$o = new $className ();
158 62
		if (self::$useTransformers) {
159 8
			foreach ( $transformers as $field => $transformer ) {
160 4
				$transform = self::$transformerOp;
161 4
				$row [$field] = $transformer::$transform ( $row [$field] );
162
			}
163
		}
164 62
		foreach ( $row as $k => $v ) {
165 62
			if (isset ( $accessors [$k] )) {
166 62
				$accesseur = $accessors [$k];
167 62
				$o->$accesseur ( $v );
168
			}
169 62
			$o->_rest [$k] = $v;
170 62
			if (isset ( $invertedJoinColumns ) && isset ( $invertedJoinColumns [$k] )) {
171 23
				$fk = '_' . $k;
172 23
				$o->$fk = $v;
173 51
				self::prepareManyToOne ( $manyToOneQueries, $o, $v, $fk, $invertedJoinColumns [$k] );
174
			}
175
		}
176 62
		if (isset ( $oneToManyFields )) {
177 40
			foreach ( $oneToManyFields as $k => $annot ) {
178 37
				self::prepareOneToMany ( $oneToManyQueries, $o, $k, $annot );
179
			}
180
		}
181 62
		if (isset ( $manyToManyFields )) {
182 23
			foreach ( $manyToManyFields as $k => $annot ) {
183 23
				self::prepareManyToMany ( $manyToManyParsers, $o, $k, $annot );
184
			}
185
		}
186 62
		return $o;
187
	}
188
189 3
	private static function parseKey(&$keyValues, $className, $quote) {
190 3
		if (! \is_array ( $keyValues )) {
191 3
			if (\strrpos ( $keyValues, '=' ) === false && \strrpos ( $keyValues, '>' ) === false && \strrpos ( $keyValues, '<' ) === false) {
192 3
				$keyValues = $quote . OrmUtils::getFirstKey ( $className ) . $quote . "='" . $keyValues . "'";
193
			}
194
		}
195 3
	}
196
}
197