Completed
Push — trunk ( 4b0f72...9966d5 )
by Justin
07:19
created

CMB2_Hookup_Base::once()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 4
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Base class for hooking CMB2 into WordPress.
4
 *
5
 * @since  2.2.0
6
 *
7
 * @category  WordPress_Plugin
8
 * @package   CMB2
9
 * @author    WebDevStudios
10
 * @license   GPL-2.0+
11
 * @link      http://webdevstudios.com
12
 */
13
abstract class CMB2_Hookup_Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
	/**
16
	 * @var   CMB2 object
17
	 * @since 2.0.2
18
	 */
19
	protected $cmb;
20
21
	/**
22
	 * The object type we are performing the hookup for
23
	 * @var   string
24
	 * @since 2.0.9
25
	 */
26
	protected $object_type = 'post';
27
28
	/**
29
	 * Constructor
30
	 * @since 2.0.0
31
	 * @param CMB2 $cmb The CMB2 object to hookup
32
	 */
33
	public function __construct( CMB2 $cmb ) {
34
		$this->cmb = $cmb;
35
		$this->object_type = $this->cmb->mb_object_type();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cmb->mb_object_type() can also be of type false. However, the property $object_type is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
	}
37
38
	abstract public function universal_hooks();
39
40
	/**
41
	 * Ensures WordPress hook only gets fired once per object.
42
	 * @since  2.0.0
43
	 * @param string   $action        The name of the filter to hook the $hook callback to.
44
	 * @param callback $hook          The callback to be run when the filter is applied.
45
	 * @param integer  $priority      Order the functions are executed
46
	 * @param int      $accepted_args The number of arguments the function accepts.
47
	 */
48
	public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) {
49
		static $hooks_completed = array();
50
51
		$args = func_get_args();
52
53
		// Get object hash.. This bypasses issues with serializing closures.
54
		if ( is_object( $hook ) ) {
55
			$args[1] = spl_object_hash( $args[1] );
56
		} elseif ( is_array( $hook ) && is_object( $hook[0] ) ) {
57
			$args[1][0] = spl_object_hash( $hook[0] );
58
		}
59
60
		$key = md5( serialize( $args ) );
61
62
		if ( ! isset( $hooks_completed[ $key ] ) ) {
63
			$hooks_completed[ $key ] = 1;
64
			add_filter( $action, $hook, $priority, $accepted_args );
65
		}
66
	}
67
68
}
69