Skip to content

Commit 2e3eedf

Browse files
author
Martin Brecht-Precht
committed
Fixed some issues.
1 parent 306d85a commit 2e3eedf

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

src/SimpleStringBuilder.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function replace($position, $length, $string)
118118
$type = is_object($length) ? get_class($length) : gettype($length);
119119
throw new \InvalidArgumentException('Length invalid. Expected integer. Got ' . $type . '.');
120120
}
121-
if ($position + $length >= $this->length()) {
121+
if ($position + $length > $this->length()) {
122122
throw new \InvalidArgumentException('Length invalid.');
123123
}
124124
if (!is_scalar($string)) {
@@ -231,17 +231,10 @@ public function contains($string)
231231
*/
232232
public function indexOf($string, $offset = 0)
233233
{
234-
if (!is_scalar($string)) {
235-
$type = is_object($string) ? get_class($string) : gettype($string);
236-
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
237-
}
238-
if (mb_strlen((string)$string) === 0) {
239-
throw new \InvalidArgumentException('Empty string is invalid.');
240-
}
241-
if (!is_int($offset)) {
242-
$type = is_object($offset) ? get_class($offset) : gettype($offset);
243-
throw new \InvalidArgumentException('Offset invalid. Expected integer. Got ' . $type . '.');
244-
}
234+
$this
235+
->validateScalar($string)
236+
->validateEmpty($string)
237+
->validateInteger($offset);
245238
$index = mb_strpos($this->string, (string)$string, $offset);
246239
return $index === false ? null : $index;
247240
}
@@ -253,17 +246,10 @@ public function indexOf($string, $offset = 0)
253246
*/
254247
public function lastIndexOf($string, $offset = 0)
255248
{
256-
if (!is_scalar($string)) {
257-
$type = is_object($string) ? get_class($string) : gettype($string);
258-
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
259-
}
260-
if (mb_strlen((string)$string) === 0) {
261-
throw new \InvalidArgumentException('Empty string is invalid.');
262-
}
263-
if (!is_int($offset)) {
264-
$type = is_object($offset) ? get_class($offset) : gettype($offset);
265-
throw new \InvalidArgumentException('Offset invalid. Expected integer. Got ' . $type . '.');
266-
}
249+
$this
250+
->validateScalar($string)
251+
->validateEmpty($string)
252+
->validateInteger($offset);
267253
$index = mb_strrpos($this->string, (string)$string, $offset);
268254
return $index === false ? null : $index;
269255
}
@@ -313,4 +299,43 @@ public function build()
313299
return $this->string;
314300
}
315301

302+
/**
303+
* @param mixed $value
304+
* @return $this
305+
*/
306+
private function validateScalar($value)
307+
{
308+
if (!is_scalar($value)) {
309+
$type = is_object($value) ? get_class($value) : gettype($value);
310+
throw new \InvalidArgumentException('Expected a scalar value. Got ' . $type . '.');
311+
}
312+
return $this;
313+
}
314+
315+
/**
316+
* @param mixed $value
317+
* @return $this
318+
*/
319+
private function validateInteger($value)
320+
{
321+
if (!is_int($value)) {
322+
$type = is_object($value) ? get_class($value) : gettype($value);
323+
throw new \InvalidArgumentException('Expected integer. Got ' . $type . '.');
324+
}
325+
return $this;
326+
}
327+
328+
/**
329+
* @param mixed $value
330+
* @return $this
331+
*/
332+
private function validateEmpty($value)
333+
{
334+
$value = (string)$value;
335+
if (empty($value)) {
336+
throw new \InvalidArgumentException('Empty string is invalid.');
337+
}
338+
return $this;
339+
}
340+
316341
}

test/SimpleStringBuilderTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function testBuilder()
5151
$this->assertEquals('02356', $builder->build());
5252
}
5353

54+
public function testBuilderConstructFail()
55+
{
56+
$this->setExpectedException(get_class(new \InvalidArgumentException()));
57+
new SimpleStringBuilder(array());
58+
}
59+
5460
public function testBuilderAppendFail()
5561
{
5662
$this->setExpectedException(get_class(new \InvalidArgumentException()));
@@ -156,7 +162,9 @@ public function testBuilderSetCharAtFail4()
156162
{
157163
$this->setExpectedException(get_class(new \InvalidArgumentException()));
158164
$builder = new SimpleStringBuilder();
159-
$builder->setCharAt(0, 'ab');
165+
$builder
166+
->append('a')
167+
->setCharAt(0, 'ab');
160168
}
161169

162170
public function testBuilderDeleteFail1()

0 commit comments

Comments
 (0)