Passed
Push — master ( 078687...6fac54 )
by Alain
02:20
created
src/DependencyManager.php 1 patch
Spacing   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 	 *
49 49
 	 * @var array;
50 50
 	 */
51
-	protected $dependencies = [ ];
51
+	protected $dependencies = [];
52 52
 
53 53
 	/**
54 54
 	 * Hold the handlers.
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
 	 *
58 58
 	 * @var DependencyHandlerInterface[]
59 59
 	 */
60
-	protected $handlers = [ ];
60
+	protected $handlers = [];
61 61
 
62 62
 	/**
63 63
 	 * Whether to enqueue immediately upon registration.
@@ -81,8 +81,8 @@  discard block
 block discarded – undo
81 81
 	 * @throws InvalidArgumentException If no dependency handlers were
82 82
 	 *                                  specified.
83 83
 	 */
84
-	public function __construct( ConfigInterface $config, $enqueue = true ) {
85
-		$this->processConfig( $config );
84
+	public function __construct(ConfigInterface $config, $enqueue = true) {
85
+		$this->processConfig($config);
86 86
 		$this->enqueue_immediately = $enqueue;
87 87
 		$this->init_handlers();
88 88
 		$this->init_dependencies();
@@ -94,10 +94,10 @@  discard block
 block discarded – undo
94 94
 	 * @since 0.1.0
95 95
 	 */
96 96
 	protected function init_handlers() {
97
-		$keys = [ self::KEY_SCRIPTS, self::KEY_STYLES ];
98
-		foreach ( $keys as $key ) {
99
-			if ( $this->hasConfigKey( $key ) ) {
100
-				$this->add_handler( $key );
97
+		$keys = [self::KEY_SCRIPTS, self::KEY_STYLES];
98
+		foreach ($keys as $key) {
99
+			if ($this->hasConfigKey($key)) {
100
+				$this->add_handler($key);
101 101
 			}
102 102
 		}
103 103
 	}
@@ -109,13 +109,13 @@  discard block
 block discarded – undo
109 109
 	 *
110 110
 	 * @param string $dependency The dependency type for which to add a handler.
111 111
 	 */
112
-	protected function add_handler( $dependency ) {
113
-		if ( $this->hasConfigKey( $dependency ) ) {
114
-			$handler = $this->hasConfigKey( self::KEY_HANDLERS, $dependency )
115
-				? $this->getConfigKey( self::KEY_HANDLERS, $dependency )
116
-				: $this->get_default_handler( $dependency );
117
-			if ( $handler ) {
118
-				$this->handlers[ $dependency ] = new $handler;
112
+	protected function add_handler($dependency) {
113
+		if ($this->hasConfigKey($dependency)) {
114
+			$handler = $this->hasConfigKey(self::KEY_HANDLERS, $dependency)
115
+				? $this->getConfigKey(self::KEY_HANDLERS, $dependency)
116
+				: $this->get_default_handler($dependency);
117
+			if ($handler) {
118
+				$this->handlers[$dependency] = new $handler;
119 119
 			}
120 120
 		}
121 121
 	}
@@ -128,8 +128,8 @@  discard block
 block discarded – undo
128 128
 	 * @param string $dependency The dependency that needs a handler.
129 129
 	 * @return string|null Class name of the handler. Null if none.
130 130
 	 */
131
-	protected function get_default_handler( $dependency ) {
132
-		switch ( $dependency ) {
131
+	protected function get_default_handler($dependency) {
132
+		switch ($dependency) {
133 133
 			case self::KEY_STYLES:
134 134
 				return self::DEFAULT_STYLE_HANDLER;
135 135
 			case self::KEY_SCRIPTS:
@@ -145,10 +145,10 @@  discard block
 block discarded – undo
145 145
 	 * @since 0.1.0
146 146
 	 */
147 147
 	protected function init_dependencies() {
148
-		array_walk( $this->handlers,
149
-			function ( $handler, $dependency_type ) {
150
-				if ( $this->hasConfigKey( $dependency_type ) ) {
151
-					$this->dependencies[ $dependency_type ] = $this->init_dependency_type( $dependency_type );
148
+		array_walk($this->handlers,
149
+			function($handler, $dependency_type) {
150
+				if ($this->hasConfigKey($dependency_type)) {
151
+					$this->dependencies[$dependency_type] = $this->init_dependency_type($dependency_type);
152 152
 				}
153 153
 			} );
154 154
 	}
@@ -161,13 +161,13 @@  discard block
 block discarded – undo
161 161
 	 * @param string $type The type of dependency to initialize.
162 162
 	 * @return array Array of dependency configurations.
163 163
 	 */
164
-	protected function init_dependency_type( $type ) {
165
-		$array = [ ];
166
-		$data  = $this->getConfigKey( $type );
167
-		foreach ( $data as $dependency ) {
168
-			$handle           = array_key_exists( 'handle',
169
-				$dependency ) ? $dependency['handle'] : '';
170
-			$array[ $handle ] = $dependency;
164
+	protected function init_dependency_type($type) {
165
+		$array = [];
166
+		$data  = $this->getConfigKey($type);
167
+		foreach ($data as $dependency) {
168
+			$handle           = array_key_exists('handle',
169
+				$dependency) ? $dependency['handle'] : '';
170
+			$array[$handle] = $dependency;
171 171
 		}
172 172
 		return $array;
173 173
 	}
@@ -179,10 +179,10 @@  discard block
 block discarded – undo
179 179
 	 *
180 180
 	 * @param mixed $context Optional. The context to pass to the dependencies.
181 181
 	 */
182
-	public function register( $context = null ) {
183
-		$context = $this->validate_context( $context );
184
-		array_walk( $this->dependencies,
185
-			[ $this, 'register_dependency_type' ], $context );
182
+	public function register($context = null) {
183
+		$context = $this->validate_context($context);
184
+		array_walk($this->dependencies,
185
+			[$this, 'register_dependency_type'], $context);
186 186
 	}
187 187
 
188 188
 	/**
@@ -193,9 +193,9 @@  discard block
 block discarded – undo
193 193
 	 * @param mixed $context The context as passed in by WordPress.
194 194
 	 * @return array Validated context.
195 195
 	 */
196
-	protected function validate_context( $context ) {
197
-		if ( is_string( $context ) ) {
198
-			return [ 'wp_context' => $context ];
196
+	protected function validate_context($context) {
197
+		if (is_string($context)) {
198
+			return ['wp_context' => $context];
199 199
 		}
200 200
 		return (array) $context;
201 201
 	}
@@ -208,11 +208,11 @@  discard block
 block discarded – undo
208 208
 	 * @param mixed $context  Optional. The context to pass to the
209 209
 	 *                        dependencies.
210 210
 	 */
211
-	public function enqueue( $context = null ) {
212
-		$context = $this->validate_context( $context );
211
+	public function enqueue($context = null) {
212
+		$context = $this->validate_context($context);
213 213
 
214
-		array_walk( $this->dependencies,
215
-			[ $this, 'enqueue_dependency_type' ], $context );
214
+		array_walk($this->dependencies,
215
+			[$this, 'enqueue_dependency_type'], $context);
216 216
 	}
217 217
 
218 218
 	/**
@@ -227,9 +227,9 @@  discard block
 block discarded – undo
227 227
 	 *                         outside of DependencyManager. Defaults to false.
228 228
 	 * @return bool Returns whether the handle was found or not.
229 229
 	 */
230
-	public function enqueue_handle( $handle, $context = null, $fallback = false ) {
231
-		if ( ! $this->enqueue_internal_handle( $handle, $context ) ) {
232
-			return $this->enqueue_fallback_handle( $handle );
230
+	public function enqueue_handle($handle, $context = null, $fallback = false) {
231
+		if ( ! $this->enqueue_internal_handle($handle, $context)) {
232
+			return $this->enqueue_fallback_handle($handle);
233 233
 		}
234 234
 		return true;
235 235
 	}
@@ -245,11 +245,11 @@  discard block
 block discarded – undo
245 245
 	 *                         dependencies.
246 246
 	 * @return bool Returns whether the handle was found or not.
247 247
 	 */
248
-	protected function enqueue_internal_handle( $handle, $context = null ) {
249
-		list( $dependency_type, $dependency ) = $this->get_dependency_array( $handle );
248
+	protected function enqueue_internal_handle($handle, $context = null) {
249
+		list($dependency_type, $dependency) = $this->get_dependency_array($handle);
250 250
 		$context['dependency_type'] = $dependency_type;
251 251
 
252
-		if ( ! $dependency ) {
252
+		if ( ! $dependency) {
253 253
 			return false;
254 254
 		}
255 255
 
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
 			$context
260 260
 		);
261 261
 
262
-		$this->maybe_localize( $dependency, $context );
262
+		$this->maybe_localize($dependency, $context);
263 263
 
264 264
 		return true;
265 265
 	}
@@ -273,14 +273,14 @@  discard block
 block discarded – undo
273 273
 	 * @return array Array containing the dependency key as well as the
274 274
 	 *                       dependency array itself.
275 275
 	 */
276
-	protected function get_dependency_array( $handle ) {
277
-		foreach ( $this->dependencies as $type => $dependencies ) {
278
-			if ( array_key_exists( $handle, $dependencies ) ) {
279
-				return [ $type, $dependencies[ $handle ] ];
276
+	protected function get_dependency_array($handle) {
277
+		foreach ($this->dependencies as $type => $dependencies) {
278
+			if (array_key_exists($handle, $dependencies)) {
279
+				return [$type, $dependencies[$handle]];
280 280
 			}
281 281
 		}
282 282
 		// Handle not found, return an empty array.
283
-		return [ '', null ];
283
+		return ['', null];
284 284
 	}
285 285
 
286 286
 	/**
@@ -295,12 +295,12 @@  discard block
 block discarded – undo
295 295
 	 *                               dependency at key
296 296
 	 *                               'dependency_type'.
297 297
 	 */
298
-	protected function enqueue_dependency( $dependency, $dependency_key, $context = null ) {
299
-		if ( ! $this->is_needed( $dependency, $context ) ) {
298
+	protected function enqueue_dependency($dependency, $dependency_key, $context = null) {
299
+		if ( ! $this->is_needed($dependency, $context)) {
300 300
 			return;
301 301
 		}
302
-		$handler = $this->handlers[ $context['dependency_type'] ];
303
-		$handler->enqueue( $dependency );
302
+		$handler = $this->handlers[$context['dependency_type']];
303
+		$handler->enqueue($dependency);
304 304
 	}
305 305
 
306 306
 	/**
@@ -314,16 +314,16 @@  discard block
 block discarded – undo
314 314
 	 *                          'dependency_type'.
315 315
 	 * @return bool Whether it is needed or not.
316 316
 	 */
317
-	protected function is_needed( $dependency, $context ) {
318
-		$is_needed = array_key_exists( 'is_needed', $dependency )
317
+	protected function is_needed($dependency, $context) {
318
+		$is_needed = array_key_exists('is_needed', $dependency)
319 319
 			? $dependency['is_needed']
320 320
 			: null;
321 321
 
322
-		if ( null === $is_needed ) {
322
+		if (null === $is_needed) {
323 323
 			return true;
324 324
 		}
325 325
 
326
-		return is_callable( $is_needed ) && $is_needed( $context );
326
+		return is_callable($is_needed) && $is_needed($context);
327 327
 	}
328 328
 
329 329
 	/**
@@ -336,18 +336,18 @@  discard block
 block discarded – undo
336 336
 	 *                          Contains the type of the dependency at key
337 337
 	 *                          'dependency_type'.
338 338
 	 */
339
-	protected function maybe_localize( $dependency, $context ) {
340
-		if ( ! array_key_exists( 'localize', $dependency ) ) {
339
+	protected function maybe_localize($dependency, $context) {
340
+		if ( ! array_key_exists('localize', $dependency)) {
341 341
 			return;
342 342
 		}
343 343
 
344 344
 		$localize = $dependency['localize'];
345 345
 		$data     = $localize['data'];
346
-		if ( is_callable( $data ) ) {
347
-			$data = $data( $context );
346
+		if (is_callable($data)) {
347
+			$data = $data($context);
348 348
 		}
349 349
 
350
-		\wp_localize_script( $dependency['handle'], $localize['name'], $data );
350
+		\wp_localize_script($dependency['handle'], $localize['name'], $data);
351 351
 	}
352 352
 
353 353
 	/**
@@ -359,10 +359,10 @@  discard block
 block discarded – undo
359 359
 	 * @param string $handle The dependency handle to enqueue.
360 360
 	 * @return bool Returns whether the handle was found or not.
361 361
 	 */
362
-	protected function enqueue_fallback_handle( $handle ) {
362
+	protected function enqueue_fallback_handle($handle) {
363 363
 		$result = false;
364
-		foreach ( $this->handlers as $handler ) {
365
-			$result = $result || $handler->maybe_enqueue( $handle );
364
+		foreach ($this->handlers as $handler) {
365
+			$result = $result || $handler->maybe_enqueue($handle);
366 366
 		}
367 367
 		return $result;
368 368
 	}
@@ -377,9 +377,9 @@  discard block
 block discarded – undo
377 377
 	 * @param mixed  $context         Optional. The context to pass to the
378 378
 	 *                                dependencies.
379 379
 	 */
380
-	protected function enqueue_dependency_type( $dependencies, $dependency_type, $context = null ) {
380
+	protected function enqueue_dependency_type($dependencies, $dependency_type, $context = null) {
381 381
 		$context['dependency_type'] = $dependency_type;
382
-		array_walk( $dependencies, [ $this, 'enqueue_dependency' ], $context );
382
+		array_walk($dependencies, [$this, 'enqueue_dependency'], $context);
383 383
 	}
384 384
 
385 385
 	/**
@@ -392,9 +392,9 @@  discard block
 block discarded – undo
392 392
 	 * @param mixed  $context         Optional. The context to pass to the
393 393
 	 *                                dependencies.
394 394
 	 */
395
-	protected function register_dependency_type( $dependencies, $dependency_type, $context = null ) {
395
+	protected function register_dependency_type($dependencies, $dependency_type, $context = null) {
396 396
 		$context['dependency_type'] = $dependency_type;
397
-		array_walk( $dependencies, [ $this, 'register_dependency' ], $context );
397
+		array_walk($dependencies, [$this, 'register_dependency'], $context);
398 398
 	}
399 399
 
400 400
 	/**
@@ -409,12 +409,12 @@  discard block
 block discarded – undo
409 409
 	 *                               dependency at key
410 410
 	 *                               'dependency_type'.
411 411
 	 */
412
-	protected function register_dependency( $dependency, $dependency_key, $context = null ) {
413
-		$handler = $this->handlers[ $context['dependency_type'] ];
414
-		$handler->register( $dependency );
412
+	protected function register_dependency($dependency, $dependency_key, $context = null) {
413
+		$handler = $this->handlers[$context['dependency_type']];
414
+		$handler->register($dependency);
415 415
 
416
-		if ( $this->enqueue_immediately ) {
417
-			$this->register_enqueue_hooks( $dependency, $context );
416
+		if ($this->enqueue_immediately) {
417
+			$this->register_enqueue_hooks($dependency, $context);
418 418
 		}
419 419
 	}
420 420
 
@@ -428,14 +428,14 @@  discard block
 block discarded – undo
428 428
 	 *                          Contains the type of the dependency at key
429 429
 	 *                          'dependency_type'.
430 430
 	 */
431
-	protected function register_enqueue_hooks( $dependency, $context = null ) {
432
-		$priority = $this->get_priority( $dependency );
431
+	protected function register_enqueue_hooks($dependency, $context = null) {
432
+		$priority = $this->get_priority($dependency);
433 433
 
434
-		foreach ( [ 'wp_enqueue_scripts', 'admin_enqueue_scripts' ] as $hook ) {
435
-			\add_action( $hook, [ $this, 'enqueue' ], $priority, 1 );
434
+		foreach (['wp_enqueue_scripts', 'admin_enqueue_scripts'] as $hook) {
435
+			\add_action($hook, [$this, 'enqueue'], $priority, 1);
436 436
 		}
437 437
 
438
-		$this->maybe_localize( $dependency, $context );
438
+		$this->maybe_localize($dependency, $context);
439 439
 	}
440 440
 
441 441
 	/**
@@ -446,9 +446,9 @@  discard block
 block discarded – undo
446 446
 	 * @param array $dependency Configuration data of the dependency.
447 447
 	 * @return int Priority to use.
448 448
 	 */
449
-	protected function get_priority( $dependency ) {
450
-		if ( array_key_exists( 'priority', $dependency ) ) {
451
-			return intval( $dependency['priority'] );
449
+	protected function get_priority($dependency) {
450
+		if (array_key_exists('priority', $dependency)) {
451
+			return intval($dependency['priority']);
452 452
 		}
453 453
 		return 10;
454 454
 	}
Please login to merge, or discard this patch.