| Conditions | 6 |
| Paths | 4 |
| Total Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 15 | public static function init( $container = null ) { |
||
| 16 | // The container holds and manages the lifecycle of our dependencies |
||
| 17 | // to make them easier to work with and increase flexibility. |
||
| 18 | if ( ! isset( $container ) ) { |
||
| 19 | require_once __DIR__ . '/class-container.php'; |
||
| 20 | $container = new Container(); |
||
| 21 | } |
||
| 22 | |||
| 23 | // phpcs:disable Generic.Commenting.DocComment.MissingShort |
||
| 24 | |||
| 25 | /** @var Plugin_Locator $plugin_locator */ |
||
| 26 | $plugin_locator = $container->get( Plugin_Locator::class ); |
||
| 27 | |||
| 28 | /** @var Plugins_Handler $plugins_handler */ |
||
| 29 | $plugins_handler = $container->get( Plugins_Handler::class ); |
||
| 30 | |||
| 31 | // The current plugin is the one that we are attempting to initialize here. |
||
| 32 | $current_plugin = $plugin_locator->find_current_plugin(); |
||
| 33 | |||
| 34 | // The cached plugins are all of those that were active or discovered by the autoloader during a previous request. |
||
| 35 | // Note that it's possible this list will include plugins that have since been deactivated, but after a request |
||
| 36 | // the cache should be updated and the deactivated plugins will be removed. |
||
| 37 | $cached_plugins = $plugins_handler->get_cached_plugins(); |
||
| 38 | |||
| 39 | // The active plugins are those that we were able to discover in on the site. This list will not include |
||
| 40 | // mu-plugins, those activated by code, or those who are hidden by filtering. |
||
| 41 | // By combining these lists we can preemptively load classes for plugins that are activated via another method. |
||
| 42 | // While this may result in us considering packages in deactivated plugins the request after they're removed, |
||
| 43 | // there shouldn't be any problems as a result and the eventual consistency is reliable enough. |
||
| 44 | $all_plugins = array_values( array_unique( array_merge( $plugins_handler->get_active_plugins(), $cached_plugins ) ) ); |
||
| 45 | |||
| 46 | /** @var Latest_Autoloader_Guard $guard */ |
||
| 47 | $guard = $container->get( Latest_Autoloader_Guard::class ); |
||
| 48 | if ( $guard->should_stop_init( $current_plugin, $all_plugins ) ) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** @var Autoloader_Handler $autoloader_handler */ |
||
| 53 | $autoloader_handler = $container->get( Autoloader_Handler::class ); |
||
| 54 | |||
| 55 | // Initialize the autoloader using the handler now that we're ready. |
||
| 56 | $autoloader_handler->create_autoloader( $all_plugins ); |
||
| 57 | |||
| 58 | /** @var Hook_Manager $hook_manager */ |
||
| 59 | $hook_manager = $container->get( Hook_Manager::class ); |
||
| 60 | |||
| 61 | // When the active and cached plugin lists do not match we should |
||
| 62 | // update the cache. This will prevent plugins that have been |
||
| 63 | // deactivated from being considered in other requests. |
||
| 64 | $hook_manager->add_action( |
||
| 65 | 'shutdown', |
||
| 66 | function () use ( $plugins_handler, $cached_plugins ) { |
||
| 67 | // Don't save a broken cache if an error happens during some plugin's initialization. |
||
| 68 | if ( ! did_action( 'plugins_loaded' ) ) { |
||
| 69 | // Ensure that the cache is emptied to prevent consecutive failures if the cache is to blame. |
||
| 70 | if ( ! empty( $cached_plugins ) ) { |
||
| 71 | $plugins_handler->cache_plugins( array() ); |
||
| 72 | } |
||
| 73 | |||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | // Load the active plugins fresh since the list we have above might not contain |
||
| 78 | // plugins that were activated but did not reset the autoloader. This happens |
||
| 79 | // because they were already included in the cache. |
||
| 80 | $active_plugins = $plugins_handler->get_active_plugins(); |
||
| 81 | |||
| 82 | // The paths should be sorted for easy comparisons with those loaded from the cache. |
||
| 83 | // Note we don't need to sort the cached entries because they're already sorted. |
||
| 84 | sort( $active_plugins ); |
||
| 85 | |||
| 86 | // We don't want to waste time saving a cache that hasn't changed. |
||
| 87 | if ( $cached_plugins === $active_plugins ) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | |||
| 91 | $plugins_handler->cache_plugins( $active_plugins ); |
||
| 92 | } |
||
| 93 | ); |
||
| 94 | |||
| 95 | // phpcs:enable Generic.Commenting.DocComment.MissingShort |
||
| 96 | } |
||
| 97 | |||
| 152 |