WebDevStudios /
CMB2
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 { |
||
| 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 | 44 | public static function add( CMB2 $cmb_instance ) { |
|
| 30 | 44 | self::$cmb2_instances[ $cmb_instance->cmb_id ] = $cmb_instance; |
|
| 31 | 44 | } |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Remove a CMB2 instance object to 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 | 32 | public static function get( $cmb_id ) { |
|
| 56 | 32 | if ( empty( self::$cmb2_instances ) || empty( self::$cmb2_instances[ $cmb_id ] ) ) { |
|
| 57 | 5 | return false; |
|
| 58 | } |
||
| 59 | |||
| 60 | 30 | 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 | * @since 2.2.3 |
||
| 76 | * @param string $property Property name. |
||
| 77 | * @param mixed $ignore The value to ignore. |
||
| 78 | * @return CMB2[] Array of matching cmb2 instances. |
||
| 79 | */ |
||
| 80 | public static function get_by_property( $property, $ignore = null ) { |
||
| 81 | $by_property[ $property ] = array(); |
||
|
0 ignored issues
–
show
|
|||
| 82 | |||
| 83 | foreach ( self::$cmb2_instances as $cmb_id => $cmb ) { |
||
| 84 | |||
| 85 | if ( $ignore === $cmb->prop( $property ) ) { |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | |||
| 89 | $by_property[ $property ][ $cmb_id ] = $cmb; |
||
| 90 | } |
||
| 91 | |||
| 92 | return $by_property[ $property ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | } |
||
| 96 |
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.