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