Completed
Push — fix/legacy-sync-modules-names ( e46b01...9d6838 )
by
unknown
18:46 queued 11:15
created

Modules::reset_modules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Simple wrapper that allows enumerating cached static instances
4
 * of sync modules.
5
 *
6
 * @package automattic/jetpack-sync
7
 */
8
9
namespace Automattic\Jetpack\Sync;
10
11
/**
12
 * A class to handle loading of sync modules.
13
 */
14
class Modules {
15
16
	/**
17
	 * Lists classnames of sync modules we load by default.
18
	 *
19
	 * @access public
20
	 *
21
	 * @var array
22
	 */
23
	const DEFAULT_SYNC_MODULES = array(
24
		'Automattic\\Jetpack\\Sync\\Modules\\Constants',
25
		'Automattic\\Jetpack\\Sync\\Modules\\Callables',
26
		'Automattic\\Jetpack\\Sync\\Modules\\Network_Options',
27
		'Automattic\\Jetpack\\Sync\\Modules\\Options',
28
		'Automattic\\Jetpack\\Sync\\Modules\\Terms',
29
		'Automattic\\Jetpack\\Sync\\Modules\\Menus',
30
		'Automattic\\Jetpack\\Sync\\Modules\\Themes',
31
		'Automattic\\Jetpack\\Sync\\Modules\\Users',
32
		'Automattic\\Jetpack\\Sync\\Modules\\Import',
33
		'Automattic\\Jetpack\\Sync\\Modules\\Posts',
34
		'Automattic\\Jetpack\\Sync\\Modules\\Protect',
35
		'Automattic\\Jetpack\\Sync\\Modules\\Comments',
36
		'Automattic\\Jetpack\\Sync\\Modules\\Updates',
37
		'Automattic\\Jetpack\\Sync\\Modules\\Attachments',
38
		'Automattic\\Jetpack\\Sync\\Modules\\Meta',
39
		'Automattic\\Jetpack\\Sync\\Modules\\Plugins',
40
		'Automattic\\Jetpack\\Sync\\Modules\\Stats',
41
		'Automattic\\Jetpack\\Sync\\Modules\\Full_Sync',
42
		'Automattic\\Jetpack\\Sync\\Modules\\Term_Relationships',
43
	);
44
45
	/**
46
	 * Keeps track of initialized sync modules.
47
	 *
48
	 * @access private
49
	 * @static
50
	 *
51
	 * @var null|array
52
	 */
53
	private static $initialized_modules = null;
54
55
	/**
56
	 * Gets a list of initialized modules.
57
	 *
58
	 * @access public
59
	 * @static
60
	 *
61
	 * @return array|null
62
	 */
63
	public static function get_modules() {
64
		if ( null === self::$initialized_modules ) {
65
			self::$initialized_modules = self::initialize_modules();
66
		}
67
68
		return self::$initialized_modules;
69
	}
70
71
	/**
72
	 * Sets defaults for all initialized modules.
73
	 *
74
	 * @access public
75
	 * @static
76
	 */
77
	public static function set_defaults() {
78
		foreach ( self::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression self::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
79
			$module->set_defaults();
80
		}
81
	}
82
83
	/**
84
	 * Gets the name of an initialized module. Returns false if given module has not been initialized.
85
	 *
86
	 * @access public
87
	 * @static
88
	 *
89
	 * @param string $module_name A module name.
90
	 *
91
	 * @return bool|Automattic\Jetpack\Sync\Modules\Module
92
	 */
93
	public static function get_module( $module_name ) {
94
		foreach ( self::get_modules() as $module ) {
0 ignored issues
show
Bug introduced by
The expression self::get_modules() of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
95
			if ( $module->name() === $module_name ) {
96
				return $module;
97
			}
98
		}
99
100
		return false;
101
	}
102
103
	/**
104
	 * Loads and sets defaults for all declared modules.
105
	 *
106
	 * @access public
107
	 * @static
108
	 *
109
	 * @return array
110
	 */
111
	public static function initialize_modules() {
112
		/**
113
		 * Filters the list of class names of sync modules.
114
		 * If you add to this list, make sure any classes implement the
115
		 * Jetpack_Sync_Module interface.
116
		 *
117
		 * @since 4.2.0
118
		 */
119
		$modules = apply_filters( 'jetpack_sync_modules', self::DEFAULT_SYNC_MODULES );
120
121
		$modules = array_map( array( __CLASS__, 'load_module' ), $modules );
122
123
		return array_map( array( __CLASS__, 'set_module_defaults' ), $modules );
124
	}
125
126
	/**
127
	 * Returns an instance of the given module class.
128
	 *
129
	 * @access public
130
	 * @static
131
	 *
132
	 * @param string $module_class The classname of a Jetpack sync module.
133
	 *
134
	 * @return Automattic\Jetpack\Sync\Modules\Module
135
	 */
136
	public static function load_module( $module_class ) {
137
		return new $module_class();
138
	}
139
140
	/**
141
	 * Sets defaults for the given instance of a Jetpack sync module.
142
	 *
143
	 * @access public
144
	 * @static
145
	 *
146
	 * @param Automattic\Jetpack\Sync\Modules\Module $module Instance of a Jetpack sync module.
147
	 *
148
	 * @return Automattic\Jetpack\Sync\Modules\Module
149
	 */
150
	public static function set_module_defaults( $module ) {
151
		$module->set_defaults();
152
		if ( method_exists( $module, 'set_late_default' ) ) {
153
			add_action( 'init', array( $module, 'set_late_default' ), 90 );
154
		}
155
		return $module;
156
	}
157
}
158