Skip to content

Commit 526a691

Browse files
lamtranbtranvantri
lamtranb
authored andcommitted
Check compatible ability to use thecodingmachine/safe.
1 parent 8763dcd commit 526a691

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/Helpers.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,35 @@ public static function applyEach(Closure $callback, $valueOrValues)
2323

2424
return $callback($valueOrValues);
2525
}
26+
27+
/**
28+
* Check compatible ability to use thecodingmachine/safe.
29+
*
30+
* @param string $methodName
31+
* @return bool
32+
*/
33+
public static function shouldUseSafe(string $methodName): bool
34+
{
35+
$safeVersion = \Composer\InstalledVersions::getVersion('thecodingmachine/safe');
36+
37+
$skipFunctions = [
38+
'uksort',
39+
];
40+
41+
// Version 2.
42+
if (version_compare($safeVersion, '2', '>='))
43+
{
44+
if (in_array($methodName, $skipFunctions))
45+
{
46+
return false;
47+
}
48+
}
49+
50+
if (!is_callable('\\Safe\\' . $methodName))
51+
{
52+
return false;
53+
}
54+
55+
return true;
56+
}
2657
}

src/Support/AliasArguments/ArrayKeyChange.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
declare(strict_types = 1);
44
namespace Rebing\GraphQL\Support\AliasArguments;
55

6+
use Rebing\GraphQL\Helpers;
7+
68
class ArrayKeyChange
79
{
810
public function modify(array $array, array $pathKeyMappings): array
@@ -21,9 +23,15 @@ public function modify(array $array, array $pathKeyMappings): array
2123
*/
2224
private function orderPaths(array $paths): array
2325
{
24-
\Safe\uksort($paths, function (string $a, string $b): int {
26+
$callback = function (string $a, string $b): int {
2527
return $this->pathLevels($b) <=> $this->pathLevels($a);
26-
});
28+
};
29+
30+
if (Helpers::shouldUseSafe('uksort')) {
31+
\Safe\uksort($paths, $callback);
32+
} else {
33+
uksort($paths, $callback);
34+
}
2735

2836
return $paths;
2937
}

0 commit comments

Comments
 (0)