Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 769a460

Browse files
committed
Ensured JSON paths are strings, clean-up
1 parent 67b4a4c commit 769a460

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/Developer/Json/JsonPath.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ private function __construct() {}
2323
* @throws \InvalidArgumentException
2424
*/
2525
static function normalizePath($path) {
26-
if (empty($path)) {
27-
throw new \InvalidArgumentException('Empty JSON path!');
26+
if (empty($path) || !is_string($path)) {
27+
throw new \InvalidArgumentException('JSON path must be a non-empty string!');
2828
}
2929
if ($path === '$') {
3030
return $path;

src/Developer/Utils/StringUtils.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
class StringUtils {
55

6-
public static function startsWith($haystack, $needle) {
7-
return empty($needle) || strrpos($haystack, $needle, -strlen($haystack)) !== false;
6+
public static function startsWith($string, $prefix) {
7+
return empty($prefix) || strrpos($string, $prefix, -strlen($string)) !== false;
88
}
99

10-
public static function endsWith($haystack, $needle) {
11-
if (empty($needle)) {
10+
public static function endsWith($string, $suffix) {
11+
if (empty($suffix)) {
1212
return true;
1313
}
14-
$diff = strlen($haystack) - strlen($needle);
15-
return $diff >= 0 && strpos($haystack, $needle, $diff) !== false;
14+
$diff = strlen($string) - strlen($suffix);
15+
return $diff >= 0 && strpos($string, $suffix, $diff) !== false;
1616
}
1717
}

tests/Developer/Json/JsonPathTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ public function testNormalizePath_ShouldThrowInvalidArgumentException_WhenJsonPa
3939

4040
// THEN
4141
$this->expectException(\InvalidArgumentException::class);
42-
$this->expectExceptionMessage('Empty JSON path!');
42+
$this->expectExceptionMessage('JSON path must be a non-empty string!');
43+
44+
// WHEN
45+
JsonPath::normalizePath($jsonPath);
46+
}
47+
48+
public function testNormalizePath_ShouldThrowInvalidArgumentException_WhenJsonPathIsNotAString() {
49+
50+
// GIVEN
51+
$jsonPath = [];
52+
53+
// THEN
54+
$this->expectException(\InvalidArgumentException::class);
55+
$this->expectExceptionMessage('JSON path must be a non-empty string!');
4356

4457
// WHEN
4558
JsonPath::normalizePath($jsonPath);
@@ -187,7 +200,7 @@ public function testGetParentPath_ShouldThrowInvalidArgumentException_WhenJsonPa
187200

188201
// THEN
189202
$this->expectException(\InvalidArgumentException::class);
190-
$this->expectExceptionMessage('Empty JSON path!');
203+
$this->expectExceptionMessage('JSON path must be a non-empty string!');
191204

192205
// WHEN
193206
JsonPath::getParentPath($jsonPath);
@@ -200,7 +213,7 @@ public function testGetElementKey_ShouldThrowInvalidArgumentException_WhenJsonPa
200213

201214
// THEN
202215
$this->expectException(\InvalidArgumentException::class);
203-
$this->expectExceptionMessage('Empty JSON path!');
216+
$this->expectExceptionMessage('JSON path must be a non-empty string!');
204217

205218
// WHEN
206219
JsonPath::getElementKey($jsonPath);

0 commit comments

Comments
 (0)