Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for thecodingmachine/safe 2.4 #948 #961

Merged
merged 3 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"illuminate/contracts": "^6.0|^8.0|^9.0",
"illuminate/support": "^6.0|^8.0|^9.0",
"laragraph/utils": "^1",
"thecodingmachine/safe": "^1.3",
"thecodingmachine/safe": "^1.1|^2.4",
"webonyx/graphql-php": "^14.6.4"
},
"require-dev": {
Expand Down
33 changes: 33 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Rebing\GraphQL;

use Closure;
use OutOfBoundsException;

class Helpers
{
Expand All @@ -23,4 +24,36 @@ public static function applyEach(Closure $callback, $valueOrValues)

return $callback($valueOrValues);
}

/**
* Check compatible ability to use thecodingmachine/safe.
*
* @return string|false
*/
public static function shouldUseSafe(string $methodName)
{
$packageName = 'thecodingmachine/safe';
$safeVersion = \Composer\InstalledVersions::getVersion($packageName);

if (!$safeVersion) {
throw new OutOfBoundsException("Package {$packageName} is being replaced or provided but is not really installed");
}

$skipFunctions = [
'uksort',
];

// Version 2.
if (version_compare($safeVersion, '2', '>=')) {
if (\in_array(str_replace('\\Safe\\', '', $methodName), $skipFunctions)) {
return false;
}
}

if (!\is_callable($methodName)) {
return false;
}

return $methodName;
}
}
14 changes: 12 additions & 2 deletions src/Support/AliasArguments/ArrayKeyChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
declare(strict_types = 1);
namespace Rebing\GraphQL\Support\AliasArguments;

use Rebing\GraphQL\Helpers;

class ArrayKeyChange
{
public function modify(array $array, array $pathKeyMappings): array
Expand All @@ -21,9 +23,17 @@ public function modify(array $array, array $pathKeyMappings): array
*/
private function orderPaths(array $paths): array
{
\Safe\uksort($paths, function (string $a, string $b): int {
$callback = function (string $a, string $b): int {
return $this->pathLevels($b) <=> $this->pathLevels($a);
});
};

$functionName = Helpers::shouldUseSafe('\\Safe\\uksort');

if (\is_callable($functionName)) {
$functionName($paths, $callback);
} else {
uksort($paths, $callback);
}

return $paths;
}
Expand Down