GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TypeMap   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 10
Bugs 2 Features 5
Metric Value
wmc 29
c 10
b 2
f 5
lcom 1
cbo 6
dl 0
loc 218
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getDestinationType() 0 4 1
A getSourceType() 0 4 1
A getObjectCreator() 0 4 1
A setObjectCreator() 0 4 1
A getPropertyMaps() 0 4 1
A getMappedPropertyMaps() 0 6 2
A getUnmappedPropertyMaps() 0 6 1
A addPropertyMap() 0 4 1
A getPropertyMap() 0 6 2
A getBeforeMapFunc() 0 4 1
A setBeforeMapFunc() 0 4 1
A getAfterMapFunc() 0 4 1
A setAfterMapFunc() 0 4 1
A validate() 0 18 2
C getMapFunc() 0 49 11
1
<?php
2
3
namespace Papper;
4
5
/**
6
 * Main configuration object holding all mapping configuration for a source and destination type
7
 *
8
 * @author Vladimir Komissarov <[email protected]>
9
 */
10
class TypeMap
11
{
12
	/**
13
	 * @var string
14
	 */
15
	private $destinationType;
16
	/**
17
	 * @var string
18
	 */
19
	private $sourceType;
20
	/**
21
	 * @var ObjectCreatorInterface
22
	 */
23
	private $objectCreator;
24
	/**
25
	 * @var PropertyMap[]
26
	 */
27
	private $propertyMaps = array();
28
	/**
29
	 * @var \Closure|null
30
	 */
31
	private $beforeMapFunc;
32
	/**
33
	 * @var \Closure|null
34
	 */
35
	private $afterMapFunc;
36
37
	public function __construct($sourceType, $destinationType, ObjectCreatorInterface $objectCreator)
38
	{
39
		$this->destinationType = $destinationType;
40
		$this->sourceType = $sourceType;
41
		$this->objectCreator = $objectCreator;
42
	}
43
44
	/**
45
	 * @return string
46
	 */
47
	public function getDestinationType()
48
	{
49
		return $this->destinationType;
50
	}
51
52
	/**
53
	 * @return string
54
	 */
55
	public function getSourceType()
56
	{
57
		return $this->sourceType;
58
	}
59
60
	/**
61
	 * @return ObjectCreatorInterface
62
	 */
63
	public function getObjectCreator()
64
	{
65
		return $this->objectCreator;
66
	}
67
68
	/**
69
	 * @param ObjectCreatorInterface $objectCreator
70
	 */
71
	public function setObjectCreator(ObjectCreatorInterface $objectCreator)
72
	{
73
		$this->objectCreator = $objectCreator;
74
	}
75
76
	/**
77
	 * @return PropertyMap[]
78
	 */
79
	public function getPropertyMaps()
80
	{
81
		return $this->propertyMaps;
82
	}
83
84
	/**
85
	 * @return PropertyMap[]
86
	 */
87
	public function getMappedPropertyMaps()
88
	{
89
		return array_filter($this->propertyMaps, function (PropertyMap $propertyMap) {
90
			return $propertyMap->isMapped() && !$propertyMap->isIgnored();
91
		});
92
	}
93
94
	/**
95
	 * @return PropertyMap[]
96
	 */
97
	public function getUnmappedPropertyMaps()
98
	{
99
		return array_filter($this->propertyMaps, function (PropertyMap $propertyMap) {
100
			return !$propertyMap->isMapped();
101
		});
102
	}
103
104
	/**
105
	 * @param PropertyMap $propertyMap
106
	 */
107
	public function addPropertyMap(PropertyMap $propertyMap)
108
	{
109
		$this->propertyMaps[$propertyMap->getMemberName()] = $propertyMap;
110
	}
111
112
	/**
113
	 * @param string $memberName
114
	 * @return null|PropertyMap
115
	 */
116
	public function getPropertyMap($memberName)
117
	{
118
		return isset($this->propertyMaps[$memberName])
119
			? $this->propertyMaps[$memberName]
120
			: null;
121
	}
122
123
	/**
124
	 * @return callable|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Closure|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
125
	 */
126
	public function getBeforeMapFunc()
127
	{
128
		return $this->beforeMapFunc;
129
	}
130
131
	/**
132
	 * @param callable $func
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $func a bit more specific; maybe use \Closure.
Loading history...
133
	 */
134
	public function setBeforeMapFunc(\Closure $func)
135
	{
136
		$this->beforeMapFunc = $func;
137
	}
138
139
	/**
140
	 * @return callable|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Closure|null.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
141
	 */
142
	public function getAfterMapFunc()
143
	{
144
		return $this->afterMapFunc;
145
	}
146
147
	/**
148
	 * @param callable $func
0 ignored issues
show
Documentation introduced by
Consider making the type for parameter $func a bit more specific; maybe use \Closure.
Loading history...
149
	 */
150
	public function setAfterMapFunc(\Closure $func)
151
	{
152
		$this->afterMapFunc = $func;
153
	}
154
155
	/**
156
	 * @todo validate source members
157
	 * @todo validate constructor args
158
	 */
159
	public function validate()
160
	{
161
		$unmappedProperties = $this->getUnmappedPropertyMaps();
162
163
		if (empty($unmappedProperties)) {
164
			return;
165
		}
166
167
		$memberNames = array_map(function (PropertyMap $propertyMap) {
168
			return $propertyMap->getMemberName();
169
		}, $unmappedProperties);
170
171
		throw new ValidationException(sprintf(
172
			"Unmapped members were found. Add a custom mapping expression, ignore, " .
173
			"add a custom resolver, or modify the source/destination type:\n%s -> %s\nDestination members: %s",
174
			$this->sourceType, $this->destinationType, implode(", ", $memberNames)
175
		));
176
	}
177
178
	public function getMapFunc()
179
	{
180
		$objectCreator = $this->getObjectCreator();
181
		$propertyMaps = $this->getMappedPropertyMaps();
182
		$sourceType = $this->getSourceType();
183
		$destinationType = $this->getDestinationType();
184
		$beforeMapFunc = $this->getBeforeMapFunc();
185
		$afterMapFunc = $this->getAfterMapFunc();
186
187
		return function ($source, $destination = null) use ($objectCreator, $propertyMaps, $sourceType, $destinationType, $beforeMapFunc, $afterMapFunc) {
188
189
			if ($destination === null) {
190
				$destination = $objectCreator->create($source);
191
			}
192
193
			if (!$destination instanceof $destinationType) {
194
				$type = is_object($destination) ? get_class($destination) : gettype($destination);
195
				$message = sprintf('Constructed object type expected %s, but actual %s', $destinationType, $type);
196
				throw new ValidationException($message);
197
			}
198
199
			if (!$source instanceof $sourceType) {
200
				$type = is_object($source) ? get_class($source) : gettype($source);
201
				$message = sprintf('Source object type expected %s, but actual %s', $destinationType, $type);
202
				throw new ValidationException($message);
203
			}
204
205
			if ($beforeMapFunc) {
206
				$beforeMapFunc($source, $destination);
207
			}
208
209
			foreach ($propertyMaps as $propertyMap) {
210
				$value = $propertyMap->getSourceGetter()->getValue($source);
211
				if ($propertyMap->hasValueConverter()) {
212
					$value = $propertyMap->getValueConverter()->convert($value);
213
				}
214
				if ($value === null) {
215
					$value = $propertyMap->getNullSubtitute();
216
				}
217
				$propertyMap->getDestinationSetter()->setValue($destination, $value);
218
			}
219
220
			if ($afterMapFunc) {
221
				$afterMapFunc($source, $destination);
222
			}
223
224
			return $destination;
225
		};
226
	}
227
}
228