Completed
Push — try/e2e-check-if-chrome-instal... ( 24dbcf...296824 )
by Yaroslav
23:03 queued 13:54
created

Hook_Manager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 25.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 16
loc 62
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A add_action() 8 8 1
A add_filter() 8 8 1
A reset() 0 8 3

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * Allows the latest autoloader to register hooks that can be removed when the autoloader is reset.
6
 */
7
class Hook_Manager {
8
9
	/**
10
	 * An array containing all of the hooks that we've registered.
11
	 *
12
	 * @var array
13
	 */
14
	private $registered_hooks;
15
16
	/**
17
	 * The constructor.
18
	 */
19
	public function __construct() {
20
		$this->registered_hooks = array();
21
	}
22
23
	/**
24
	 * Adds an action to WordPress and registers it internally.
25
	 *
26
	 * @param string   $tag           The name of the action which is hooked.
27
	 * @param callable $callable      The function to call.
28
	 * @param int      $priority      Used to specify the priority of the action.
29
	 * @param int      $accepted_args Used to specify the number of arguments the callable accepts.
30
	 */
31 View Code Duplication
	public function add_action( $tag, $callable, $priority = 10, $accepted_args = 1 ) {
32
		$this->registered_hooks[ $tag ][] = array(
33
			'priority' => $priority,
34
			'callable' => $callable,
35
		);
36
37
		add_action( $tag, $callable, $priority, $accepted_args );
38
	}
39
40
	/**
41
	 * Adds a filter to WordPress and registers it internally.
42
	 *
43
	 * @param string   $tag           The name of the filter which is hooked.
44
	 * @param callable $callable      The function to call.
45
	 * @param int      $priority      Used to specify the priority of the filter.
46
	 * @param int      $accepted_args Used to specify the number of arguments the callable accepts.
47
	 */
48 View Code Duplication
	public function add_filter( $tag, $callable, $priority = 10, $accepted_args = 1 ) {
49
		$this->registered_hooks[ $tag ][] = array(
50
			'priority' => $priority,
51
			'callable' => $callable,
52
		);
53
54
		add_filter( $tag, $callable, $priority, $accepted_args );
55
	}
56
57
	/**
58
	 * Removes all of the registered hooks.
59
	 */
60
	public function reset() {
61
		foreach ( $this->registered_hooks as $tag => $hooks ) {
62
			foreach ( $hooks as $hook ) {
63
				remove_filter( $tag, $hook['callable'], $hook['priority'] );
64
			}
65
		}
66
		$this->registered_hooks = array();
67
	}
68
}
69