Completed
Push — master ( 0a5d2e...4a55ea )
by Jean-Christophe
01:23
created

ClassParser::parse()   C

Complexity

Conditions 10
Paths 4

Size

Total Lines 47
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 47
rs 5.1578
c 2
b 0
f 1
cc 10
eloc 34
nc 4
nop 0

How to fix   Complexity   

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:

1
<?php
2
3
namespace micro\utils\yuml;
4
5
use micro\orm\OrmUtils;
6
7
/**
8
 * @author jc
9
 * yuml export tool for class
10
 */
11
class ClassParser {
12
	protected $class;
13
	protected $displayProperties=true;
14
	protected $displayMethods=false;
15
	protected $displayMethodsParams=false;
16
	protected $displayPropertiesTypes=false;
17
	protected $displayAssociations;
18
	protected $displayAssociationClassProperties=false;
19
	protected $properties;
20
	protected $oneToManys=[];
21
	protected $manyToOne=[];
22
23
	public function __construct($class,$displayProperties=true,$displayAssociations=true,$displayMethods=false,$displayMethodsParams=false,$displayPropertiesTypes=false,$displayAssociationClassProperties=false){
24
		$this->class=$class;
25
		$this->displayProperties=$displayProperties;
26
		$this->displayAssociations=$displayAssociations;
27
		$this->displayMethods=$displayMethods;
28
		$this->displayMethodsParams=$displayMethodsParams;
29
		$this->displayPropertiesTypes=$displayPropertiesTypes;
30
		$this->displayAssociationClassProperties=$displayAssociationClassProperties;
31
	}
32
33
	public function init($hasManyToOne,$hasOneToMany){
34
		if($hasManyToOne){
35
			$this->loadManyToOne();
36
		}
37
		if($hasOneToMany){
38
			$this->loadOneToManys();
39
		}
40
	}
41
42
	public function parse(){
43
		$reflect=new \ReflectionClass($this->class);
44
		$parts=[$reflect->getShortName()];
45
46
		if($this->displayProperties){
47
			$prikeys=OrmUtils::getKeyFields($this->class);
48
			$types=OrmUtils::getFieldTypes($this->class);
49
			$propertiesArray=[];
50
			$properties=$reflect->getProperties();
51
			foreach ($properties as $property){
52
				$propertyName=$property->getName();
53
				$type="";$isPri="";
54
				if($this->displayPropertiesTypes){
55
					if(\array_key_exists($propertyName, $types)){
56
						$type=Yuml::$parameterTypeSeparator.$types[$propertyName];
57
					}
58
				}
59
				if(\array_search($propertyName, $prikeys)!==false){
60
					$isPri=Yuml::$primary;
61
				}
62
				$propertiesArray[]=Yuml::setPropertyVariables([$this->getAccess($property),$isPri,$propertyName,$type]);
63
			}
64
			$parts[]=\implode(Yuml::$memberSeparator, $propertiesArray);
65
		}
66
67
		if($this->displayMethods){
68
			$methodsArray=[];
69
			$methods=$reflect->getMethods();
70
			foreach ($methods as $method){
71
				$parameters="";
72
				if($this->displayMethodsParams){
73
					$parameters=$this->getMethodParameters($method);
74
				}
75
				$methodName=$method->getName();
1 ignored issue
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
76
				$type="";
77
				if($method->hasReturnType()){
78
					$type=Yuml::$parameterTypeSeparator.$method->getReturnType();
79
				}
80
				$methodsArray[]=Yuml::setMethodVariables([$this->getAccess($method),$methodName,$parameters,$type]);
81
			}
82
			$parts[]=\implode(Yuml::$memberSeparator, $methodsArray);
83
		}
84
85
		$result=\implode(Yuml::$classSeparator, $parts);
86
		$result=Yuml::setClassContent($result);
87
		return $result;
88
	}
89
90
	protected function getShortClassName($class){
91
		$reflect=new \ReflectionClass($class);
92
		return $reflect->getShortName();
93
	}
94
95
	protected function loadOneToManys(){
96
		$oneToManys=OrmUtils::getAnnotationInfo($this->class, "#oneToMany");
97
		if($oneToManys){
98
			foreach ($oneToManys as $member=>$array){
99
				$this->oneToManys[$member]=$array["className"];
100
			}
101
		}
102
	}
103
104
	protected function loadManyToOne(){
105
		$manyToOne=OrmUtils::getAnnotationInfo($this->class, "#manyToOne");
106
		if($manyToOne){
107
			foreach ($manyToOne as $member){
108
				$joinColumn=OrmUtils::getAnnotationInfoMember($this->class, "#joinColumn", $member);
109
				if($joinColumn){
110
					if(isset($joinColumn["className"]))
111
						$this->manyToOne[$member]=$joinColumn["className"];
112
				}
113
			}
114
		}
115
	}
116
117
	protected function _getYumlManyToOne(){
118
		return $this->_getYumlRelationsType($this->manyToOne,"0..*-1");
119
	}
120
121
	protected function _getYumlOneToMany(){
122
		return $this->_getYumlRelationsType($this->oneToManys,"1-0..*");
123
	}
124
125
	protected function _getYumlRelationsType($relations,$branche){
126
		$myClass=$this->getShortClassName($this->class);
127
		$yumlRelations=[];
128
		foreach ($relations as $model){
129
			$yumlRelations[]=Yuml::setClassContent($myClass).$branche.new ClassParser($model,$this->displayAssociationClassProperties,false);
130
		}
131
		return $yumlRelations;
132
	}
133
134
	protected function getMethodParameters(\ReflectionMethod $method){
135
		$paramsValues=[];
136
		$parameters=$method->getParameters();
137
		foreach ($parameters as $parameter){
138
			$v=$parameter->getName();
1 ignored issue
show
Bug introduced by
Consider using $parameter->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
139
			if($parameter->hasType()){
140
				$v.=Yuml::$parameterTypeSeparator.$parameter->getType();
141
			}
142
			$paramsValues[]=$v;
143
		}
144
		return \implode(Yuml::$parameterSeparator, $paramsValues);
145
	}
146
147
	protected function getAccess($property){
148
		$result=Yuml::$private;
149
		if($property->isPublic()){
150
			$result=Yuml::$public;
151
		}elseif($property->isProtected()){
152
			$result=Yuml::$protected;
153
		}
154
		return $result;
155
	}
156
157
	public function manyToOneTostring(){
158
		$this->loadManyToOne();
159
		return \implode(Yuml::$groupeSeparator, $this->_getYumlManyToOne());
160
	}
161
162
	public function oneToManyTostring(){
163
		$this->loadOneToManys();
164
		return \implode(Yuml::$groupeSeparator, $this->_getYumlOneToMany());
165
	}
166
167
	public function __toString(){
168
		$result=[$this->parse()];
169
		if($this->displayAssociations){
170
			$result=\array_merge($result,$this->_getYumlManyToOne());
171
			$result=\array_merge($result,$this->_getYumlOneToMany());
172
		}
173
		return \implode(Yuml::$groupeSeparator, $result);
174
	}
175
176
177
	public function setDisplayProperties($displayProperties) {
178
		$this->displayProperties=$displayProperties;
179
		return $this;
180
	}
181
182
	public function setDisplayMethods($displayMethods) {
183
		$this->displayMethods=$displayMethods;
184
		return $this;
185
	}
186
187
	public function setDisplayAssociations($displayAssociations) {
188
		$this->displayAssociations=$displayAssociations;
189
		return $this;
190
	}
191
192
}