Passed
Push — master ( 7da913...a3998d )
by Jean-Christophe
05:48
created

OrmUtilsRelationsTrait   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Test Coverage

Coverage 50.36%

Importance

Changes 0
Metric Value
wmc 54
eloc 124
dl 0
loc 220
ccs 69
cts 137
cp 0.5036
rs 6.4799
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllJoinTables() 0 6 2
A getJoinTables() 0 10 3
A getFieldsInRelations() 0 13 4
B getAnnotFieldsInRelations() 0 21 8
A getJoinAlias() 0 2 1
A getJoinColumnName() 0 8 2
B getFieldsInRelationsForUpdate_() 0 21 7
A getDefaultFk() 0 2 1
A getRelationInfos() 0 17 5
A getFieldsInRelations_() 0 2 1
B getManyToOneMembersAndValues() 0 20 7
A getOneToManyFields() 0 2 1
A getUJoinSQL() 0 33 5
A getMemberJoinColumns() 0 12 4
A getManyToManyFields() 0 5 2
A getManyToOneFields() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like OrmUtilsRelationsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OrmUtilsRelationsTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Ubiquity\orm\traits;
4
5
use Ubiquity\orm\parser\ManyToManyParser;
6
7
trait OrmUtilsRelationsTrait {
8
	
9
	abstract public static function getAnnotationInfoMember($class, $keyAnnotation, $member);
10
	abstract public static function getAnnotationInfo($class, $keyAnnotation);
11
	abstract public static function getTableName($class);
12
	abstract public static function getFirstKey($class);
13
	abstract public static function getModelMetadata($className);
14
	abstract public static function getKeyFieldsAndValues($instance);
15
	
16
	public static function getJoinTables($class){
17
		$result=[];
18
		
19
		if(isset(self::getModelMetadata($class)["#joinTable"])){
20
			$jts=self::getModelMetadata($class)["#joinTable"];
21
			foreach ($jts as $jt){
22
				$result[]=$jt["name"];
23
			}
24
		}
25
		return $result;
26
	}
27
	
28
	public static function getAllJoinTables($models){
29
		$result=[];
30
		foreach ($models as $model){
31
			$result=array_merge($result,self::getJoinTables($model));
32
		}
33
		return $result;
34
	}
35
	
36 3
	public static function getFieldsInRelations($class) {
37 3
		$result=[ ];
38
		
39 3
		if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) {
40
			$result=\array_merge($result, $manyToOne);
41
		}
42 3
		if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) {
43 3
			$result=\array_merge($result, \array_keys($oneToMany));
44
		}
45 3
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
46
			$result=\array_merge($result, \array_keys($manyToMany));
47
		}
48 3
		return $result;
49
	}
50
	
51
	public static function getRelationInfos($class){
52
		$result=[ ];
53
		$joinColumns=self::getAnnotationInfo($class, "#joinColumn");
54
		$invertedJoinColumns=self::getAnnotationInfo($class, "#invertedJoinColumn");
55
		if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) {
56
			foreach ($manyToOne as $oneField){
57
				$field=$joinColumns[$oneField]['name'];
58
				$result[$field]=$invertedJoinColumns[$field];
59
			}
60
		}
61
		if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) {
62
			$result=\array_merge($result, $oneToMany);
63
		}
64
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
65
			$result=\array_merge($result, $manyToMany);
66
		}
67
		return $result;
68
	}
69
	
70 4
	public static function getFieldsInRelations_($class) {
71 4
		return self::getFieldsInRelationsForUpdate_($class)["relations"];
72
	}
73
	
74 4
	public static function getFieldsInRelationsForUpdate_($class) {
75 4
		$result=[ ];
76 4
		if ($manyToOne=self::getAnnotationInfo($class, "#manyToOne")) {
77 1
			foreach ($manyToOne as $member){
78 1
				$joinColumn = self::getAnnotationInfoMember ( $class, "#joinColumn", $member );
79 1
				$result[$joinColumn["name"]]=$member;
80
			}
81
		}
82 4
		if ($manyToMany=self::getAnnotationInfo($class, "#manyToMany")) {
83
			$manyToMany=array_keys($manyToMany);
84
			foreach ($manyToMany as $member){
85
				$result[$member . "Ids"]=$member;
86
			}
87
		}
88 4
		if ($oneToMany=self::getAnnotationInfo($class, "#oneToMany")) {
89 3
			$oneToMany=array_keys($oneToMany);
90 3
			foreach ($oneToMany as $member){
91 3
				$result[$member . "Ids"]=$member;
92
			}
93
		}
94 4
		return ["relations"=>$result,"manyToOne"=>$manyToOne,"manyToMany"=>$manyToMany,"oneToMany"=>$oneToMany];
95
	}
96
	
97 1
	public static function getAnnotFieldsInRelations($class) {
98 1
		$result=[ ];
99 1
		if ($manyToOnes=self::getAnnotationInfo($class, "#manyToOne")) {
100 1
			$joinColumns=self::getAnnotationInfo($class, "#joinColumn");
101 1
			foreach ($manyToOnes as $manyToOne ){
102 1
				if(isset($joinColumns[$manyToOne])){
103 1
					$result[$manyToOne]=["type"=>"manyToOne","value"=>$joinColumns[$manyToOne]];
104
				}
105
			}
106
		}
107 1
		if ($oneToManys=self::getAnnotationInfo($class, "#oneToMany")) {
108 1
			foreach ($oneToManys as $field=>$oneToMany){
109 1
				$result[$field]=["type"=>"oneToMany","value"=>$oneToMany];
110
			}
111
		}
112 1
		if ($manyToManys=self::getAnnotationInfo($class, "#manyToMany")) {
113 1
			foreach ($manyToManys as $field=>$manyToMany){
114 1
				$result[$field]=["type"=>"manyToMany","value"=>$manyToMany];
115
			}
116
		}
117 1
		return $result;
118
	}
119
	
120 3
	public static function getUJoinSQL($model,$arrayAnnot,$field,&$aliases){
121 3
		$type=$arrayAnnot["type"];$annot=$arrayAnnot["value"];
122 3
		$table=self::getTableName($model);
123 3
		$tableAlias=(isset($aliases[$table]))?$aliases[$table]:$table;
124 3
		if($type==="manyToOne"){
125
			$fkClass=$annot["className"];
126
			$fkTable=self::getTableName($fkClass);
127
			$fkField=$annot["name"];
128
			$pkField=self::getFirstKey($fkClass);
129
			$alias=self::getJoinAlias($table, $fkTable);
130
			$result="INNER JOIN `{$fkTable}` `{$alias}` ON `{$tableAlias}`.`{$fkField}`=`{$alias}`.`{$pkField}`";
131 3
		}elseif($type==="oneToMany"){
132
			$fkClass=$annot["className"];
133
			$fkAnnot=self::getAnnotationInfoMember($fkClass, "#joinColumn", $annot["mappedBy"]);
134
			$fkTable=self::getTableName($fkClass);
135
			$fkField=$fkAnnot["name"];
136
			$pkField=self::getFirstKey($model);
137
			$alias=self::getJoinAlias($table, $fkTable);
138
			$result="INNER JOIN `{$fkTable}` `{$alias}` ON `{$tableAlias}`.`{$pkField}`=`{$alias}`.`{$fkField}`";
139
		}else{
140 3
			$parser=new ManyToManyParser($model,$field);
141 3
			$parser->init($annot);
142 3
			$fkTable=$parser->getTargetEntityTable();
143 3
			$fkClass=$parser->getTargetEntityClass();
144 3
			$alias=self::getJoinAlias($table, $fkTable);
145 3
			$result=$parser->getSQL($alias,$aliases);
146
		}
147
		
148 3
		if(array_search($alias, $aliases)!==false){
149
			$result="";
150
		}
151 3
		$aliases[$fkTable]=$alias;
152 3
		return ["class"=>$fkClass,"table"=>$fkTable,"sql"=>$result,"alias"=>$alias];
153
	}
154
	
155 3
	private static function getJoinAlias($table,$fkTable){
156 3
		return uniqid($fkTable.'_'.$table[0]);
157
	}
158
	
159
	public static function getOneToManyFields($class) {
160
		return self::getAnnotationInfo($class, "#oneToMany");
161
	}
162
	
163
	public static function getManyToOneFields($class) {
164
		return self::getAnnotationInfo($class, "#manyToOne");
165
	}
166
	
167
	public static function getManyToManyFields($class) {
168
		$result=self::getAnnotationInfo($class, "#manyToMany");
169
		if($result!==false)
170
			return \array_keys($result);
171
			return [];
172
	}
173
	
174 18
	public static function getDefaultFk($classname) {
175 18
		return "id" . \ucfirst(self::getTableName($classname));
176
	}
177
	
178 4
	public static function getMemberJoinColumns($instance,$member,$metaDatas=NULL){
179 4
		if(!isset($metaDatas)){
180 3
			$class=get_class($instance);
181 3
			$metaDatas=self::getModelMetadata($class);
182
		}
183 4
		$invertedJoinColumns=$metaDatas["#invertedJoinColumn"];
184 4
		foreach ($invertedJoinColumns as $field=>$invertedJoinColumn){
185 4
			if($invertedJoinColumn["member"]===$member){
186 4
				return [$field,$invertedJoinColumn];
187
			}
188
		}
189
		return null;
190
	}
191
	
192
	/**
193
	 *
194
	 * @param object $instance
195
	 * @return mixed[]
196
	 */
197 4
	public static function getManyToOneMembersAndValues($instance) {
198 4
		$ret=array ();
199 4
		$class=get_class($instance);
200 4
		$members=self::getAnnotationInfo($class, "#manyToOne");
201 4
		if ($members !== false) {
202 4
			foreach ( $members as $member ) {
203
				$memberAccessor="get" . ucfirst($member);
204
				if (method_exists($instance, $memberAccessor)) {
205
					$memberInstance=$instance->$memberAccessor();
206
					if (isset($memberInstance) && is_object($memberInstance)) {
207
						$keyValues=self::getKeyFieldsAndValues($memberInstance);
208
						if (sizeof($keyValues) > 0) {
209
							$fkName=self::getJoinColumnName($class, $member);
210
							$ret[$fkName]=current($keyValues);
211
						}
212
					}
213
				}
214
			}
215
		}
216 4
		return $ret;
217
	}
218
	
219
	public static function getJoinColumnName($class, $member) {
220
		$annot=self::getAnnotationInfoMember($class, "#joinColumn", $member);
221
		if ($annot !== false) {
222
			$fkName=$annot["name"];
223
		} else {
224
			$fkName="id" . ucfirst(self::getTableName(ucfirst($member)));
225
		}
226
		return $fkName;
227
	}
228
}
229
230