Passed
Push — master ( 213516...f477f5 )
by Jean-Christophe
04:14
created

OrmUtilsRelationsTrait   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Test Coverage

Coverage 30.65%

Importance

Changes 0
Metric Value
wmc 49
eloc 112
dl 0
loc 201
ccs 38
cts 124
cp 0.3065
rs 8.48
c 0
b 0
f 0

15 Methods

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

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