Passed
Push — add/multiplan ( be5784...8c4a89 )
by Warwick
04:27 queued 11s
created

Plan_Query::get_section_count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
namespace lsx_health_plan\classes\frontend;
3
4
/**
5
 * Holds the functionality to check and control your status with the current plan.
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Plan_Query {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\frontend\Plan_Query()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * Holds the sections for the current plan.
22
	 *
23
	 * @var array
24
	 */
25
	public $sections = array();
26
27
	/**
28
	 * Holds the variable true/false if the current plan has sections or not.
29
	 *
30
	 * @var array
31
	 */
32
	public $has_sections = false;	
33
34
	/**
35
	 * Contructor
36
	 */
37
	public function __construct() {
38
	}
39
40
	/**
41
	 * Return an instance of this class.
42
	 *
43
	 * @since 1.0.0
44
	 *
45
	 * @return    object \lsx_health_plan\classes\frontend\Plan_Query()    A single instance of this class.
46
	 */
47
	public static function get_instance() {
48
		// If the single instance hasn't been set, set it now.
49
		if ( null === self::$instance ) {
50
			self::$instance = new self();
51
		}
52
		return self::$instance;
53
	}
54
55
	/**
56
	 * Undocumented function
57
	 *
58
	 * @return void
59
	 */
60
	public function query_sections( $plan_id = '' ) {
61
		if ( '' === $plan_id ) {
62
			$plan_id = get_the_ID();
63
		}
64
65
		$section_array = get_post_meta( $plan_id, 'plan_sections', true );
66
		if ( ! empty( $section_array ) ) {
67
			$this->has_sections = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type true is incompatible with the declared type array of property $has_sections.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
			$this->sections     = $section_array;
0 ignored issues
show
Documentation Bug introduced by
It seems like $section_array can also be of type string. However, the property $sections is declared as type array. 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...
69
		}
70
		return $this->has_sections;
71
	}
72
73
	public function get_sections( $group = false ) {
74
		$sections = $this->sections;
75
		if ( false !== $group ) {
76
			$sections = $this->group_sections( $sections );
77
		}
78
		return $sections;
79
	}
80
81
	public function get_section_count() {
82
		return count( $this->sections );
83
	}
84
85
	/**
86
	 * This will group the sections by their "Group" field.
87
	 *
88
	 * @param  array $sections
89
	 * @return array
90
	 */
91
	public function group_sections( $sections = array() ) {
92
		$groups = array();
93
		if ( ! empty( $sections ) ) {
94
			foreach ( $sections as $section_key => $section_values ) {
95
				$group_key = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
96
				if ( isset( $section_values['group'] ) && '' !== $section_values['group'] ) {
97
					$group_key = $section_values['group'];
98
				}
99
				$group_key                            = sanitize_title( $group_key );
100
				$groups[ $group_key ][ $section_key ] = $section_values;
101
			}
102
		}
103
		return $groups;
104
	}
105
}
106