Skip to content

Commit 5df627d

Browse files
committed
Switch to PSR12
1 parent 40c8a83 commit 5df627d

35 files changed

Lines changed: 807 additions & 795 deletions

.github/workflows/Test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
php-version: ${{ matrix.php }}
3636
extensions: json
3737
coverage: pcov
38+
#tools: cs2pr
3839
env:
3940
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4041

@@ -44,6 +45,9 @@ jobs:
4445
- name: Install dependencies
4546
run: composer install --prefer-dist --no-interaction --no-suggest
4647

48+
- name: Run phpcs
49+
run: composer cs-check #| cs2pr
50+
4751
- name: Setup PCOV
4852
if: matrix.php == '7.1'
4953
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ vendor
44
.php_cs.cache
55
composer.lock
66
composer.phar
7+
.phpcs-cache

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MIT License
22

33
Original work - Copyright (c) 2018 Flow Communications
4-
Modified work - Copyright (c) 2020 1-2.dev
4+
Modified work - Copyright (c) 2020 Sascha Greuel <hello@1-2.dev>
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
{
32
"name": "softcreatr/jsonpath",
43
"description": "JSONPath implementation for parsing, searching and flattening arrays",
@@ -29,7 +28,9 @@
2928
"phpunit/phpunit": "<7.0 || >= 10.0"
3029
},
3130
"require-dev": {
32-
"phpunit/phpunit": ">=7.0"
31+
"phpunit/phpunit": ">=7.0",
32+
"roave/security-advisories": "dev-master",
33+
"squizlabs/php_codesniffer": "^3.5"
3334
},
3435
"config": {
3536
"optimize-autoloader": true,
@@ -46,6 +47,10 @@
4647
}
4748
},
4849
"minimum-stability": "stable",
50+
"scripts": {
51+
"cs-check": "phpcs",
52+
"cs-fix": "phpcbf"
53+
},
4954
"support": {
5055
"email": "hello@1-2.dev",
5156
"issues": "https://github.com/SoftCreatR/JSONPath/issues",

phpcs.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?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 -->
12+
<arg value="p"/>
13+
14+
<!-- Paths to check -->
15+
<file>src</file>
16+
<file>tests</file>
17+
18+
<!-- Include all rules from the Zend Coding Standard -->
19+
<rule ref="PSR12"/>
20+
</ruleset>

src/AccessHelper.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
2+
23
/**
34
* JSONPath implementation for PHP.
45
*
5-
* @copyright Copyright (c) 2018 Flow Communications
6-
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Flow\JSONPath;
1112

1213
use ArrayAccess;
14+
1315
use function abs;
1416
use function array_key_exists;
1517
use function array_keys;
@@ -74,16 +76,22 @@ public static function keyExists($collection, $key, bool $magicIsAllowed = false
7476
}
7577

7678
/**
79+
* @todo Optimize conditions
7780
* @param array|ArrayAccess $collection
7881
* @param mixed $key
7982
* @param bool $magicIsAllowed
8083
* @return mixed
84+
* @noinspection NotOptimalIfConditionsInspection
8185
*/
8286
public static function getValue($collection, $key, bool $magicIsAllowed = false)
8387
{
8488
$return = null;
8589

86-
if ($magicIsAllowed && is_object($collection) && !$collection instanceof ArrayAccess && method_exists($collection, '__get')) {
90+
if (
91+
$magicIsAllowed &&
92+
is_object($collection) &&
93+
!$collection instanceof ArrayAccess && method_exists($collection, '__get')
94+
) {
8795
$return = $collection->__get($key);
8896
} elseif (is_object($collection) && !$collection instanceof ArrayAccess) {
8997
$return = $collection->$key;
@@ -108,7 +116,7 @@ public static function getValue($collection, $key, bool $magicIsAllowed = false)
108116
*
109117
* @param array|ArrayAccess $collection
110118
* @param mixed $key
111-
* @return mixed
119+
* @return mixed|null
112120
*/
113121
private static function getValueByIndex($collection, $key)
114122
{

src/Filters/AbstractFilter.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
2+
23
/**
34
* JSONPath implementation for PHP.
45
*
5-
* @copyright Copyright (c) 2018 Flow Communications
6-
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Flow\JSONPath\Filters;
1112

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

1516
abstract class AbstractFilter
1617
{
@@ -25,8 +26,6 @@ abstract class AbstractFilter
2526
protected $magicIsAllowed = false;
2627

2728
/**
28-
* AbstractFilter constructor.
29-
*
3029
* @param JSONPathToken $token
3130
* @param int|bool $options
3231
*/
@@ -37,7 +36,7 @@ public function __construct(JSONPathToken $token, $options = false)
3736
}
3837

3938
/**
40-
* @param array|object $collection
39+
* @param array|ArrayAccess $collection
4140
* @return array
4241
*/
4342
abstract public function filter($collection): array;

src/Filters/IndexFilter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
2+
23
/**
34
* JSONPath implementation for PHP.
45
*
5-
* @copyright Copyright (c) 2018 Flow Communications
6-
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Flow\JSONPath\Filters;
1112

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

1515
class IndexFilter extends AbstractFilter
1616
{
@@ -22,7 +22,7 @@ public function filter($collection): array
2222
{
2323
if (AccessHelper::keyExists($collection, $this->token->value, $this->magicIsAllowed)) {
2424
return [
25-
AccessHelper::getValue($collection, $this->token->value, $this->magicIsAllowed)
25+
AccessHelper::getValue($collection, $this->token->value, $this->magicIsAllowed),
2626
];
2727
}
2828

src/Filters/IndexesFilter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
2+
23
/**
34
* JSONPath implementation for PHP.
45
*
5-
* @copyright Copyright (c) 2018 Flow Communications
6-
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Flow\JSONPath\Filters;

src/Filters/QueryMatchFilter.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
<?php
2+
23
/**
34
* JSONPath implementation for PHP.
45
*
5-
* @copyright Copyright (c) 2018 Flow Communications
6-
* @license MIT <https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE>
6+
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
77
*/
8+
89
declare(strict_types=1);
910

1011
namespace Flow\JSONPath\Filters;
1112

1213
use Flow\JSONPath\AccessHelper;
1314
use RuntimeException;
15+
16+
use function explode;
17+
use function in_array;
18+
use function is_array;
1419
use function is_string;
1520
use function preg_match;
1621
use function preg_replace;
22+
use function strpos;
1723
use function strtolower;
24+
use function substr;
1825

1926
class QueryMatchFilter extends AbstractFilter
2027
{
21-
public const MATCH_QUERY_OPERATORS = '
28+
protected const MATCH_QUERY_OPERATORS = '
2229
@(\.(?<key>[^\s<>!=]+)|\[["\']?(?<keySquare>.*?)["\']?\])
2330
(\s*(?<operator>==|=~|=|<>|!==|!=|>=|<=|>|<|in|!in|nin)\s*(?<comparisonValue>.+))?
2431
';
2532

2633
/**
2734
* @inheritDoc
28-
* @return array
2935
*/
3036
public function filter($collection): array
3137
{
@@ -44,7 +50,7 @@ public function filter($collection): array
4450
$operator = $matches['operator'] ?? null;
4551
$comparisonValue = $matches['comparisonValue'] ?? null;
4652

47-
if (is_string(($comparisonValue))) {
53+
if (is_string($comparisonValue)) {
4854
if (strpos($comparisonValue, "[") === 0 && substr($comparisonValue, -1) === "]") {
4955
$comparisonValue = substr($comparisonValue, 1, -1);
5056
$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
@@ -76,11 +82,13 @@ public function filter($collection): array
7682
}
7783

7884
/** @noinspection TypeUnsafeComparisonInspection */
85+
// phpcs:ignore -- This is a loose comparison by design.
7986
if (($operator === '=' || $operator === '==') && $value1 == $comparisonValue) {
8087
$return[] = $value;
8188
}
8289

8390
/** @noinspection TypeUnsafeComparisonInspection */
91+
// phpcs:ignore -- This is a loose comparison by design.
8492
if (($operator === '!=' || $operator === '!==' || $operator === '<>') && $value1 != $comparisonValue) {
8593
$return[] = $value;
8694
}
@@ -109,7 +117,11 @@ public function filter($collection): array
109117
$return[] = $value;
110118
}
111119

112-
if (($operator === 'nin' || $operator === '!in') && is_array($comparisonValue) && !in_array($value1, $comparisonValue, true)) {
120+
if (
121+
($operator === 'nin' || $operator === '!in') &&
122+
is_array($comparisonValue) &&
123+
!in_array($value1, $comparisonValue, true)
124+
) {
113125
$return[] = $value;
114126
}
115127
}

0 commit comments

Comments
 (0)