@@ -25,10 +25,7 @@ class SimpleStringBuilder
2525 public function __construct ($ string = null )
2626 {
2727 if (!is_null ($ string )) {
28- if (!is_scalar ($ string )) {
29- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
30- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
31- }
28+ $ this ->validateScalar ($ string );
3229 $ this ->string = (string )$ string ;
3330 }
3431 }
@@ -39,10 +36,7 @@ public function __construct($string = null)
3936 */
4037 public function charAt ($ position )
4138 {
42- if (!is_int ($ position )) {
43- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
44- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
45- }
39+ $ this ->validateInteger ($ position );
4640 if ($ position >= $ this ->length ()) {
4741 throw new \InvalidArgumentException ('Position invalid. ' );
4842 }
@@ -55,10 +49,7 @@ public function charAt($position)
5549 */
5650 public function append ($ string )
5751 {
58- if (!is_scalar ($ string )) {
59- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
60- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
61- }
52+ $ this ->validateScalar ($ string );
6253 $ this ->string .= (string )$ string ;
6354 return $ this ;
6455 }
@@ -69,10 +60,7 @@ public function append($string)
6960 */
7061 public function prepend ($ string )
7162 {
72- if (!is_scalar ($ string )) {
73- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
74- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
75- }
63+ $ this ->validateScalar ($ string );
7664 $ this ->string = (string )$ string . $ this ->string ;
7765 return $ this ;
7866 }
@@ -84,14 +72,9 @@ public function prepend($string)
8472 */
8573 public function insert ($ position , $ string )
8674 {
87- if (!is_int ($ position )) {
88- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
89- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
90- }
91- if (!is_scalar ($ string )) {
92- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
93- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
94- }
75+ $ this
76+ ->validateInteger ($ position )
77+ ->validateScalar ($ string );
9578 if ($ position >= $ this ->length ()) {
9679 throw new \InvalidArgumentException ('Position invalid. ' );
9780 }
@@ -107,24 +90,16 @@ public function insert($position, $string)
10790 */
10891 public function replace ($ position , $ length , $ string )
10992 {
110- if (! is_int ( $ position )) {
111- $ type = is_object ($ position ) ? get_class ( $ position ) : gettype ( $ position );
112- throw new \ InvalidArgumentException ( ' Position invalid. Expected integer. Got ' . $ type . ' . ' );
113- }
93+ $ this
94+ -> validateInteger ($ position )
95+ -> validateInteger ( $ length )
96+ -> validateScalar ( $ string );
11497 if ($ position >= $ this ->length ()) {
11598 throw new \InvalidArgumentException ('Position invalid. ' );
11699 }
117- if (!is_int ($ length )) {
118- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
119- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
120- }
121100 if ($ position + $ length > $ this ->length ()) {
122101 throw new \InvalidArgumentException ('Length invalid. ' );
123102 }
124- if (!is_scalar ($ string )) {
125- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
126- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
127- }
128103 $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + $ length );
129104 return $ this ;
130105 }
@@ -136,17 +111,12 @@ public function replace($position, $length, $string)
136111 */
137112 public function setCharAt ($ position , $ string )
138113 {
139- if (!is_int ($ position )) {
140- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
141- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
142- }
114+ $ this
115+ ->validateInteger ($ position )
116+ ->validateScalar ($ string );
143117 if ($ position >= $ this ->length ()) {
144118 throw new \InvalidArgumentException ('Position invalid. ' );
145119 }
146- if (!is_scalar ($ string )) {
147- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
148- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
149- }
150120 if (mb_strlen ((string )$ string ) !== 1 ) {
151121 throw new \InvalidArgumentException ('Expected a scalar value of length 1. ' );
152122 }
@@ -175,14 +145,9 @@ public function reverse()
175145 */
176146 public function delete ($ position , $ length = null )
177147 {
178- if (!is_int ($ position )) {
179- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
180- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
181- }
182- if (!is_int ($ length ) && !is_null ($ length )) {
183- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
184- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
185- }
148+ $ this
149+ ->validateInteger ($ position )
150+ ->validateIntegerOrNull ($ length );
186151 if ($ position >= $ this ->length ()) {
187152 throw new \InvalidArgumentException ('Position invalid. ' );
188153 }
@@ -200,10 +165,7 @@ public function delete($position, $length = null)
200165 */
201166 public function deleteCharAt ($ position )
202167 {
203- if (!is_int ($ position )) {
204- $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
205- throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
206- }
168+ $ this ->validateInteger ($ position );
207169 if ($ position >= $ this ->length ()) {
208170 throw new \InvalidArgumentException ('Position invalid. ' );
209171 }
@@ -217,10 +179,7 @@ public function deleteCharAt($position)
217179 */
218180 public function contains ($ string )
219181 {
220- if (!is_scalar ($ string )) {
221- $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
222- throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
223- }
182+ $ this ->validateScalar ($ string );
224183 return strpos ($ this ->string , (string )$ string ) !== false ;
225184 }
226185
@@ -277,13 +236,12 @@ public function length()
277236 */
278237 public function buildSubstring ($ startPosition , $ length = null )
279238 {
280- if ($ startPosition > $ this ->length ()) {
239+ $ this
240+ ->validateInteger ($ startPosition )
241+ ->validateIntegerOrNull ($ length );
242+ if ($ startPosition >= $ this ->length ()) {
281243 throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid. ' );
282244 }
283- if (!is_int ($ length ) && !is_null ($ length )) {
284- $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
285- throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
286- }
287245 if (is_null ($ length )) {
288246 return mb_substr ($ this ->string , $ startPosition );
289247 } else {
@@ -325,6 +283,18 @@ private function validateInteger($value)
325283 return $ this ;
326284 }
327285
286+ /**
287+ * @param mixed $value
288+ * @return $this
289+ */
290+ private function validateIntegerOrNull ($ value )
291+ {
292+ if (is_null ($ value )) {
293+ return $ this ;
294+ }
295+ return $ this ->validateInteger ($ value );
296+ }
297+
328298 /**
329299 * @param mixed $value
330300 * @return $this
0 commit comments