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, Comparable, Countable { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Statement[] |
||
| 35 | */ |
||
| 36 | private $statements = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param Statement[]|Traversable|Statement $statements |
||
| 40 | * @param Statement [$statement2,...] |
||
| 41 | * |
||
| 42 | * @throws InvalidArgumentException |
||
| 43 | */ |
||
| 44 | public function __construct( $statements = array() /*...*/ ) { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Returns the property ids used by the statements. |
||
| 64 | * The keys of the returned array hold the serializations of the property ids. |
||
| 65 | * |
||
| 66 | * @return PropertyId[] Array indexed by property id serialization. |
||
| 67 | */ |
||
| 68 | public function getPropertyIds() { |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @since 1.0, setting an index is supported since 6.1 |
||
| 80 | * @see ReferenceList::addReference |
||
| 81 | * |
||
| 82 | * @param Statement $statement |
||
| 83 | * @param int|null $index New position of the added statement, or null to append. |
||
| 84 | * |
||
| 85 | * @throws InvalidArgumentException |
||
| 86 | */ |
||
| 87 | public function addStatement( Statement $statement, $index = null ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param Snak $mainSnak |
||
| 99 | * @param Snak[]|SnakList|null $qualifiers |
||
| 100 | * @param Reference[]|ReferenceList|null $references |
||
| 101 | * @param string|null $guid |
||
| 102 | */ |
||
| 103 | public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references = null, $guid = null ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @since 3.0 |
||
| 115 | * |
||
| 116 | * @param string|null $guid |
||
| 117 | */ |
||
| 118 | public function removeStatementsWithGuid( $guid ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Statements that have a main snak already in the list are filtered out. |
||
| 130 | * The last occurrences are retained. |
||
| 131 | * |
||
| 132 | * @since 1.0 |
||
| 133 | * @deprecated since 6.1, use getMainSnaks instead |
||
| 134 | * |
||
| 135 | * @return self |
||
| 136 | */ |
||
| 137 | public function getWithUniqueMainSnaks() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @since 3.0 |
||
| 149 | * |
||
| 150 | * @param PropertyId $id |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function getByPropertyId( PropertyId $id ) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @since 3.0 |
||
| 168 | * |
||
| 169 | * @param int|int[] $acceptableRanks |
||
| 170 | * |
||
| 171 | * @return self |
||
| 172 | */ |
||
| 173 | public function getByRank( $acceptableRanks ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the so called "best statements". |
||
| 188 | * If there are preferred statements, then this is all the preferred statements. |
||
| 189 | * If there are no preferred statements, then this is all normal statements. |
||
| 190 | * |
||
| 191 | * @since 2.4 |
||
| 192 | * |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | public function getBestStatements() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns a list of all Snaks on this StatementList. This includes at least the main snaks of |
||
| 207 | * all statements, the snaks from qualifiers, and the snaks from references. |
||
| 208 | * |
||
| 209 | * This is a convenience method for use in code that needs to operate on all snaks, e.g. |
||
| 210 | * to find all referenced Entities. |
||
| 211 | * |
||
| 212 | * @since 1.1 |
||
| 213 | * |
||
| 214 | * @return Snak[] Numerically indexed (non-sparse) array. |
||
| 215 | */ |
||
| 216 | public function getAllSnaks() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @since 2.3 |
||
| 230 | * |
||
| 231 | * @return Snak[] Numerically indexed (non-sparse) array. |
||
| 232 | */ |
||
| 233 | public function getMainSnaks() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return Traversable|Statement[] |
||
| 245 | */ |
||
| 246 | public function getIterator() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return Statement[] Numerically indexed (non-sparse) array. |
||
| 252 | */ |
||
| 253 | public function toArray() { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @see Countable::count |
||
| 259 | * |
||
| 260 | * @return int |
||
| 261 | */ |
||
| 262 | public function count() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @see Comparable::equals |
||
| 268 | * |
||
| 269 | * @param mixed $target |
||
| 270 | * |
||
| 271 | * @return bool |
||
| 272 | */ |
||
| 273 | public function equals( $target ) { |
||
| 286 | |||
| 287 | private function statementsEqual( array $statements ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return bool |
||
| 303 | */ |
||
| 304 | public function isEmpty() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @since 3.0 |
||
| 310 | * @see StatementByGuidMap |
||
| 311 | * |
||
| 312 | * @param string|null $statementGuid |
||
| 313 | * |
||
| 314 | * @return Statement|null The first statement with the given GUID or null if not found. |
||
| 315 | */ |
||
| 316 | public function getFirstStatementWithGuid( $statementGuid ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @since 4.1 |
||
| 328 | * |
||
| 329 | * @param StatementFilter $filter |
||
| 330 | * |
||
| 331 | * @return self |
||
| 332 | */ |
||
| 333 | public function filter( StatementFilter $filter ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @see http://php.net/manual/en/language.oop5.cloning.php |
||
| 347 | * |
||
| 348 | * @since 5.1 |
||
| 349 | */ |
||
| 350 | public function __clone() { |
||
| 355 | |||
| 356 | } |
||
| 357 |