Completed
Push — master ( 2a8480...b4e0c6 )
by mw
415:39 queued 380:48
created

CompositePropertyTableDiffIterator   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 96.83%

Importance

Changes 0
Metric Value
dl 0
loc 273
ccs 61
cts 63
cp 0.9683
rs 8.3673
c 0
b 0
f 0
wmc 45
lcom 3
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setSubject() 0 3 1
A getSubject() 0 3 1
A getIterator() 0 3 1
A getHash() 0 3 2
A addDataRecord() 0 3 1
A getDataChangeOps() 0 12 3
A addTableDiffChangeOp() 0 11 1
A addFixedPropertyRecord() 0 3 1
A getFixedPropertyRecords() 0 3 1
A getTableChangeOps() 0 10 2
C getOrderedDiffByTable() 0 41 14
C getListOfChangedEntityIdsByType() 0 21 10
A getCombinedIdListOfChangedEntities() 0 3 1
B addToIdList() 0 16 5

How to fix   Complexity   

Complex Class

Complex classes like CompositePropertyTableDiffIterator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 CompositePropertyTableDiffIterator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace SMW\SQLStore;
4
5
use ArrayIterator;
6
use IteratorAggregate;
7
use SMW\DIWikiPage;
8
use SMW\SQLStore\ChangeOp\TableChangeOp;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 2.3
13
 *
14
 * @author mwjames
15
 */
16
class CompositePropertyTableDiffIterator implements IteratorAggregate {
17
18
	/**
19
	 * Type of change operations
20
	 */
21
	const TYPE_INSERT = 'insert';
22
	const TYPE_DELETE = 'delete';
23
24
	/**
25
	 * @var array
26
	 */
27
	private $diff = array();
28
29
	/**
30
	 * @var array
31
	 */
32
	private $data = array();
33
34
	/**
35
	 * @var array
36
	 */
37
	private $orderedDiff = array();
38
39
	/**
40
	 * @var DIWikiPage
41
	 */
42
	private $subject;
43 278
44 278
	/**
45 278
	 * @var string
46
	 */
47
	private $hash = '';
48
49
	/**
50
	 * @var array
51
	 */
52 272
	private $fixedPropertyRecords = array();
53 272
54 272
	/**
55
	 * @since 2.3
56
	 *
57
	 * @param array $diff
58
	 */
59
	public function __construct( array $diff = array() ) {
60
		$this->diff = $diff;
61
	}
62
63
	/**
64
	 * @since 2.5
65
	 *
66
	 * @return DIWikiPage $subject
67
	 */
68
	public function setSubject( DIWikiPage $subject ) {
69
		$this->subject = $subject;
70 207
	}
71 207
72
	/**
73
	 * @since 2.5
74
	 *
75
	 * @return DIWikiPage
76
	 */
77
	public function getSubject() {
78
		return $this->subject;
79 1
	}
80 1
81
	/**
82
	 * @since 2.3
83
	 *
84
	 * @return ArrayIterator
85
	 */
86
	public function getIterator() {
87
		return new ArrayIterator( $this->diff );
88
	}
89 271
90
	/**
91
	 * @since 2.5
92 271
	 *
93 271
	 * @return string
94
	 */
95
	public function getHash() {
96 271
		return md5( $this->hash . ( $this->subject !== null ? $this->subject->asBase()->getHash() : '' ) );
97
	}
98 271
99 271
	/**
100
	 * @since 3.0
101
	 *
102
	 * @param array $data
103
	 */
104
	public function addDataRecord( $hash, array $data ) {
105
		$this->data[$hash] = $data;
106 234
	}
107 234
108 234
	/**
109
	 * @since 3.0
110
	 *
111
	 * @return TableChangeOp[]
112
	 */
113
	public function getDataChangeOps() {
114
115 3
		$dataChangeOps = array();
116 3
117
		foreach ( $this->data as $hash => $data ) {
118
			foreach ( $data as $tableName => $d ) {
119
				$dataChangeOps[] = new TableChangeOp( $tableName, $d );
120
			}
121
		}
122
123
		return $dataChangeOps;
124
	}
125
126
	/**
127
	 * @since 2.3
128
	 *
129 203
	 * @param array $insertChangeOp
130
	 * @param array $deleteChangeOp
131 203
	 */
132
	public function addTableDiffChangeOp( array $insertChangeOp, array $deleteChangeOp ) {
133 203
134 198
		$diff = array(
135
			'insert' => $insertChangeOp,
136
			'delete' => $deleteChangeOp
137 203
		);
138
139
		$this->diff[] = $diff;
140
141
		$this->hash .= json_encode( $diff );
142
	}
143
144
	/**
145
	 * @since 2.3
146
	 *
147
	 * @param array $fixedPropertyRecord
148 207
	 */
149
	public function addFixedPropertyRecord( $tableName, array $fixedPropertyRecord ) {
150 207
		$this->fixedPropertyRecords[$tableName] = $fixedPropertyRecord;
151
	}
152 207
153 205
	/**
154 205
	 * @since 2.4
155
	 *
156 200
	 * @return array
157 199
	 */
158
	public function getFixedPropertyRecords() {
159
		return $this->fixedPropertyRecords;
160 200
	}
161 195
162
	/**
163
	 * ChangeOp (TableChangeOp/FieldChangeOp) representation of the composite
164 200
	 * diff.
165 184
	 *
166
	 * @since 2.4
167
	 *
168 200
	 * @param string|null $table
169 200
	 *
170
	 * @return TableChangeOp[]|[]
0 ignored issues
show
Documentation introduced by
The doc-type TableChangeOp[]|[] could not be parsed: Unknown type name "" at position 16. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
171
	 */
172 200
	public function getTableChangeOps( $table = null ) {
173 205
174
		$tableChangeOps = array();
175
176
		foreach ( $this->getOrderedDiffByTable( $table ) as $tableName => $diff ) {
177
			$tableChangeOps[] = new TableChangeOp( $tableName, $diff );
178
		}
179 207
180
		return $tableChangeOps;
181
	}
182
183
	/**
184
	 * Simplified (ordered by table) diff array to allow for an easier
185
	 * post-processing
186
	 *
187 205
	 * @since 2.3
188
	 *
189 205
	 * @return array
190
	 */
191 205
	public function getOrderedDiffByTable( $table = null ) {
192
193 199
		if ( $table === null && $this->orderedDiff !== array() ) {
194 198
			return $this->orderedDiff;
195
		}
196
197 199
		$ordered = array();
198 192
199
		foreach ( $this as $diff ) {
200
			foreach ( $diff as $key => $value ) {
201 199
				foreach ( $value as $tableName => $val ) {
202 199
203
					if ( $val === array() || ( $table !== null && $table !== $tableName ) ) {
204
						continue;
205
					}
206 205
207
					if ( isset( $this->fixedPropertyRecords[$tableName] ) ) {
208
						$ordered[$tableName]['property'] = $this->fixedPropertyRecords[$tableName];
209 199
					}
210 199
211
					if ( !isset( $ordered[$tableName] ) ) {
212 199
						$ordered[$tableName] = array();
213 183
					}
214
215
					if ( !isset( $ordered[$tableName][$key] ) ) {
216 199
						$ordered[$tableName][$key] = array();
217 199
					}
218
219
					foreach ( $val as $v ) {
220 199
						$ordered[$tableName][$key][] = $v;
221 199
					}
222
				}
223
			}
224 199
		}
225
226
		if ( $table === null ) {
227
			$this->orderedDiff = $ordered;
228
		}
229
230
		return $ordered;
231
	}
232
233
	/**
234
	 * @since 3.0
235
	 *
236
	 * @param string|null $type
237
	 *
238
	 * @return array
239
	 */
240
	public function getListOfChangedEntityIdsByType( $type = null ) {
241
242
		$changedEntities = array();
243
244
		foreach ( $this->getOrderedDiffByTable() as $diff ) {
245
246
			if ( ( $type === 'insert' || $type === null ) && isset( $diff['insert'] )  ) {
247
				$this->addToIdList( $changedEntities, $diff['insert'] );
248
			}
249
250
			if ( ( $type === 'delete' || $type === null ) && isset( $diff['delete'] )  ) {
251
				$this->addToIdList( $changedEntities, $diff['delete'] );
252
			}
253
254
			if ( $type === null && isset( $diff['property'] )  ) {
255
				$changedEntities[$diff['property']['p_id']] = true;
256
			}
257
		}
258
259
		return $changedEntities;
260
	}
261
262
	/**
263
	 * @since 2.3
264
	 *
265
	 * @return array
266
	 */
267
	public function getCombinedIdListOfChangedEntities() {
268
		return array_keys( $this->getListOfChangedEntityIdsByType() );
269
	}
270
271
	private function addToIdList( &$list, $value ) {
272
		foreach ( $value as $element ) {
273
274
			if ( isset( $element['p_id'] ) ) {
275
				$list[$element['p_id']] = true;
276
			}
277
278
			if ( isset( $element['s_id'] ) ) {
279
				$list[$element['s_id']] = true;
280
			}
281
282
			if ( isset( $element['o_id'] ) ) {
283
				$list[$element['o_id']] = true;
284
			}
285
		}
286
	}
287
288
}
289