Complex classes like DeferredUpdates 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 DeferredUpdates, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class DeferredUpdates { |
||
| 44 | /** @var DeferrableUpdate[] Updates to be deferred until before request end */ |
||
| 45 | private static $preSendUpdates = []; |
||
| 46 | /** @var DeferrableUpdate[] Updates to be deferred until after request end */ |
||
| 47 | private static $postSendUpdates = []; |
||
| 48 | |||
| 49 | const ALL = 0; // all updates; in web requests, use only after flushing the output buffer |
||
| 50 | const PRESEND = 1; // for updates that should run before flushing output buffer |
||
| 51 | const POSTSEND = 2; // for updates that should run after flushing output buffer |
||
| 52 | |||
| 53 | const BIG_QUEUE_SIZE = 100; |
||
| 54 | |||
| 55 | /** @var array|null Information about the current execute() call or null if not running */ |
||
| 56 | private static $executeContext; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add an update to the deferred list to be run later by execute() |
||
| 60 | * |
||
| 61 | * In CLI mode, callback magic will also be used to run updates when safe |
||
| 62 | * |
||
| 63 | * @param DeferrableUpdate $update Some object that implements doUpdate() |
||
| 64 | * @param integer $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27) |
||
| 65 | */ |
||
| 66 | public static function addUpdate( DeferrableUpdate $update, $stage = self::POSTSEND ) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add a callable update. In a lot of cases, we just need a callback/closure, |
||
| 90 | * defining a new DeferrableUpdate object is not necessary |
||
| 91 | * |
||
| 92 | * @see MWCallableUpdate::__construct() |
||
| 93 | * |
||
| 94 | * @param callable $callable |
||
| 95 | * @param integer $stage DeferredUpdates constant (PRESEND or POSTSEND) (since 1.27) |
||
| 96 | * @param IDatabase|null $dbw Abort if this DB is rolled back [optional] (since 1.28) |
||
| 97 | */ |
||
| 98 | public static function addCallableUpdate( |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Do any deferred updates and clear the list |
||
| 106 | * |
||
| 107 | * @param string $mode Use "enqueue" to use the job queue when possible [Default: "run"] |
||
| 108 | * @param integer $stage DeferredUpdates constant (PRESEND, POSTSEND, or ALL) (since 1.27) |
||
| 109 | */ |
||
| 110 | public static function doUpdates( $mode = 'run', $stage = self::ALL ) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param DeferrableUpdate[] $queue |
||
| 124 | * @param DeferrableUpdate $update |
||
| 125 | */ |
||
| 126 | private static function push( array &$queue, DeferrableUpdate $update ) { |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param DeferrableUpdate[] &$queue List of DeferrableUpdate objects |
||
| 143 | * @param string $mode Use "enqueue" to use the job queue when possible |
||
| 144 | * @param integer $stage Class constant (PRESEND, POSTSEND) (since 1.28) |
||
| 145 | * @throws ErrorPageError Happens on top-level calls |
||
| 146 | * @throws Exception Happens on second-level calls |
||
| 147 | */ |
||
| 148 | public static function execute( array &$queue, $mode, $stage ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param DeferrableUpdate $update |
||
| 217 | * @param LBFactory $lbFactory |
||
| 218 | * @param integer $stage |
||
| 219 | * @return ErrorPageError|null |
||
| 220 | */ |
||
| 221 | private static function runUpdate( DeferrableUpdate $update, LBFactory $lbFactory, $stage ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Run all deferred updates immediately if there are no DB writes active |
||
| 240 | * |
||
| 241 | * If $mode is 'run' but there are busy databates, EnqueueableDataUpdate |
||
| 242 | * tasks will be enqueued anyway for the sake of progress. |
||
| 243 | * |
||
| 244 | * @param string $mode Use "enqueue" to use the job queue when possible |
||
| 245 | * @return bool Whether updates were allowed to run |
||
| 246 | * @since 1.28 |
||
| 247 | */ |
||
| 248 | public static function tryOpportunisticExecute( $mode = 'run' ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Enqueue a job for each EnqueueableDataUpdate item and return the other items |
||
| 272 | * |
||
| 273 | * @param DeferrableUpdate[] $updates A list of deferred update instances |
||
| 274 | * @return DeferrableUpdate[] Remaining updates that do not support being queued |
||
| 275 | */ |
||
| 276 | private static function enqueueUpdates( array $updates ) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @return integer Number of enqueued updates |
||
| 293 | * @since 1.28 |
||
| 294 | */ |
||
| 295 | public static function pendingUpdatesCount() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Clear all pending updates without performing them. Generally, you don't |
||
| 301 | * want or need to call this. Unit tests need it though. |
||
| 302 | */ |
||
| 303 | public static function clearPendingUpdates() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return IDatabase[] Connection where commit() cannot be called yet |
||
| 310 | */ |
||
| 311 | private static function getBusyDbConnections() { |
||
| 325 | } |
||
| 326 |