Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MediaWikiServices 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 MediaWikiServices, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
75 | class MediaWikiServices extends ServiceContainer { |
||
76 | |||
77 | /** |
||
78 | * @var MediaWikiServices|null |
||
79 | */ |
||
80 | private static $instance = null; |
||
81 | |||
82 | /** |
||
83 | * Returns the global default instance of the top level service locator. |
||
84 | * |
||
85 | * @since 1.27 |
||
86 | * |
||
87 | * The default instance is initialized using the service instantiator functions |
||
88 | * defined in ServiceWiring.php. |
||
89 | * |
||
90 | * @note This should only be called by static functions! The instance returned here |
||
91 | * should not be passed around! Objects that need access to a service should have |
||
92 | * that service injected into the constructor, never a service locator! |
||
93 | * |
||
94 | * @return MediaWikiServices |
||
95 | */ |
||
96 | public static function getInstance() { |
||
108 | |||
109 | /** |
||
110 | * Replaces the global MediaWikiServices instance. |
||
111 | * |
||
112 | * @since 1.28 |
||
113 | * |
||
114 | * @note This is for use in PHPUnit tests only! |
||
115 | * |
||
116 | * @throws MWException if called outside of PHPUnit tests. |
||
117 | * |
||
118 | * @param MediaWikiServices $services The new MediaWikiServices object. |
||
119 | * |
||
120 | * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later. |
||
121 | */ |
||
122 | public static function forceGlobalInstance( MediaWikiServices $services ) { |
||
132 | |||
133 | /** |
||
134 | * Creates a new instance of MediaWikiServices and sets it as the global default |
||
135 | * instance. getInstance() will return a different MediaWikiServices object |
||
136 | * after every call to resetGlobalInstance(). |
||
137 | * |
||
138 | * @since 1.28 |
||
139 | * |
||
140 | * @warning This should not be used during normal operation. It is intended for use |
||
141 | * when the configuration has changed significantly since bootstrap time, e.g. |
||
142 | * during the installation process or during testing. |
||
143 | * |
||
144 | * @warning Calling resetGlobalInstance() may leave the application in an inconsistent |
||
145 | * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to |
||
146 | * any of the services managed by MediaWikiServices exist. If any service objects |
||
147 | * managed by the old MediaWikiServices instance remain in use, they may INTERFERE |
||
148 | * with the operation of the services managed by the new MediaWikiServices. |
||
149 | * Operating with a mix of services created by the old and the new |
||
150 | * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS! |
||
151 | * Any class implementing LAZY LOADING is especially prone to this problem, |
||
152 | * since instances would typically retain a reference to a storage layer service. |
||
153 | * |
||
154 | * @see forceGlobalInstance() |
||
155 | * @see resetGlobalInstance() |
||
156 | * @see resetBetweenTest() |
||
157 | * |
||
158 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
159 | * 'BootstrapConfig' service. This has to contain at least the information |
||
160 | * needed to set up the 'ConfigFactory' service. If not given, the bootstrap |
||
161 | * config of the old instance of MediaWikiServices will be re-used. If there |
||
162 | * was no previous instance, a new GlobalVarConfig object will be used to |
||
163 | * bootstrap the services. |
||
164 | * |
||
165 | * @param string $quick Set this to "quick" to allow expensive resources to be re-used. |
||
166 | * See SalvageableService for details. |
||
167 | * |
||
168 | * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in |
||
169 | * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN |
||
170 | * is defined). |
||
171 | */ |
||
172 | public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) { |
||
196 | |||
197 | /** |
||
198 | * Salvages the state of any salvageable service instances in $other. |
||
199 | * |
||
200 | * @note $other will have been destroyed when salvage() returns. |
||
201 | * |
||
202 | * @param MediaWikiServices $other |
||
203 | */ |
||
204 | private function salvage( self $other ) { |
||
224 | |||
225 | /** |
||
226 | * Creates a new MediaWikiServices instance and initializes it according to the |
||
227 | * given $bootstrapConfig. In particular, all wiring files defined in the |
||
228 | * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called. |
||
229 | * |
||
230 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
231 | * 'BootstrapConfig' service. |
||
232 | * |
||
233 | * @param string $loadWiring set this to 'load' to load the wiring files specified |
||
234 | * in the 'ServiceWiringFiles' setting in $bootstrapConfig. |
||
235 | * |
||
236 | * @return MediaWikiServices |
||
237 | * @throws MWException |
||
238 | * @throws \FatalError |
||
239 | */ |
||
240 | private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) { |
||
254 | |||
255 | /** |
||
256 | * Disables all storage layer services. After calling this, any attempt to access the |
||
257 | * storage layer will result in an error. Use resetGlobalInstance() to restore normal |
||
258 | * operation. |
||
259 | * |
||
260 | * @since 1.28 |
||
261 | * |
||
262 | * @warning This is intended for extreme situations only and should never be used |
||
263 | * while serving normal web requests. Legitimate use cases for this method include |
||
264 | * the installation process. Test fixtures may also use this, if the fixture relies |
||
265 | * on globalState. |
||
266 | * |
||
267 | * @see resetGlobalInstance() |
||
268 | * @see resetChildProcessServices() |
||
269 | */ |
||
270 | public static function disableStorageBackend() { |
||
281 | |||
282 | /** |
||
283 | * Resets any services that may have become stale after a child process |
||
284 | * returns from after pcntl_fork(). It's also safe, but generally unnecessary, |
||
285 | * to call this method from the parent process. |
||
286 | * |
||
287 | * @since 1.28 |
||
288 | * |
||
289 | * @note This is intended for use in the context of process forking only! |
||
290 | * |
||
291 | * @see resetGlobalInstance() |
||
292 | * @see disableStorageBackend() |
||
293 | */ |
||
294 | public static function resetChildProcessServices() { |
||
303 | |||
304 | /** |
||
305 | * Resets the given service for testing purposes. |
||
306 | * |
||
307 | * @since 1.28 |
||
308 | * |
||
309 | * @warning This is generally unsafe! Other services may still retain references |
||
310 | * to the stale service instance, leading to failures and inconsistencies. Subclasses |
||
311 | * may use this method to reset specific services under specific instances, but |
||
312 | * it should not be exposed to application logic. |
||
313 | * |
||
314 | * @note With proper dependency injection used throughout the codebase, this method |
||
315 | * should not be needed. It is provided to allow tests that pollute global service |
||
316 | * instances to clean up. |
||
317 | * |
||
318 | * @param string $name |
||
319 | * @param bool $destroy Whether the service instance should be destroyed if it exists. |
||
320 | * When set to false, any existing service instance will effectively be detached |
||
321 | * from the container. |
||
322 | * |
||
323 | * @throws MWException if called outside of PHPUnit tests. |
||
324 | */ |
||
325 | View Code Duplication | public function resetServiceForTesting( $name, $destroy = true ) { |
|
332 | |||
333 | /** |
||
334 | * Convenience method that throws an exception unless it is called during a phase in which |
||
335 | * resetting of global services is allowed. In general, services should not be reset |
||
336 | * individually, since that may introduce inconsistencies. |
||
337 | * |
||
338 | * @since 1.28 |
||
339 | * |
||
340 | * This method will throw an exception if: |
||
341 | * |
||
342 | * - self::$resetInProgress is false (to allow all services to be reset together |
||
343 | * via resetGlobalInstance) |
||
344 | * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation) |
||
345 | * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing) |
||
346 | * |
||
347 | * This method is intended to be used to safeguard against accidentally resetting |
||
348 | * global service instances that are not yet managed by MediaWikiServices. It is |
||
349 | * defined here in the MediaWikiServices services class to have a central place |
||
350 | * for managing service bootstrapping and resetting. |
||
351 | * |
||
352 | * @param string $method the name of the caller method, as given by __METHOD__. |
||
353 | * |
||
354 | * @throws MWException if called outside bootstrap mode. |
||
355 | * |
||
356 | * @see resetGlobalInstance() |
||
357 | * @see forceGlobalInstance() |
||
358 | * @see disableStorageBackend() |
||
359 | */ |
||
360 | public static function failIfResetNotAllowed( $method ) { |
||
370 | |||
371 | /** |
||
372 | * @param Config $config The Config object to be registered as the 'BootstrapConfig' service. |
||
373 | * This has to contain at least the information needed to set up the 'ConfigFactory' |
||
374 | * service. |
||
375 | */ |
||
376 | public function __construct( Config $config ) { |
||
384 | |||
385 | // CONVENIENCE GETTERS //////////////////////////////////////////////////// |
||
386 | |||
387 | /** |
||
388 | * Returns the Config object containing the bootstrap configuration. |
||
389 | * Bootstrap configuration would typically include database credentials |
||
390 | * and other information that may be needed before the ConfigFactory |
||
391 | * service can be instantiated. |
||
392 | * |
||
393 | * @note This should only be used during bootstrapping, in particular |
||
394 | * when creating the MainConfig service. Application logic should |
||
395 | * use getMainConfig() to get a Config instances. |
||
396 | * |
||
397 | * @since 1.27 |
||
398 | * @return Config |
||
399 | */ |
||
400 | public function getBootstrapConfig() { |
||
403 | |||
404 | /** |
||
405 | * @since 1.27 |
||
406 | * @return ConfigFactory |
||
407 | */ |
||
408 | public function getConfigFactory() { |
||
411 | |||
412 | /** |
||
413 | * Returns the Config object that provides configuration for MediaWiki core. |
||
414 | * This may or may not be the same object that is returned by getBootstrapConfig(). |
||
415 | * |
||
416 | * @since 1.27 |
||
417 | * @return Config |
||
418 | */ |
||
419 | public function getMainConfig() { |
||
422 | |||
423 | /** |
||
424 | * @since 1.27 |
||
425 | * @return SiteLookup |
||
426 | */ |
||
427 | public function getSiteLookup() { |
||
430 | |||
431 | /** |
||
432 | * @since 1.27 |
||
433 | * @return SiteStore |
||
434 | */ |
||
435 | public function getSiteStore() { |
||
438 | |||
439 | /** |
||
440 | * @since 1.28 |
||
441 | * @return InterwikiLookup |
||
442 | */ |
||
443 | public function getInterwikiLookup() { |
||
446 | |||
447 | /** |
||
448 | * @since 1.27 |
||
449 | * @return StatsdDataFactory |
||
450 | */ |
||
451 | public function getStatsdDataFactory() { |
||
454 | |||
455 | /** |
||
456 | * @since 1.27 |
||
457 | * @return EventRelayerGroup |
||
458 | */ |
||
459 | public function getEventRelayerGroup() { |
||
462 | |||
463 | /** |
||
464 | * @since 1.27 |
||
465 | * @return SearchEngine |
||
466 | */ |
||
467 | public function newSearchEngine() { |
||
471 | |||
472 | /** |
||
473 | * @since 1.27 |
||
474 | * @return SearchEngineFactory |
||
475 | */ |
||
476 | public function getSearchEngineFactory() { |
||
479 | |||
480 | /** |
||
481 | * @since 1.27 |
||
482 | * @return SearchEngineConfig |
||
483 | */ |
||
484 | public function getSearchEngineConfig() { |
||
487 | |||
488 | /** |
||
489 | * @since 1.27 |
||
490 | * @return SkinFactory |
||
491 | */ |
||
492 | public function getSkinFactory() { |
||
495 | |||
496 | /** |
||
497 | * @since 1.28 |
||
498 | * @return LBFactory |
||
499 | */ |
||
500 | public function getDBLoadBalancerFactory() { |
||
503 | |||
504 | /** |
||
505 | * @since 1.28 |
||
506 | * @return LoadBalancer The main DB load balancer for the local wiki. |
||
507 | */ |
||
508 | public function getDBLoadBalancer() { |
||
511 | |||
512 | /** |
||
513 | * @since 1.28 |
||
514 | * @return WatchedItemStore |
||
515 | */ |
||
516 | public function getWatchedItemStore() { |
||
519 | |||
520 | /** |
||
521 | * @since 1.28 |
||
522 | * @return WatchedItemQueryService |
||
523 | */ |
||
524 | public function getWatchedItemQueryService() { |
||
527 | |||
528 | /** |
||
529 | * @since 1.28 |
||
530 | * @return CryptRand |
||
531 | */ |
||
532 | public function getCryptRand() { |
||
535 | |||
536 | /** |
||
537 | * @since 1.28 |
||
538 | * @return CryptHKDF |
||
539 | */ |
||
540 | public function getCryptHKDF() { |
||
543 | |||
544 | /** |
||
545 | * @since 1.28 |
||
546 | * @return MediaHandlerFactory |
||
547 | */ |
||
548 | public function getMediaHandlerFactory() { |
||
551 | |||
552 | /** |
||
553 | * @since 1.28 |
||
554 | * @return MimeAnalyzer |
||
555 | */ |
||
556 | public function getMimeAnalyzer() { |
||
559 | |||
560 | /** |
||
561 | * @since 1.28 |
||
562 | * @return ProxyLookup |
||
563 | */ |
||
564 | public function getProxyLookup() { |
||
567 | |||
568 | /** |
||
569 | * @since 1.28 |
||
570 | * @return GenderCache |
||
571 | */ |
||
572 | public function getGenderCache() { |
||
575 | |||
576 | /** |
||
577 | * @since 1.28 |
||
578 | * @return LinkCache |
||
579 | */ |
||
580 | public function getLinkCache() { |
||
583 | |||
584 | /** |
||
585 | * @since 1.28 |
||
586 | * @return LinkRendererFactory |
||
587 | */ |
||
588 | public function getLinkRendererFactory() { |
||
591 | |||
592 | /** |
||
593 | * LinkRenderer instance that can be used |
||
594 | * if no custom options are needed |
||
595 | * |
||
596 | * @since 1.28 |
||
597 | * @return LinkRenderer |
||
598 | */ |
||
599 | public function getLinkRenderer() { |
||
602 | |||
603 | /** |
||
604 | * @since 1.28 |
||
605 | * @return TitleFormatter |
||
606 | */ |
||
607 | public function getTitleFormatter() { |
||
610 | |||
611 | /** |
||
612 | * @since 1.28 |
||
613 | * @return TitleParser |
||
614 | */ |
||
615 | public function getTitleParser() { |
||
618 | |||
619 | /** |
||
620 | * @since 1.28 |
||
621 | * @return \BagOStuff |
||
622 | */ |
||
623 | public function getMainObjectStash() { |
||
626 | |||
627 | /** |
||
628 | * @since 1.28 |
||
629 | * @return \WANObjectCache |
||
630 | */ |
||
631 | public function getMainWANObjectCache() { |
||
634 | |||
635 | /** |
||
636 | * @since 1.28 |
||
637 | * @return \BagOStuff |
||
638 | */ |
||
639 | public function getLocalServerObjectCache() { |
||
642 | |||
643 | /** |
||
644 | * @since 1.28 |
||
645 | * @return VirtualRESTServiceClient |
||
646 | */ |
||
647 | public function getVirtualRESTServiceClient() { |
||
650 | |||
651 | /////////////////////////////////////////////////////////////////////////// |
||
652 | // NOTE: When adding a service getter here, don't forget to add a test |
||
653 | // case for it in MediaWikiServicesTest::provideGetters() and in |
||
654 | // MediaWikiServicesTest::provideGetService()! |
||
655 | /////////////////////////////////////////////////////////////////////////// |
||
656 | |||
657 | } |
||
658 |