CMB2_Boxes   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 40.91%

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 9
cts 22
cp 0.4091
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A remove() 0 5 2
A get() 0 7 3
A get_all() 0 3 1
A get_by_property() 0 14 3
1
<?php
2
3
/**
4
 * A CMB2 object instance registry for storing every CMB2 instance.
5
 *
6
 * @category  WordPress_Plugin
7
 * @package   CMB2
8
 * @author    WebDevStudios
9
 * @license   GPL-2.0+
10
 * @link      http://webdevstudios.com
11
 */
12
class CMB2_Boxes {
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...
13
14
	/**
15
	 * Array of all metabox objects.
16
	 *
17
	 * @since 2.0.0
18
	 * @var array
19
	 */
20
	protected static $cmb2_instances = array();
21
22
	/**
23
	 * Add a CMB2 instance object to the registry.
24
	 *
25
	 * @since 1.X.X
26
	 *
27
	 * @param CMB2 $cmb_instance CMB2 instance.
28
	 */
29 48
	public static function add( CMB2 $cmb_instance ) {
30 48
		self::$cmb2_instances[ $cmb_instance->cmb_id ] = $cmb_instance;
0 ignored issues
show
Documentation introduced by
The property $cmb_id is declared protected in CMB2_Base. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
31 48
	}
32
33
	/**
34
	 * Remove a CMB2 instance object from the registry.
35
	 *
36
	 * @since 1.X.X
37
	 *
38
	 * @param string $cmb_id A CMB2 instance id.
39
	 */
40
	public static function remove( $cmb_id ) {
41
		if ( array_key_exists( $cmb_id, self::$cmb2_instances ) ) {
42
			unset( self::$cmb2_instances[ $cmb_id ] );
43
		}
44
	}
45
46
	/**
47
	 * Retrieve a CMB2 instance by cmb id.
48
	 *
49
	 * @since 1.X.X
50
	 *
51
	 * @param string $cmb_id A CMB2 instance id.
52
	 *
53
	 * @return CMB2|bool False or CMB2 object instance.
54
	 */
55 34
	public static function get( $cmb_id ) {
56 34
		if ( empty( self::$cmb2_instances ) || empty( self::$cmb2_instances[ $cmb_id ] ) ) {
57 5
			return false;
58
		}
59
60 32
		return self::$cmb2_instances[ $cmb_id ];
61
	}
62
63
	/**
64
	 * Retrieve all CMB2 instances registered.
65
	 *
66
	 * @since  1.X.X
67
	 * @return CMB2[] Array of all registered cmb2 instances.
68
	 */
69 1
	public static function get_all() {
70 1
		return self::$cmb2_instances;
71
	}
72
73
	/**
74
	 * Retrieve all CMB2 instances that have the specified property set.
75
	 *
76
	 * @since  2.2.3
77
	 * @param  string $property Property name.
78
	 * @param  mixed  $ignore   The value to ignore.
79
	 * @return CMB2[]           Array of matching cmb2 instances.
80
	 */
81
	public static function get_by_property( $property, $ignore = null ) {
82
		$by_property[ $property ] = array();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$by_property was never initialized. Although not strictly required by PHP, it is generally a good practice to add $by_property = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
83
84
		foreach ( self::$cmb2_instances as $cmb_id => $cmb ) {
85
86
			if ( $ignore === $cmb->prop( $property ) ) {
87
				continue;
88
			}
89
90
			$by_property[ $property ][ $cmb_id ] = $cmb;
91
		}
92
93
		return $by_property[ $property ];
94
	}
95
96
}
97