VariableParameter   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createWithAttributes() 0 9 2
A getParameterType() 0 3 1
A create() 0 7 1
A setValue() 0 9 2
A addValue() 0 19 4
1
<?php
2
	
3
/**
4
	* VariableParameter.php
5
	*/
6
	
7
namespace netfocusinc\argh;
8
9
use netfocusinc\argh\ArghException;
10
use netfocusinc\argh\Parameter;
11
12
/**
13
	* Variable parameter.
14
	*
15
	* Subtype of Parameter that represents an (unmarked) variable.
16
	* Variable Parameters are used to save unmakred input (naked variables)
17
	* Their value always consists of an array.
18
	*
19
	* @api
20
	*
21
	* @author Benjamin Hough
22
	*
23
	* @since 1.0.0
24
	*/
25
class VariableParameter extends Parameter
26
{
27
	
28
	//
29
	// STATIC FUNCTIONS
30
	//
31
	
32
	/**
33
		* Returns an instance of VariableParameter
34
		*
35
		* VariableParameters are always named with the ARGH_NAME_VARIABLE constant
36
		*
37
		* @since 1.0.1
38
		*
39
		* @return Parameter
40
		*/
41
	public static function create() : Parameter
42
	{	
43
		return parent::createWithAttributes(
44
			[
45
				'name' => Parameter::ARGH_NAME_VARIABLE,
46
				'required' => FALSE,
47
				'description' => 'Unnamed argument inputs' 
48
			]
49
		);
50
	}
51
	
52
	/**
53
		* Returns an instance of VariableParameter
54
		*
55
		* Overriding Parameter::createWithAttribute() here prevents this from being called with a custom 'name' attribute
56
		* VariableParameters need to be named with ARGH_NAME_VARIABLE constant; Argh uses this 'name' to find variables
57
		*
58
		* @since 1.0.2
59
		*
60
		* @param array $attributes
61
		*
62
		* @return Parameter
63
		*/
64
	public static function createWithAttributes(array $attributes) : Parameter
65
	{	
66
		// Force VariableParameters to use a constant name
67
		if(array_key_exists('name', $attributes))
68
		{
69
			$attributes['name'] = Parameter::ARGH_NAME_VARIABLE;
70
		}
71
		
72
		return parent::createWithAttributes($attributes);
73
	}
74
	
75
	//
76
	// PUBLIC FUNCTIONS
77
	//
78
	
79
	/**
80
		* Returns ARGH_TYPE_VARIABLE
81
		*
82
		* @since 1.0.0
83
		*
84
		* @return int
85
		*/
86
	public function getParameterType(): int
87
	{
88
		return Parameter::ARGH_TYPE_VARIABLE;
89
	}
90
91
	/**
92
		* Sets the array value of this Parameter.
93
		*
94
		* Forces all values into an array
95
		*
96
		* @since 1.0.0
97
		*
98
		* @param mixed $value
99
		*/
100
	public function setValue($value)
101
	{	
102
		if(is_array($value))
103
		{
104
			$this->value = $value;
105
		}
106
		else
107
		{
108
			$this->value = array($value);
109
		}
110
	}
111
112
	/**
113
		* Adds an element to the value of this Parameter
114
		*
115
		* Forces all values into an array
116
		*
117
		* @since 1.0.0
118
		*
119
		* @param mixed $value
120
		*
121
		*/	
122
	public function addValue($value)
123
	{
124
		// Check if this Parameter has a previously set value
125
		if($this->value === null)
126
		{
127
			// Initialize this Parameters value to a new array
128
			$this->value = array();
129
		}
130
		
131
		// Check if the new value is an array
132
		if(!is_array($value))
133
		{
134
			// Append new single value to this Parameters value array
135
			$this->value[] = $value;
136
		}
137
		else
138
		{
139
			// Append every new value to this Parameters value array
140
			foreach($value as $v) $this->value[] = $v;
141
		}
142
	}
143
	
144
}
145
146
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...