Complex classes like MediawikiEditEntity 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 MediawikiEditEntity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class MediawikiEditEntity implements EditEntity { |
||
38 | |||
39 | /** |
||
40 | * @var EntityRevisionLookup |
||
41 | */ |
||
42 | private $entityRevisionLookup; |
||
43 | |||
44 | /** |
||
45 | * @var EntityTitleStoreLookup |
||
46 | */ |
||
47 | private $titleLookup; |
||
48 | |||
49 | /** |
||
50 | * @var EntityStore |
||
51 | */ |
||
52 | private $entityStore; |
||
53 | |||
54 | /** |
||
55 | * @var EntityPermissionChecker |
||
56 | */ |
||
57 | private $permissionChecker; |
||
58 | |||
59 | /** |
||
60 | * @var EntityDiffer |
||
61 | */ |
||
62 | private $entityDiffer; |
||
63 | |||
64 | /** |
||
65 | * @var EntityPatcher |
||
66 | */ |
||
67 | private $entityPatcher; |
||
68 | |||
69 | /** |
||
70 | * The ID of the entity to edit. May be null if a new entity is being created. |
||
71 | * |
||
72 | * @var EntityId|null |
||
73 | */ |
||
74 | private $entityId = null; |
||
75 | |||
76 | /** |
||
77 | * @var EntityRevision|null |
||
78 | */ |
||
79 | private $baseRev = null; |
||
80 | |||
81 | /** |
||
82 | * @var int|bool |
||
83 | */ |
||
84 | private $baseRevId; |
||
85 | |||
86 | /** |
||
87 | * @var EntityRevision|null |
||
88 | */ |
||
89 | private $latestRev = null; |
||
90 | |||
91 | /** |
||
92 | * @var int |
||
93 | */ |
||
94 | private $latestRevId = 0; |
||
95 | |||
96 | /** |
||
97 | * @var Status|null |
||
98 | */ |
||
99 | private $status = null; |
||
100 | |||
101 | /** |
||
102 | * @var User|null |
||
103 | */ |
||
104 | private $user = null; |
||
105 | |||
106 | /** |
||
107 | * @var Title|null |
||
108 | */ |
||
109 | private $title = null; |
||
110 | |||
111 | /** |
||
112 | * @var EditFilterHookRunner |
||
113 | */ |
||
114 | private $editFilterHookRunner; |
||
115 | |||
116 | /** |
||
117 | * @var int Bit field for error types, using the EditEntity::XXX_ERROR constants. |
||
118 | */ |
||
119 | private $errorType = 0; |
||
120 | |||
121 | /** |
||
122 | * @var int |
||
123 | */ |
||
124 | private $maxSerializedEntitySize; |
||
125 | |||
126 | /** |
||
127 | * @var bool Can use a master connection or not |
||
128 | */ |
||
129 | private $allowMasterConnection; |
||
130 | |||
131 | /** |
||
132 | * @param EntityTitleStoreLookup $titleLookup |
||
133 | * @param EntityRevisionLookup $entityLookup |
||
134 | * @param EntityStore $entityStore |
||
135 | * @param EntityPermissionChecker $permissionChecker |
||
136 | * @param EntityDiffer $entityDiffer |
||
137 | * @param EntityPatcher $entityPatcher |
||
138 | * @param EntityId|null $entityId the ID of the entity being edited. |
||
139 | * May be null when creating a new entity. |
||
140 | * @param User $user the user performing the edit |
||
141 | * @param EditFilterHookRunner $editFilterHookRunner |
||
142 | * @param int $maxSerializedEntitySize the maximal allowed entity size in Kilobytes |
||
143 | * @param int $baseRevId the base revision ID for conflict checking. |
||
144 | * Use 0 to indicate that the current revision should be used as the base revision, |
||
145 | * effectively disabling conflict detections. true and false will be accepted for |
||
146 | * backwards compatibility, but both will be treated like 0. Note that the behavior |
||
147 | * of this class changed so that "late" conflicts that arise between edit conflict |
||
148 | * detection and database update are always detected, and result in the update to fail. |
||
149 | * @param bool $allowMasterConnection |
||
150 | */ |
||
151 | public function __construct( |
||
192 | |||
193 | /** |
||
194 | * Returns the ID of the entity being edited. |
||
195 | * May be null if a new entity is to be created. |
||
196 | * |
||
197 | * @return null|EntityId |
||
198 | */ |
||
199 | public function getEntityId() { |
||
202 | |||
203 | /** |
||
204 | * Returns the Title of the page holding the entity that is being edited. |
||
205 | * |
||
206 | * @return Title|null |
||
207 | */ |
||
208 | private function getTitle() { |
||
219 | |||
220 | /** |
||
221 | * Returns the latest revision of the entity. |
||
222 | * |
||
223 | * @return EntityRevision|null |
||
224 | */ |
||
225 | public function getLatestRevision() { |
||
242 | |||
243 | /** |
||
244 | * @return int 0 if the entity doesn't exist |
||
245 | */ |
||
246 | private function getLatestRevisionId() { |
||
271 | |||
272 | /** |
||
273 | * Is the entity new? |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | private function isNew() { |
||
280 | |||
281 | /** |
||
282 | * Does this entity belong to a new page? |
||
283 | * (An entity may {@link isNew be new}, and yet not belong to a new page, |
||
284 | * e. g. if it is stored in a non-main slot.) |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | private function isNewPage(): bool { |
||
295 | |||
296 | /** |
||
297 | * Return the ID of the base revision for the edit. If no base revision ID was supplied to |
||
298 | * the constructor, this returns the ID of the latest revision. If the entity does not exist |
||
299 | * yet, this returns 0. |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | private function getBaseRevisionId() { |
||
310 | |||
311 | /** |
||
312 | * Return the base revision for the edit. If no base revision ID was supplied to |
||
313 | * the constructor, this returns the latest revision. If the entity does not exist |
||
314 | * yet, this returns null. |
||
315 | * |
||
316 | * @return EntityRevision|null |
||
317 | * @throws MWException |
||
318 | */ |
||
319 | public function getBaseRevision() { |
||
343 | |||
344 | /** |
||
345 | * @return string |
||
346 | */ |
||
347 | private function getReplicaMode() { |
||
354 | |||
355 | /** |
||
356 | * Get the status object. Only defined after attemptSave() was called. |
||
357 | * |
||
358 | * After a successful save, the Status object's value field will contain an array, |
||
359 | * just like the status returned by WikiPage::doEditContent(). Well known fields |
||
360 | * in the status value are: |
||
361 | * |
||
362 | * - new: bool whether the edit created a new page |
||
363 | * - revision: Revision the new revision object |
||
364 | * - errorFlags: bit field indicating errors, see the XXX_ERROR constants. |
||
365 | * |
||
366 | * @return Status |
||
367 | */ |
||
368 | public function getStatus() { |
||
375 | |||
376 | /** |
||
377 | * Determines whether the last call to attemptSave was successful. |
||
378 | * |
||
379 | * @return bool false if attemptSave() failed, true otherwise |
||
380 | */ |
||
381 | public function isSuccess() { |
||
384 | |||
385 | /** |
||
386 | * Checks whether this EditEntity encountered any of the given error types while executing attemptSave(). |
||
387 | * |
||
388 | * @param int $errorType bit field using the EditEntity::XXX_ERROR constants. |
||
389 | * Defaults to EditEntity::ANY_ERROR. |
||
390 | * |
||
391 | * @return bool true if this EditEntity encountered any of the error types in $errorType, false otherwise. |
||
392 | */ |
||
393 | public function hasError( $errorType = EditEntity::ANY_ERROR ) { |
||
396 | |||
397 | /** |
||
398 | * Determines whether an edit conflict exists, that is, whether another user has edited the |
||
399 | * same item after the base revision was created. In other words, this method checks whether |
||
400 | * the base revision (as provided to the constructor) is still current. If no base revision |
||
401 | * was provided to the constructor, this will always return false. |
||
402 | * |
||
403 | * If the base revision is different from the current revision, this will return true even if |
||
404 | * the edit conflict is resolvable. Indeed, it is used to determine whether conflict resolution |
||
405 | * should be attempted. |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | public function hasEditConflict() { |
||
413 | |||
414 | /** |
||
415 | * Attempts to fix an edit conflict by patching the intended change into the latest revision after |
||
416 | * checking for conflicts. |
||
417 | * |
||
418 | * @param EntityDocument $newEntity |
||
419 | * |
||
420 | * @throws MWException |
||
421 | * @return null|EntityDocument The patched Entity, or null if patching failed. |
||
422 | */ |
||
423 | private function fixEditConflict( EntityDocument $newEntity ) { |
||
476 | |||
477 | /** |
||
478 | * Check if no edits were made by other users since the given revision. |
||
479 | * This makes the assumption that revision ids are monotonically increasing. |
||
480 | * |
||
481 | * @param User|null $user |
||
482 | * @param EntityId|null $entityId |
||
483 | * @param int|bool $lastRevId |
||
484 | * |
||
485 | * @return bool |
||
486 | */ |
||
487 | private function userWasLastToEdit( User $user = null, EntityId $entityId = null, $lastRevId = false ) { |
||
494 | |||
495 | /** |
||
496 | * Checks the necessary permissions to perform this edit. |
||
497 | * Per default, the 'edit' permission is checked. |
||
498 | * Use addRequiredPermission() to check more permissions. |
||
499 | * |
||
500 | * @param EntityDocument $newEntity |
||
501 | */ |
||
502 | private function checkEditPermissions( EntityDocument $newEntity ) { |
||
516 | |||
517 | /** |
||
518 | * Checks if rate limits have been exceeded. |
||
519 | */ |
||
520 | private function checkRateLimits() { |
||
528 | |||
529 | /** |
||
530 | * Make sure the given WebRequest contains a valid edit token. |
||
531 | * |
||
532 | * @param string $token The token to check. |
||
533 | * |
||
534 | * @return bool true if the token is valid |
||
535 | */ |
||
536 | public function isTokenOK( $token ) { |
||
553 | |||
554 | /** |
||
555 | * Resolve user specific default default for watch state, if $watch is null. |
||
556 | * |
||
557 | * @param boolean|null $watch |
||
558 | * |
||
559 | * @return bool |
||
560 | */ |
||
561 | private function getDesiredWatchState( $watch ) { |
||
568 | |||
569 | /** |
||
570 | * @param EntityId|null $id |
||
571 | * |
||
572 | * @throws InvalidArgumentException |
||
573 | */ |
||
574 | private function checkEntityId( EntityId $id = null ) { |
||
584 | |||
585 | /** |
||
586 | * @param EntityDocument $entity |
||
587 | * |
||
588 | * @throws ReadOnlyError |
||
589 | */ |
||
590 | private function checkReadOnly( EntityDocument $entity ) { |
||
601 | |||
602 | /** |
||
603 | * @param EntityDocument $entity |
||
604 | * @return bool |
||
605 | */ |
||
606 | private function entityTypeIsReadOnly( EntityDocument $entity ) { |
||
611 | |||
612 | /** |
||
613 | * Attempts to save the given Entity object. |
||
614 | * |
||
615 | * This method performs entity level permission checks, checks the edit toke, enforces rate |
||
616 | * limits, resolves edit conflicts, and updates user watchlists if appropriate. |
||
617 | * |
||
618 | * Success or failure are reported via the Status object returned by this method. |
||
619 | * |
||
620 | * @param EntityDocument $newEntity |
||
621 | * @param string $summary The edit summary. |
||
622 | * @param int $flags The EDIT_XXX flags as used by WikiPage::doEditContent(). |
||
623 | * Additionally, the EntityContent::EDIT_XXX constants can be used. |
||
624 | * @param string|bool $token Edit token to check, or false to disable the token check. |
||
625 | * Null will fail the token text, as will the empty string. |
||
626 | * @param bool|null $watch Whether the user wants to watch the entity. |
||
627 | * Set to null to apply default according to getWatchDefault(). |
||
628 | * @param string[] $tags Change tags to add to the edit. |
||
629 | * Callers are responsible for checking that the user is permitted to add these tags |
||
630 | * (typically using {@link ChangeTags::canAddTagsAccompanyingChange}). |
||
631 | * |
||
632 | * @return Status |
||
633 | * |
||
634 | * @throws MWException |
||
635 | * @throws ReadOnlyError |
||
636 | * |
||
637 | * @see WikiPage::doEditContent |
||
638 | * @see EntityStore::saveEntity |
||
639 | */ |
||
640 | public function attemptSave( EntityDocument $newEntity, $summary, $flags, $token, $watch = null, array $tags = [] ) { |
||
774 | |||
775 | /** |
||
776 | * Returns whether the present edit would, per default, |
||
777 | * lead to the user watching the page. |
||
778 | * |
||
779 | * This uses the user's watchdefault and watchcreations settings |
||
780 | * and considers whether the entity is already watched by the user. |
||
781 | * |
||
782 | * @note Keep in sync with logic in EditPage! |
||
783 | * |
||
784 | * @return bool |
||
785 | */ |
||
786 | private function getWatchDefault() { |
||
798 | |||
799 | /** |
||
800 | * Watches or unwatches the entity. |
||
801 | * |
||
802 | * @note Keep in sync with logic in EditPage! |
||
803 | * @todo move to separate service |
||
804 | * |
||
805 | * @param bool $watch whether to watch or unwatch the page. |
||
806 | * |
||
807 | * @throws MWException |
||
808 | */ |
||
809 | private function updateWatchlist( $watch ) { |
||
816 | |||
817 | } |
||
818 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: