Passed
Push — master ( c3e0c4...8eb9d2 )
by Jean-Christophe
05:39
created

ClassToYuml   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 69.64%

Importance

Changes 0
Metric Value
wmc 37
eloc 106
dl 0
loc 174
ccs 78
cts 112
cp 0.6964
rs 9.44
c 0
b 0
f 0

15 Methods

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