Passed
Push — master ( 35cf85...32fc48 )
by Jesse
05:09 queued 02:14
created

ArgumentLexeme::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace donatj\Printf;
4
5
class ArgumentLexeme extends Lexeme {
6
7
	/** @var string the argument is treated as an integer and presented as a binary number. */
8
	public const T_INT_AS_BINARY = 'b';
9
	/** @var string the argument is treated as an integer and presented as the character with that ASCII value. */
10
	public const T_INT_AS_CHARACTER = 'c';
11
	/** @var string the argument is treated as an integer and presented as a (signed) decimal number. */
12
	public const T_INT = 'd';
13
	/**
14
	 * @var string the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the
15
	 *     number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of
16
	 *     significant digits (one less).
17
	 */
18
	public const T_DOUBLE_AS_SCI = 'e';
19
	/** @var string like %e but uses uppercase letter (e.g. 1.2E+2). */
20
	public const T_DOUBLE_AS_SCI_CAP = 'E';
21
	/** @var string the argument is treated as a float and presented as a floating-point number (locale aware). */
22
	public const T_FLOAT_LOCALE = 'f';
23
	/**
24
	 * @var string the argument is treated as a float and presented as a floating-point number (non-locale aware).
25
	 *     Available since PHP 5.0.3.
26
	 */
27
	public const T_FLOAT_NO_LOCALE = 'F';
28
	/** @var string shorter of %e and %f. */
29
	public const T_FLOAT_AUTO_SCI = 'g';
30
	/** @var string shorter of %E and %F. */
31
	public const T_FLOAT_AUTO_SCI_CAP = 'G';
32
	/** @var string the argument is treated as an integer and presented as an octal number. */
33
	public const T_INT_AS_OCTAL = 'o';
34
	/** @var string the argument is treated as and presented as a string. */
35
	public const T_STRING = 's';
36
	/** @var string the argument is treated as an integer and presented as an unsigned decimal number. */
37
	public const T_INT_UNSIGNED = 'u';
38
	/** @var string the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). */
39
	public const T_INT_HEX = 'x';
40
	/** @var string the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). */
41
	public const T_INT_HEX_CAP = 'X';
42
43
	public const VALID_T_TYPES = [
44
		self::T_INT_AS_BINARY,
45
		self::T_INT_AS_CHARACTER,
46
		self::T_INT,
47
		self::T_DOUBLE_AS_SCI,
48
		self::T_DOUBLE_AS_SCI_CAP,
49
		self::T_FLOAT_LOCALE,
50
		self::T_FLOAT_NO_LOCALE,
51
		self::T_FLOAT_AUTO_SCI,
52
		self::T_FLOAT_AUTO_SCI_CAP,
53
		self::T_INT_AS_OCTAL,
54
		self::T_STRING,
55
		self::T_INT_UNSIGNED,
56
		self::T_INT_HEX,
57
		self::T_INT_HEX_CAP,
58
	];
59
60
	public const ARG_TYPE_MISSING = '';
61
	public const ARG_TYPE_INT     = 'int';
62
	public const ARG_TYPE_DOUBLE  = 'float';
63
	public const ARG_TYPE_STRING  = 'string';
64
65
	/** @var string[] string    s */
66
	public const STRING_TYPES = [ self::T_STRING ];
67
68
	/** @var string[] integer    d, u, c, o, x, X, b */
69
	public const INTEGER_TYPES = [
70
		self::T_INT, self::T_INT_UNSIGNED,
71
		self::T_INT_AS_CHARACTER, self::T_INT_AS_OCTAL,
72
		self::T_INT_HEX, self::T_INT_HEX_CAP,
73
		self::T_INT_AS_BINARY,
74
	];
75
76
	/** @var string[] double    g, G, e, E, f, F */
77
	public const DOUBLE_TYPES = [
78
		self::T_FLOAT_AUTO_SCI, self::T_FLOAT_AUTO_SCI_CAP,
79
		self::T_DOUBLE_AS_SCI, self::T_DOUBLE_AS_SCI_CAP,
80
		self::T_FLOAT_LOCALE, self::T_FLOAT_NO_LOCALE,
81
	];
82
83
	/** @var int|null */
84
	private $arg;
85
	/** @var bool */
86
	private $showPositive;
87
	/** @var string|null */
88
	private $padChar;
89
	/** @var int|null */
90
	private $padWidth;
91
	/** @var bool */
92
	private $leftJustified;
93
	/** @var int|null */
94
	private $precision;
95
96
	/**
97
	 * ArgumentLexeme constructor.
98
	 */
99 14
	public function __construct(
100
		string $lexItemType, string $val, int $pos,
101
		?int $arg, bool $showPositive, ?string $padChar, ?int $padWidth, bool $leftJustified, ?int $precision
102
	) {
103 14
		parent::__construct($lexItemType, $val, $pos);
104
105 14
		$this->arg           = $arg;
106 14
		$this->showPositive  = $showPositive;
107 14
		$this->padChar       = $padChar;
108 14
		$this->padWidth      = $padWidth;
109 14
		$this->leftJustified = $leftJustified;
110 14
		$this->precision     = $precision;
111 14
	}
112
113
	/**
114
	 * The position specifier, such as `%3$s` would return 3 and `%s` would return null
115
	 * @return int|null null on unspecified
116
	 */
117 14
	public function getArg() : ?int {
118 14
		return $this->arg;
119
	}
120
121
	/**
122
	 * Is the "Prefix positive numbers with a plus sign +" flag enabled
123
	 */
124 10
	public function getShowPositive() : bool {
125 10
		return $this->showPositive;
126
	}
127
128
	/**
129
	 * Specified pad character flag
130
	 *
131
	 * @return string|null null on unspecified
132
	 */
133 10
	public function getPadChar() : ?string {
134 10
		return $this->padChar;
135
	}
136
137
	/**
138
	 * Specified pad width
139
	 *
140
	 * @return int|null null on unspecified
141
	 */
142 10
	public function getPadWidth() : ?int {
143 10
		return $this->padWidth;
144
	}
145
146
	/**
147
	 * Is left-justification flag enabled?
148
	 */
149 10
	public function getLeftJustified() : bool {
150 10
		return $this->leftJustified;
151
	}
152
153
	/**
154
	 * The Lexeme's indicated precision.
155
	 *
156
	 * @return int|null null on unspecified
157
	 */
158 10
	public function getPrecision() : ?int {
159 10
		return $this->precision;
160
	}
161
162
	/**
163
	 * Returns based on the type of argument one of the following
164
	 *
165
	 *   ArgumentLexeme::ARG_TYPE_MISSING
166
	 *   ArgumentLexeme::ARG_TYPE_INT
167
	 *   ArgumentLexeme::ARG_TYPE_DOUBLE
168
	 *   ArgumentLexeme::ARG_TYPE_STRING
169
	 */
170 4
	public function argType() : string {
171 4
		if( in_array($this->lexItemType, self::INTEGER_TYPES, true) ) {
172 2
			return self::ARG_TYPE_INT;
173
		}
174
175 4
		if( in_array($this->lexItemType, self::DOUBLE_TYPES, true) ) {
176 1
			return self::ARG_TYPE_DOUBLE;
177
		}
178
179 3
		if( in_array($this->lexItemType, self::STRING_TYPES, true) ) {
180 2
			return self::ARG_TYPE_STRING;
181
		}
182
183 2
		return self::ARG_TYPE_MISSING;
184
	}
185
186
}
187