Skip to content

Commit b87ec97

Browse files
author
Martin Brecht-Precht
committed
Modified exception handling.
1 parent da9a5af commit b87ec97

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/StringBuilder.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function prepend($string)
6666
public function insert($position, $string)
6767
{
6868
$this
69-
->validateInteger($position)
69+
->validateUnsignedInteger($position)
7070
->validateScalar($string);
7171
if ($position >= $this->length()) {
7272
throw new \InvalidArgumentException('Position invalid');
@@ -86,8 +86,8 @@ public function insert($position, $string)
8686
public function replace($position, $length, $string)
8787
{
8888
$this
89-
->validateInteger($position)
90-
->validateInteger($length)
89+
->validateUnsignedInteger($position)
90+
->validateUnsignedInteger($length)
9191
->validateScalar($string);
9292
if ($position >= $this->length()) {
9393
throw new \InvalidArgumentException('Position invalid');
@@ -109,7 +109,7 @@ public function replace($position, $length, $string)
109109
public function setCharAt($position, $character)
110110
{
111111
$this
112-
->validateInteger($position)
112+
->validateUnsignedInteger($position)
113113
->validateScalar($character);
114114
if ($position >= $this->length()) {
115115
throw new \InvalidArgumentException('Position invalid');
@@ -147,8 +147,8 @@ public function reverse()
147147
public function delete($position, $length = null)
148148
{
149149
$this
150-
->validateInteger($position)
151-
->validateIntegerOrNull($length);
150+
->validateUnsignedInteger($position)
151+
->validateUnsignedIntegerOrNull($length);
152152
if ($position >= $this->length()) {
153153
throw new \InvalidArgumentException('Position invalid');
154154
}
@@ -168,7 +168,7 @@ public function delete($position, $length = null)
168168
*/
169169
public function deleteCharAt($position)
170170
{
171-
$this->validateInteger($position);
171+
$this->validateUnsignedInteger($position);
172172
if ($position >= $this->length()) {
173173
throw new \InvalidArgumentException('Position invalid');
174174
}
@@ -202,7 +202,7 @@ public function indexOf($string, $offset = 0)
202202
$this
203203
->validateScalar($string)
204204
->validateEmpty($string)
205-
->validateInteger($offset);
205+
->validateUnsignedInteger($offset);
206206
$index = mb_strpos($this->string, (string)$string, $offset);
207207
return $index === false ? null : $index;
208208
}
@@ -221,7 +221,7 @@ public function lastIndexOf($string, $offset = 0)
221221
$this
222222
->validateScalar($string)
223223
->validateEmpty($string)
224-
->validateInteger($offset);
224+
->validateUnsignedInteger($offset);
225225
$index = mb_strrpos($this->string, (string)$string, -1 * $offset);
226226
return $index === false ? null : $index;
227227
}
@@ -254,7 +254,7 @@ public function length()
254254
*/
255255
public function charAt($position)
256256
{
257-
$this->validateInteger($position);
257+
$this->validateUnsignedInteger($position);
258258
if ($position >= $this->length()) {
259259
throw new \InvalidArgumentException('Position invalid');
260260
}
@@ -271,8 +271,8 @@ public function charAt($position)
271271
public function buildSubstring($startPosition, $length = null)
272272
{
273273
$this
274-
->validateInteger($startPosition)
275-
->validateIntegerOrNull($length);
274+
->validateUnsignedInteger($startPosition)
275+
->validateUnsignedIntegerOrNull($length);
276276
if ($startPosition >= $this->length()) {
277277
throw new \InvalidArgumentException('Start position ' . (string)$startPosition . ' invalid');
278278
}
@@ -320,11 +320,14 @@ private function validateScalar($value)
320320
* @param mixed $value
321321
* @return $this
322322
*/
323-
private function validateInteger($value)
323+
private function validateUnsignedInteger($value)
324324
{
325325
if (!is_int($value)) {
326326
$type = is_object($value) ? get_class($value) : gettype($value);
327-
throw new \InvalidArgumentException('Expected integer; got ' . $type);
327+
throw new \InvalidArgumentException('Expected an unsigned integer; got ' . $type);
328+
}
329+
if ($value < 0) {
330+
throw new \InvalidArgumentException('Expected an unsigned integer; got ' . $value);
328331
}
329332
return $this;
330333
}
@@ -333,12 +336,12 @@ private function validateInteger($value)
333336
* @param mixed $value
334337
* @return $this
335338
*/
336-
private function validateIntegerOrNull($value)
339+
private function validateUnsignedIntegerOrNull($value)
337340
{
338341
if (is_null($value)) {
339342
return $this;
340343
}
341-
return $this->validateInteger($value);
344+
return $this->validateUnsignedInteger($value);
342345
}
343346

344347
/**

0 commit comments

Comments
 (0)