Complex classes like StatementList 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 StatementList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class StatementList implements IteratorAggregate, Countable { |
||
32 | |||
33 | /** |
||
34 | * @var Statement[] |
||
35 | */ |
||
36 | private $statements = []; |
||
37 | |||
38 | /** |
||
39 | * @param Statement ...$statements |
||
40 | * (passing a single Statement[] or Traversable is still supported but deprecated) |
||
41 | * |
||
42 | * @throws InvalidArgumentException |
||
43 | 50 | */ |
|
44 | 50 | public function __construct( ...$statements ) { |
|
64 | |||
65 | /** |
||
66 | * Returns the property ids used by the statements. |
||
67 | 2 | * The keys of the returned array hold the serializations of the property ids. |
|
68 | 2 | * |
|
69 | * @return PropertyId[] Array indexed by property id serialization. |
||
70 | 2 | */ |
|
71 | 1 | public function getPropertyIds() { |
|
80 | |||
81 | /** |
||
82 | * @since 1.0, setting an index is supported since 6.1 |
||
83 | * @see ReferenceList::addReference |
||
84 | * |
||
85 | * @param Statement $statement |
||
86 | * @param int|null $index New position of the added statement, or null to append. |
||
87 | 7 | * |
|
88 | 7 | * @throws InvalidArgumentException |
|
89 | 7 | */ |
|
90 | public function addStatement( Statement $statement, $index = null ) { |
||
99 | |||
100 | /** |
||
101 | * @param Snak $mainSnak |
||
102 | 4 | * @param Snak[]|SnakList|null $qualifiers |
|
103 | 4 | * @param Reference[]|ReferenceList|null $references |
|
104 | 4 | * @param string|null $guid |
|
105 | 3 | */ |
|
106 | 3 | public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references = null, $guid = null ) { |
|
115 | |||
116 | /** |
||
117 | * @since 3.0 |
||
118 | * |
||
119 | * @param string|null $guid |
||
120 | 1 | */ |
|
121 | 1 | public function removeStatementsWithGuid( $guid ) { |
|
130 | |||
131 | /** |
||
132 | * Statements that have a main snak already in the list are filtered out. |
||
133 | * The last occurrences are retained. |
||
134 | * |
||
135 | * @since 1.0 |
||
136 | * |
||
137 | 2 | * @return self |
|
138 | 2 | */ |
|
139 | public function getWithUniqueMainSnaks() { |
||
148 | |||
149 | /** |
||
150 | * @since 3.0 |
||
151 | * |
||
152 | * @param PropertyId $id |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | 6 | public function getByPropertyId( PropertyId $id ) { |
|
167 | |||
168 | /** |
||
169 | * @since 3.0 |
||
170 | * |
||
171 | * @param int|int[] $acceptableRanks |
||
172 | * |
||
173 | * @return self |
||
174 | */ |
||
175 | public function getByRank( $acceptableRanks ) { |
||
187 | |||
188 | /** |
||
189 | * Returns the so called "best statements". |
||
190 | * If there are preferred statements, then this is all the preferred statements. |
||
191 | * If there are no preferred statements, then this is all normal statements. |
||
192 | * |
||
193 | * @since 2.4 |
||
194 | * |
||
195 | * @return self |
||
196 | */ |
||
197 | public function getBestStatements() { |
||
206 | 1 | ||
207 | /** |
||
208 | 1 | * Returns a list of all Snaks on this StatementList. This includes at least the main snaks of |
|
209 | * all statements, the snaks from qualifiers, and the snaks from references. |
||
210 | * |
||
211 | * This is a convenience method for use in code that needs to operate on all snaks, e.g. |
||
212 | * to find all referenced Entities. |
||
213 | * |
||
214 | * @since 1.1 |
||
215 | * |
||
216 | 1 | * @return Snak[] Numerically indexed (non-sparse) array. |
|
217 | 1 | */ |
|
218 | public function getAllSnaks() { |
||
229 | 1 | ||
230 | 1 | /** |
|
231 | * @since 2.3 |
||
232 | * |
||
233 | * @return Snak[] Numerically indexed (non-sparse) array. |
||
234 | */ |
||
235 | public function getMainSnaks() { |
||
244 | |||
245 | 12 | /** |
|
246 | 12 | * @return Iterator|Statement[] |
|
247 | */ |
||
248 | public function getIterator() { |
||
251 | |||
252 | /** |
||
253 | * @return Statement[] Numerically indexed (non-sparse) array. |
||
254 | */ |
||
255 | public function toArray() { |
||
258 | |||
259 | /** |
||
260 | * @see Countable::count |
||
261 | 8 | * |
|
262 | 8 | * @return int |
|
263 | 8 | */ |
|
264 | 2 | public function count() { |
|
267 | 6 | ||
268 | /** |
||
269 | * |
||
270 | 6 | * @param mixed $target |
|
271 | 6 | * |
|
272 | * @return bool |
||
273 | 6 | */ |
|
274 | 5 | public function equals( $target ) { |
|
287 | 6 | ||
288 | 6 | private function statementsEqual( array $statements ) { |
|
301 | 3 | ||
302 | 3 | /** |
|
303 | * @return bool |
||
304 | 4 | */ |
|
305 | public function isEmpty() { |
||
308 | |||
309 | /** |
||
310 | * @since 3.0 |
||
311 | * @see StatementByGuidMap |
||
312 | * |
||
313 | * @param string|null $statementGuid |
||
314 | * |
||
315 | * @return Statement|null The first statement with the given GUID or null if not found. |
||
316 | 1 | */ |
|
317 | 1 | public function getFirstStatementWithGuid( $statementGuid ) { |
|
326 | |||
327 | /** |
||
328 | * @since 4.1 |
||
329 | * |
||
330 | * @param StatementFilter $filter |
||
331 | * |
||
332 | * @return self |
||
333 | */ |
||
334 | public function filter( StatementFilter $filter ) { |
||
345 | |||
346 | /** |
||
347 | * Removes all statements from this list. |
||
348 | * |
||
349 | * @since 7.0 |
||
350 | */ |
||
351 | public function clear() { |
||
354 | |||
355 | /** |
||
356 | * @see http://php.net/manual/en/language.oop5.cloning.php |
||
357 | * |
||
358 | * @since 5.1 |
||
359 | */ |
||
360 | public function __clone() { |
||
365 | |||
366 | } |
||
367 |