Passed
Push — master ( 488e2d...680fd5 )
by Jean-Christophe
05:46
created

ClassToYuml::removeManyToManyExt()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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 2
	protected function getShortClassName($class) {
125 2
		$reflect = new \ReflectionClass ( $class );
126 2
		return $reflect->getShortName ();
127
	}
128
129
	protected function _getNote() {
130
		return "-[note:" . $this->note . "]";
131
	}
132
133 1
	protected function getMethodParameters(\ReflectionMethod $method) {
134 1
		$paramsValues = [ ];
135 1
		$parameters = $method->getParameters ();
136 1
		foreach ( $parameters as $parameter ) {
137 1
			$v = $parameter->getName ();
138 1
			if ($parameter->hasType ()) {
139
				$v .= Yuml::$parameterTypeSeparator . $parameter->getType ();
140
			}
141 1
			$paramsValues [] = $v;
142
		}
143 1
		return \implode ( Yuml::$parameterSeparator, $paramsValues );
144
	}
145
146 2
	protected function getAccess($property) {
147 2
		$result = Yuml::$private;
148 2
		if ($property->isPublic ()) {
149 1
			$result = Yuml::$public;
150 2
		} elseif ($property->isProtected ()) {
151
			$result = Yuml::$protected;
152
		}
153 2
		return $result;
154
	}
155
156
	public function manyToOneTostring() {
157
		$this->loadManyToOne ();
158
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlManyToOne () );
159
	}
160
161 2
	public function oneToManyTostring() {
162 2
		$this->loadOneToManys ();
163 2
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlOneToMany () );
164
	}
165
166 2
	public function manyToManyTostring($load = true) {
167 2
		if ($load) {
168
			$this->loadManyToManys ();
169
		}
170 2
		return \implode ( Yuml::$groupeSeparator, $this->_getYumlManyToMany () );
171
	}
172
173 2
	public function __toString() {
174 2
		$result = [ $this->parse () ];
175 2
		if ($this->displayAssociations) {
176
			$result = \array_merge ( $result, $this->_getYumlManyToOne () );
177
			$result = \array_merge ( $result, $this->_getYumlOneToMany () );
178
			$result = \array_merge ( $result, $this->_getYumlManyToMany () );
179
		}
180 2
		return \implode ( Yuml::$groupeSeparator, $result );
181
	}
182
183
	public function setDisplayProperties($displayProperties) {
184
		$this->displayProperties = $displayProperties;
185
		return $this;
186
	}
187
188
	public function setDisplayMethods($displayMethods) {
189
		$this->displayMethods = $displayMethods;
190
		return $this;
191
	}
192
193
	public function setDisplayAssociations($displayAssociations) {
194
		$this->displayAssociations = $displayAssociations;
195
		return $this;
196
	}
197
}
198