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 |
||
| 73 | class MediaWikiServices extends ServiceContainer { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var MediaWikiServices|null |
||
| 77 | */ |
||
| 78 | private static $instance = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns the global default instance of the top level service locator. |
||
| 82 | * |
||
| 83 | * @since 1.27 |
||
| 84 | * |
||
| 85 | * The default instance is initialized using the service instantiator functions |
||
| 86 | * defined in ServiceWiring.php. |
||
| 87 | * |
||
| 88 | * @note This should only be called by static functions! The instance returned here |
||
| 89 | * should not be passed around! Objects that need access to a service should have |
||
| 90 | * that service injected into the constructor, never a service locator! |
||
| 91 | * |
||
| 92 | * @return MediaWikiServices |
||
| 93 | */ |
||
| 94 | public static function getInstance() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Replaces the global MediaWikiServices instance. |
||
| 109 | * |
||
| 110 | * @since 1.28 |
||
| 111 | * |
||
| 112 | * @note This is for use in PHPUnit tests only! |
||
| 113 | * |
||
| 114 | * @throws MWException if called outside of PHPUnit tests. |
||
| 115 | * |
||
| 116 | * @param MediaWikiServices $services The new MediaWikiServices object. |
||
| 117 | * |
||
| 118 | * @return MediaWikiServices The old MediaWikiServices object, so it can be restored later. |
||
| 119 | */ |
||
| 120 | public static function forceGlobalInstance( MediaWikiServices $services ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Creates a new instance of MediaWikiServices and sets it as the global default |
||
| 133 | * instance. getInstance() will return a different MediaWikiServices object |
||
| 134 | * after every call to resetGlobalInstance(). |
||
| 135 | * |
||
| 136 | * @since 1.28 |
||
| 137 | * |
||
| 138 | * @warning This should not be used during normal operation. It is intended for use |
||
| 139 | * when the configuration has changed significantly since bootstrap time, e.g. |
||
| 140 | * during the installation process or during testing. |
||
| 141 | * |
||
| 142 | * @warning Calling resetGlobalInstance() may leave the application in an inconsistent |
||
| 143 | * state. Calling this is only safe under the ASSUMPTION that NO REFERENCE to |
||
| 144 | * any of the services managed by MediaWikiServices exist. If any service objects |
||
| 145 | * managed by the old MediaWikiServices instance remain in use, they may INTERFERE |
||
| 146 | * with the operation of the services managed by the new MediaWikiServices. |
||
| 147 | * Operating with a mix of services created by the old and the new |
||
| 148 | * MediaWikiServices instance may lead to INCONSISTENCIES and even DATA LOSS! |
||
| 149 | * Any class implementing LAZY LOADING is especially prone to this problem, |
||
| 150 | * since instances would typically retain a reference to a storage layer service. |
||
| 151 | * |
||
| 152 | * @see forceGlobalInstance() |
||
| 153 | * @see resetGlobalInstance() |
||
| 154 | * @see resetBetweenTest() |
||
| 155 | * |
||
| 156 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 157 | * 'BootstrapConfig' service. This has to contain at least the information |
||
| 158 | * needed to set up the 'ConfigFactory' service. If not given, the bootstrap |
||
| 159 | * config of the old instance of MediaWikiServices will be re-used. If there |
||
| 160 | * was no previous instance, a new GlobalVarConfig object will be used to |
||
| 161 | * bootstrap the services. |
||
| 162 | * |
||
| 163 | * @param string $quick Set this to "quick" to allow expensive resources to be re-used. |
||
| 164 | * See SalvageableService for details. |
||
| 165 | * |
||
| 166 | * @throws MWException If called after MW_SERVICE_BOOTSTRAP_COMPLETE has been defined in |
||
| 167 | * Setup.php (unless MW_PHPUNIT_TEST or MEDIAWIKI_INSTALL or RUN_MAINTENANCE_IF_MAIN |
||
| 168 | * is defined). |
||
| 169 | */ |
||
| 170 | public static function resetGlobalInstance( Config $bootstrapConfig = null, $quick = '' ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Salvages the state of any salvageable service instances in $other. |
||
| 197 | * |
||
| 198 | * @note $other will have been destroyed when salvage() returns. |
||
| 199 | * |
||
| 200 | * @param MediaWikiServices $other |
||
| 201 | */ |
||
| 202 | private function salvage( self $other ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Creates a new MediaWikiServices instance and initializes it according to the |
||
| 225 | * given $bootstrapConfig. In particular, all wiring files defined in the |
||
| 226 | * ServiceWiringFiles setting are loaded, and the MediaWikiServices hook is called. |
||
| 227 | * |
||
| 228 | * @param Config|null $bootstrapConfig The Config object to be registered as the |
||
| 229 | * 'BootstrapConfig' service. |
||
| 230 | * |
||
| 231 | * @param string $loadWiring set this to 'load' to load the wiring files specified |
||
| 232 | * in the 'ServiceWiringFiles' setting in $bootstrapConfig. |
||
| 233 | * |
||
| 234 | * @return MediaWikiServices |
||
| 235 | * @throws MWException |
||
| 236 | * @throws \FatalError |
||
| 237 | */ |
||
| 238 | private static function newInstance( Config $bootstrapConfig, $loadWiring = '' ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Disables all storage layer services. After calling this, any attempt to access the |
||
| 255 | * storage layer will result in an error. Use resetGlobalInstance() to restore normal |
||
| 256 | * operation. |
||
| 257 | * |
||
| 258 | * @since 1.28 |
||
| 259 | * |
||
| 260 | * @warning This is intended for extreme situations only and should never be used |
||
| 261 | * while serving normal web requests. Legitimate use cases for this method include |
||
| 262 | * the installation process. Test fixtures may also use this, if the fixture relies |
||
| 263 | * on globalState. |
||
| 264 | * |
||
| 265 | * @see resetGlobalInstance() |
||
| 266 | * @see resetChildProcessServices() |
||
| 267 | */ |
||
| 268 | public static function disableStorageBackend() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Resets any services that may have become stale after a child process |
||
| 282 | * returns from after pcntl_fork(). It's also safe, but generally unnecessary, |
||
| 283 | * to call this method from the parent process. |
||
| 284 | * |
||
| 285 | * @since 1.28 |
||
| 286 | * |
||
| 287 | * @note This is intended for use in the context of process forking only! |
||
| 288 | * |
||
| 289 | * @see resetGlobalInstance() |
||
| 290 | * @see disableStorageBackend() |
||
| 291 | */ |
||
| 292 | public static function resetChildProcessServices() { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Resets the given service for testing purposes. |
||
| 304 | * |
||
| 305 | * @since 1.28 |
||
| 306 | * |
||
| 307 | * @warning This is generally unsafe! Other services may still retain references |
||
| 308 | * to the stale service instance, leading to failures and inconsistencies. Subclasses |
||
| 309 | * may use this method to reset specific services under specific instances, but |
||
| 310 | * it should not be exposed to application logic. |
||
| 311 | * |
||
| 312 | * @note With proper dependency injection used throughout the codebase, this method |
||
| 313 | * should not be needed. It is provided to allow tests that pollute global service |
||
| 314 | * instances to clean up. |
||
| 315 | * |
||
| 316 | * @param string $name |
||
| 317 | * @param bool $destroy Whether the service instance should be destroyed if it exists. |
||
| 318 | * When set to false, any existing service instance will effectively be detached |
||
| 319 | * from the container. |
||
| 320 | * |
||
| 321 | * @throws MWException if called outside of PHPUnit tests. |
||
| 322 | */ |
||
| 323 | View Code Duplication | public function resetServiceForTesting( $name, $destroy = true ) { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Convenience method that throws an exception unless it is called during a phase in which |
||
| 333 | * resetting of global services is allowed. In general, services should not be reset |
||
| 334 | * individually, since that may introduce inconsistencies. |
||
| 335 | * |
||
| 336 | * @since 1.28 |
||
| 337 | * |
||
| 338 | * This method will throw an exception if: |
||
| 339 | * |
||
| 340 | * - self::$resetInProgress is false (to allow all services to be reset together |
||
| 341 | * via resetGlobalInstance) |
||
| 342 | * - and MEDIAWIKI_INSTALL is not defined (to allow services to be reset during installation) |
||
| 343 | * - and MW_PHPUNIT_TEST is not defined (to allow services to be reset during testing) |
||
| 344 | * |
||
| 345 | * This method is intended to be used to safeguard against accidentally resetting |
||
| 346 | * global service instances that are not yet managed by MediaWikiServices. It is |
||
| 347 | * defined here in the MediaWikiServices services class to have a central place |
||
| 348 | * for managing service bootstrapping and resetting. |
||
| 349 | * |
||
| 350 | * @param string $method the name of the caller method, as given by __METHOD__. |
||
| 351 | * |
||
| 352 | * @throws MWException if called outside bootstrap mode. |
||
| 353 | * |
||
| 354 | * @see resetGlobalInstance() |
||
| 355 | * @see forceGlobalInstance() |
||
| 356 | * @see disableStorageBackend() |
||
| 357 | */ |
||
| 358 | public static function failIfResetNotAllowed( $method ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param Config $config The Config object to be registered as the 'BootstrapConfig' service. |
||
| 371 | * This has to contain at least the information needed to set up the 'ConfigFactory' |
||
| 372 | * service. |
||
| 373 | */ |
||
| 374 | public function __construct( Config $config ) { |
||
| 382 | |||
| 383 | // CONVENIENCE GETTERS //////////////////////////////////////////////////// |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the Config object containing the bootstrap configuration. |
||
| 387 | * Bootstrap configuration would typically include database credentials |
||
| 388 | * and other information that may be needed before the ConfigFactory |
||
| 389 | * service can be instantiated. |
||
| 390 | * |
||
| 391 | * @note This should only be used during bootstrapping, in particular |
||
| 392 | * when creating the MainConfig service. Application logic should |
||
| 393 | * use getMainConfig() to get a Config instances. |
||
| 394 | * |
||
| 395 | * @since 1.27 |
||
| 396 | * @return Config |
||
| 397 | */ |
||
| 398 | public function getBootstrapConfig() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @since 1.27 |
||
| 404 | * @return ConfigFactory |
||
| 405 | */ |
||
| 406 | public function getConfigFactory() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns the Config object that provides configuration for MediaWiki core. |
||
| 412 | * This may or may not be the same object that is returned by getBootstrapConfig(). |
||
| 413 | * |
||
| 414 | * @since 1.27 |
||
| 415 | * @return Config |
||
| 416 | */ |
||
| 417 | public function getMainConfig() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @since 1.27 |
||
| 423 | * @return SiteLookup |
||
| 424 | */ |
||
| 425 | public function getSiteLookup() { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @since 1.27 |
||
| 431 | * @return SiteStore |
||
| 432 | */ |
||
| 433 | public function getSiteStore() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @since 1.28 |
||
| 439 | * @return InterwikiLookup |
||
| 440 | */ |
||
| 441 | public function getInterwikiLookup() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @since 1.27 |
||
| 447 | * @return StatsdDataFactory |
||
| 448 | */ |
||
| 449 | public function getStatsdDataFactory() { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @since 1.27 |
||
| 455 | * @return EventRelayerGroup |
||
| 456 | */ |
||
| 457 | public function getEventRelayerGroup() { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @since 1.27 |
||
| 463 | * @return SearchEngine |
||
| 464 | */ |
||
| 465 | public function newSearchEngine() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @since 1.27 |
||
| 472 | * @return SearchEngineFactory |
||
| 473 | */ |
||
| 474 | public function getSearchEngineFactory() { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @since 1.27 |
||
| 480 | * @return SearchEngineConfig |
||
| 481 | */ |
||
| 482 | public function getSearchEngineConfig() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @since 1.27 |
||
| 488 | * @return SkinFactory |
||
| 489 | */ |
||
| 490 | public function getSkinFactory() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @since 1.28 |
||
| 496 | * @return LBFactory |
||
| 497 | */ |
||
| 498 | public function getDBLoadBalancerFactory() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * @since 1.28 |
||
| 504 | * @return LoadBalancer The main DB load balancer for the local wiki. |
||
| 505 | */ |
||
| 506 | public function getDBLoadBalancer() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @since 1.28 |
||
| 512 | * @return WatchedItemStore |
||
| 513 | */ |
||
| 514 | public function getWatchedItemStore() { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @since 1.28 |
||
| 520 | * @return WatchedItemQueryService |
||
| 521 | */ |
||
| 522 | public function getWatchedItemQueryService() { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @since 1.28 |
||
| 528 | * @return CryptRand |
||
| 529 | */ |
||
| 530 | public function getCryptRand() { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @since 1.28 |
||
| 536 | * @return MediaHandlerFactory |
||
| 537 | */ |
||
| 538 | public function getMediaHandlerFactory() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * @since 1.28 |
||
| 544 | * @return ProxyLookup |
||
| 545 | */ |
||
| 546 | public function getProxyLookup() { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @since 1.28 |
||
| 552 | * @return GenderCache |
||
| 553 | */ |
||
| 554 | public function getGenderCache() { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @since 1.28 |
||
| 560 | * @return LinkCache |
||
| 561 | */ |
||
| 562 | public function getLinkCache() { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @since 1.28 |
||
| 568 | * @return LinkRendererFactory |
||
| 569 | */ |
||
| 570 | public function getLinkRendererFactory() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * LinkRenderer instance that can be used |
||
| 576 | * if no custom options are needed |
||
| 577 | * |
||
| 578 | * @since 1.28 |
||
| 579 | * @return LinkRenderer |
||
| 580 | */ |
||
| 581 | public function getLinkRenderer() { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @since 1.28 |
||
| 587 | * @return TitleFormatter |
||
| 588 | */ |
||
| 589 | public function getTitleFormatter() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @since 1.28 |
||
| 595 | * @return TitleParser |
||
| 596 | */ |
||
| 597 | public function getTitleParser() { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @since 1.28 |
||
| 603 | * @return \BagOStuff |
||
| 604 | */ |
||
| 605 | public function getMainObjectStash() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * @since 1.28 |
||
| 611 | * @return \WANObjectCache |
||
| 612 | */ |
||
| 613 | public function getMainWANObjectCache() { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * @since 1.28 |
||
| 619 | * @return \BagOStuff |
||
| 620 | */ |
||
| 621 | public function getLocalServerObjectCache() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * @since 1.28 |
||
| 627 | * @return VirtualRESTServiceClient |
||
| 628 | */ |
||
| 629 | public function getVirtualRESTServiceClient() { |
||
| 632 | |||
| 633 | /////////////////////////////////////////////////////////////////////////// |
||
| 634 | // NOTE: When adding a service getter here, don't forget to add a test |
||
| 635 | // case for it in MediaWikiServicesTest::provideGetters() and in |
||
| 636 | // MediaWikiServicesTest::provideGetService()! |
||
| 637 | /////////////////////////////////////////////////////////////////////////// |
||
| 638 | |||
| 639 | } |
||
| 640 |