Skip to content

Commit 2982d84

Browse files
committed
v0.8.1
1 parent c5350e1 commit 2982d84

23 files changed

+510
-570
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Changelog
22

3+
### 0.8.1
4+
- Removed strict_types
5+
- Applied some PSR-12 related changes
6+
- Small code optimizations
7+
38
### 0.8.0
49
🔻 Breaking changes ahead:
510

611
- Dropped support for PHP < 8.0
712
- Removed deprecated method `JSONPath->data()`
813

9-
1014
### 0.7.5
1115
- Added support for $.length
1216
- Added trim to explode to support both 1,2,3 and 1, 2, 3 inputs

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "softcreatr/jsonpath",
33
"description": "JSONPath implementation for parsing, searching and flattening arrays",
4-
"version": "0.7.5",
4+
"version": "0.8.1",
55
"license": "MIT",
66
"authors": [
77
{
@@ -27,7 +27,7 @@
2727
"require-dev": {
2828
"phpunit/phpunit": "^9.5",
2929
"roave/security-advisories": "dev-latest",
30-
"squizlabs/php_codesniffer": "^3.6"
30+
"squizlabs/php_codesniffer": "^3.7"
3131
},
3232
"config": {
3333
"optimize-autoloader": true,

phpcs.xml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
<?xml version="1.0"?>
2-
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
4-
>
5-
<arg name="basepath" value="."/>
6-
<arg name="cache" value=".phpcs-cache"/>
7-
<arg name="colors"/>
8-
<arg name="extensions" value="php"/>
9-
<arg name="parallel" value="10"/>
10-
11-
<!-- Show progress -->
2+
<ruleset>
3+
<file>files/</file>
4+
<exclude-pattern>lib/system/api/*</exclude-pattern>
5+
<arg name="extensions" value="php" />
126
<arg value="p"/>
7+
<arg name="basepath" value="."/>
138

14-
<!-- Paths to check -->
15-
<file>src</file>
16-
<file>tests</file>
9+
<rule ref="PSR12">
10+
<!-- https://github.com/squizlabs/PHP_CodeSniffer/issues/3200 -->
11+
<exclude name="PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword"/>
1712

18-
<!-- Include all rules from the Zend Coding Standard -->
19-
<rule ref="PSR12"/>
13+
<!-- We have a large number of comments between the closing brace of an `if` and the `else`. -->
14+
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace"/>
15+
</rule>
2016
</ruleset>

src/AccessHelper.php

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,46 @@
66
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
88

9-
declare(strict_types=1);
10-
119
namespace Flow\JSONPath;
1210

1311
use ArrayAccess;
1412

15-
use function abs;
16-
use function array_key_exists;
17-
use function array_keys;
18-
use function array_slice;
19-
use function array_values;
20-
use function get_object_vars;
21-
use function is_array;
22-
use function is_int;
23-
use function is_object;
24-
use function method_exists;
25-
use function property_exists;
26-
2713
class AccessHelper
2814
{
2915
public static function collectionKeys(mixed $collection): array
3016
{
31-
if (is_object($collection)) {
32-
return array_keys(get_object_vars($collection));
17+
if (\is_object($collection)) {
18+
return \array_keys(\get_object_vars($collection));
3319
}
3420

35-
return array_keys($collection);
21+
return \array_keys($collection);
3622
}
3723

3824
public static function isCollectionType(mixed $collection): bool
3925
{
40-
return is_array($collection) || is_object($collection);
26+
return \is_array($collection) || \is_object($collection);
4127
}
4228

4329
public static function keyExists(mixed $collection, $key, bool $magicIsAllowed = false): bool
4430
{
45-
if ($magicIsAllowed && is_object($collection) && method_exists($collection, '__get')) {
31+
if ($magicIsAllowed && \is_object($collection) && \method_exists($collection, '__get')) {
4632
return true;
4733
}
4834

49-
if (is_int($key) && $key < 0) {
50-
$key = abs($key);
35+
if (\is_int($key) && $key < 0) {
36+
$key = \abs($key);
5137
}
5238

53-
if (is_array($collection)) {
54-
return array_key_exists($key, $collection);
39+
if (\is_array($collection)) {
40+
return \array_key_exists($key, $collection);
5541
}
5642

5743
if ($collection instanceof ArrayAccess) {
5844
return $collection->offsetExists($key);
5945
}
6046

61-
if (is_object($collection)) {
62-
return property_exists($collection, (string)$key);
47+
if (\is_object($collection)) {
48+
return \property_exists($collection, (string)$key);
6349
}
6450

6551
return false;
@@ -71,22 +57,22 @@ public static function keyExists(mixed $collection, $key, bool $magicIsAllowed =
7157
public static function getValue(mixed $collection, $key, bool $magicIsAllowed = false)
7258
{
7359
if (
74-
$magicIsAllowed &&
75-
is_object($collection) &&
76-
!$collection instanceof ArrayAccess && method_exists($collection, '__get')
60+
$magicIsAllowed
61+
&& \is_object($collection)
62+
&& !$collection instanceof ArrayAccess && \method_exists($collection, '__get')
7763
) {
7864
$return = $collection->__get($key);
79-
} elseif (is_object($collection) && !$collection instanceof ArrayAccess) {
80-
$return = $collection->$key;
65+
} elseif (\is_object($collection) && !$collection instanceof ArrayAccess) {
66+
$return = $collection->{$key};
8167
} elseif ($collection instanceof ArrayAccess) {
8268
$return = $collection->offsetGet($key);
83-
} elseif (is_array($collection)) {
84-
if (is_int($key) && $key < 0) {
85-
$return = array_slice($collection, $key, 1, false)[0];
69+
} elseif (\is_array($collection)) {
70+
if (\is_int($key) && $key < 0) {
71+
$return = \array_slice($collection, $key, 1)[0];
8672
} else {
8773
$return = $collection[$key];
8874
}
89-
} elseif (is_int($key)) {
75+
} elseif (\is_int($key)) {
9076
$return = self::getValueByIndex($collection, $key);
9177
} else {
9278
$return = $collection[$key];
@@ -108,7 +94,7 @@ private static function getValueByIndex(mixed $collection, $key): mixed
10894
return $val;
10995
}
11096

111-
++$i;
97+
$i++;
11298
}
11399

114100
if ($key < 0) {
@@ -120,7 +106,7 @@ private static function getValueByIndex(mixed $collection, $key): mixed
120106
return $val;
121107
}
122108

123-
++$i;
109+
$i++;
124110
}
125111
}
126112

@@ -129,8 +115,8 @@ private static function getValueByIndex(mixed $collection, $key): mixed
129115

130116
public static function setValue(mixed &$collection, $key, $value)
131117
{
132-
if (is_object($collection) && !$collection instanceof ArrayAccess) {
133-
return $collection->$key = $value;
118+
if (\is_object($collection) && !$collection instanceof ArrayAccess) {
119+
return $collection->{$key} = $value;
134120
}
135121

136122
if ($collection instanceof ArrayAccess) {
@@ -143,30 +129,30 @@ public static function setValue(mixed &$collection, $key, $value)
143129

144130
public static function unsetValue(mixed &$collection, $key): void
145131
{
146-
if (is_object($collection) && !$collection instanceof ArrayAccess) {
147-
unset($collection->$key);
132+
if (\is_object($collection) && !$collection instanceof ArrayAccess) {
133+
unset($collection->{$key});
148134
}
149135

150136
if ($collection instanceof ArrayAccess) {
151137
$collection->offsetUnset($key);
152138
}
153139

154-
if (is_array($collection)) {
140+
if (\is_array($collection)) {
155141
unset($collection[$key]);
156142
}
157143
}
158144

159145
/**
160146
* @throws JSONPathException
161147
*/
162-
public static function arrayValues(array|ArrayAccess $collection): array
148+
public static function arrayValues(array|ArrayAccess $collection): array|ArrayAccess
163149
{
164-
if (is_array($collection)) {
165-
return array_values($collection);
150+
if (\is_array($collection)) {
151+
return \array_values($collection);
166152
}
167153

168-
if (is_object($collection)) {
169-
return array_values((array)$collection);
154+
if (\is_object($collection)) {
155+
return \array_values((array)$collection);
170156
}
171157

172158
throw new JSONPathException("Invalid variable type for arrayValues");

src/Filters/AbstractFilter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
88

9-
declare(strict_types=1);
10-
119
namespace Flow\JSONPath\Filters;
1210

1311
use ArrayAccess;
14-
use Flow\JSONPath\{JSONPath, JSONPathToken};
12+
use Flow\JSONPath\JSONPath;
13+
use Flow\JSONPath\JSONPathToken;
1514

1615
abstract class AbstractFilter
1716
{

src/Filters/IndexFilter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
88

9-
declare(strict_types=1);
10-
119
namespace Flow\JSONPath\Filters;
1210

13-
use Flow\JSONPath\{AccessHelper, JSONPathException};
11+
use Flow\JSONPath\AccessHelper;
12+
use Flow\JSONPath\JSONPathException;
1413

1514
class IndexFilter extends AbstractFilter
1615
{
@@ -19,13 +18,14 @@ class IndexFilter extends AbstractFilter
1918
*/
2019
public function filter($collection): array
2120
{
22-
if (is_array($this->token->value)) {
21+
if (\is_array($this->token->value)) {
2322
$result = [];
2423
foreach ($this->token->value as $value) {
2524
if (AccessHelper::keyExists($collection, $value, $this->magicIsAllowed)) {
2625
$result[] = AccessHelper::getValue($collection, $value, $this->magicIsAllowed);
2726
}
2827
}
28+
2929
return $result;
3030
}
3131

@@ -41,7 +41,7 @@ public function filter($collection): array
4141

4242
if ($this->token->value === 'length') {
4343
return [
44-
count($collection),
44+
\count($collection),
4545
];
4646
}
4747

src/Filters/IndexesFilter.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
88

9-
declare(strict_types=1);
10-
119
namespace Flow\JSONPath\Filters;
1210

1311
use Flow\JSONPath\AccessHelper;

src/Filters/QueryMatchFilter.php

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,11 @@
66
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
88

9-
declare(strict_types=1);
10-
119
namespace Flow\JSONPath\Filters;
1210

1311
use Flow\JSONPath\AccessHelper;
1412
use RuntimeException;
1513

16-
use function explode;
17-
use function in_array;
18-
use function is_array;
19-
use function is_string;
20-
use function preg_match;
21-
use function preg_replace;
22-
use function str_ends_with;
23-
use function str_starts_with;
24-
use function strtolower;
25-
use function substr;
26-
2714
class QueryMatchFilter extends AbstractFilter
2815
{
2916
protected const MATCH_QUERY_OPERATORS = '
@@ -33,7 +20,7 @@ class QueryMatchFilter extends AbstractFilter
3320

3421
public function filter($collection): array
3522
{
36-
preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
23+
\preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
3724

3825
if (!isset($matches[1])) {
3926
throw new RuntimeException('Malformed filter query');
@@ -48,22 +35,22 @@ public function filter($collection): array
4835
$operator = $matches['operator'] ?? null;
4936
$comparisonValue = $matches['comparisonValue'] ?? null;
5037

51-
if (is_string($comparisonValue)) {
52-
if (str_starts_with($comparisonValue, "[") && str_ends_with($comparisonValue, "]")) {
53-
$comparisonValue = substr($comparisonValue, 1, -1);
54-
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
55-
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
56-
$comparisonValue = preg_replace('/[\'"],[ ]*[\'"]/', ',', $comparisonValue);
57-
$comparisonValue = array_map('trim', explode(",", $comparisonValue));
38+
if (\is_string($comparisonValue)) {
39+
if (\str_starts_with($comparisonValue, "[") && \str_ends_with($comparisonValue, "]")) {
40+
$comparisonValue = \substr($comparisonValue, 1, -1);
41+
$comparisonValue = \preg_replace('/^[\'"]/', '', $comparisonValue);
42+
$comparisonValue = \preg_replace('/[\'"]$/', '', $comparisonValue);
43+
$comparisonValue = \preg_replace('/[\'"], *[\'"]/', ',', $comparisonValue);
44+
$comparisonValue = \array_map('trim', \explode(",", $comparisonValue));
5845
} else {
59-
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
60-
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
46+
$comparisonValue = \preg_replace('/^[\'"]/', '', $comparisonValue);
47+
$comparisonValue = \preg_replace('/[\'"]$/', '', $comparisonValue);
6148

62-
if (strtolower($comparisonValue) === 'false') {
49+
if (\strtolower($comparisonValue) === 'false') {
6350
$comparisonValue = false;
64-
} elseif (strtolower($comparisonValue) === 'true') {
51+
} elseif (\strtolower($comparisonValue) === 'true') {
6552
$comparisonValue = true;
66-
} elseif (strtolower($comparisonValue) === 'null') {
53+
} elseif (\strtolower($comparisonValue) === 'null') {
6754
$comparisonValue = null;
6855
}
6956
}
@@ -91,7 +78,7 @@ public function filter($collection): array
9178
$return[] = $value;
9279
}
9380

94-
if ($operator === '=~' && @preg_match($comparisonValue, $value1)) {
81+
if ($operator === '=~' && @\preg_match($comparisonValue, $value1)) {
9582
$return[] = $value;
9683
}
9784

@@ -111,14 +98,14 @@ public function filter($collection): array
11198
$return[] = $value;
11299
}
113100

114-
if ($operator === 'in' && is_array($comparisonValue) && in_array($value1, $comparisonValue, false)) {
101+
if ($operator === 'in' && \is_array($comparisonValue) && \in_array($value1, $comparisonValue, false)) {
115102
$return[] = $value;
116103
}
117104

118105
if (
119-
($operator === 'nin' || $operator === '!in') &&
120-
is_array($comparisonValue) &&
121-
!in_array($value1, $comparisonValue, false)
106+
($operator === 'nin' || $operator === '!in')
107+
&& \is_array($comparisonValue)
108+
&& !\in_array($value1, $comparisonValue, false)
122109
) {
123110
$return[] = $value;
124111
}

0 commit comments

Comments
 (0)