Passed
Push — master ( c298dd...c4dd8e )
by Alain
24:29
created

AbstractDependencyHandler::maybe_enqueue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * AbstractDependencyHandler Class
4
 *
5
 * @package   BrightNucleus\Dependency
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Dependency;
13
14
use BrightNucleus\Exception\InvalidArgumentException;
15
use BrightNucleus\Invoker\FunctionInvokerTrait;
16
17
/**
18
 * Abstract dependency.
19
 *
20
 * @since   0.1.0
21
 *
22
 * @package BrightNucleus\Dependency
23
 */
24
abstract class AbstractDependencyHandler implements DependencyHandlerInterface {
25
26
	use FunctionInvokerTrait;
27
28
	/**
29
	 * Register the dependency's assets.
30
	 *
31
	 * @since 0.1.0
32
	 *
33
	 * @param array $args               Optional. Array of arguments that is
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
	 *                                  passed to the registration function.
35
	 * @throws InvalidArgumentException If the register function could not be
36
	 *                                  called.
37
	 */
38
	public function register( $args = null ) {
39
		$this->invokeFunction( $this->get_register_function(), $args );
0 ignored issues
show
Bug introduced by
It seems like $args defined by parameter $args on line 38 can also be of type null; however, BrightNucleus\Invoker\Fu...Trait::invokeFunction() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
	}
41
42
	/**
43
	 * Get the name of the function that is used for registering the dependency.
44
	 *
45
	 * @since 0.1.0
46
	 *
47
	 * @return string Function name.
48
	 */
49
	abstract protected function get_register_function();
50
51
	/**
52
	 * Enqueue the dependency's assets.
53
	 *
54
	 * @since 0.1.0
55
	 *
56
	 * @param array $args               Optional. Array of arguments that is
0 ignored issues
show
Documentation introduced by
Should the type for parameter $args not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
	 *                                  passed to the enqueueing function.
58
	 * @throws InvalidArgumentException If the register function could not be
59
	 *                                  called.
60
	 */
61
	public function enqueue( $args = null ) {
62
		$this->invokeFunction( $this->get_enqueue_function(), $args );
0 ignored issues
show
Bug introduced by
It seems like $args defined by parameter $args on line 61 can also be of type null; however, BrightNucleus\Invoker\Fu...Trait::invokeFunction() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
63
	}
64
65
	/**
66
	 * Get the name of the function that is used for enqueueing the dependency.
67
	 *
68
	 * @since 0.1.0
69
	 *
70
	 * @return string Function name.
71
	 */
72
	abstract protected function get_enqueue_function();
73
74
	/**
75
	 * Maybe enqueue a dependency that has been registered outside of the
76
	 * Dependency Manager.
77
	 *
78
	 * @since 0.2.3
79
	 *
80
	 * @param string $handle Handle of the dependency to enqueue.
81
	 */
82
	public function maybe_enqueue( $handle ) {
83
		if ( $this->is_registered( $handle ) ) {
84
			$enqueue = $this->get_enqueue_function();
85
			$enqueue( $handle );
86
		}
87
	}
88
89
	/**
90
	 * Check whether a specific handle has been registered.
91
	 *
92
	 * @since 0.2.3
93
	 *
94
	 * @param string $handle The handle to check
95
	 * @return bool Whether it is registered or not.
96
	 */
97
	abstract protected function is_registered( $handle );
98
}
99