Skip to content

refactor: update BaseConstraint.php to PHP 7.2 language level #826

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

Merged
merged 1 commit into from
May 7, 2025
Merged
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
31 changes: 10 additions & 21 deletions src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@

declare(strict_types=1);

/*
* This file is part of the JsonSchema package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace JsonSchema\Constraints;

use const JSON_ERROR_NONE;
use JsonSchema\ConstraintError;
use JsonSchema\Entity\JsonPointer;
use JsonSchema\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -50,8 +44,8 @@ public function addError(ConstraintError $constraint, ?JsonPointer $path = null,
$name = $constraint->getValue();
$error = [
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
'message' => ucfirst(vsprintf($message, array_map(function ($val) {
'pointer' => ltrim((string) ($path ?: new JsonPointer('')), '#'),
'message' => ucfirst(vsprintf($message, array_map(static function ($val) {
if (is_scalar($val)) {
return is_bool($val) ? var_export($val, true) : $val;
}
Expand All @@ -78,7 +72,7 @@ public function addErrors(array $errors): void
if ($errors) {
$this->errors = array_merge($this->errors, $errors);
$errorMask = &$this->errorMask;
array_walk($errors, function ($error) use (&$errorMask) {
array_walk($errors, static function ($error) use (&$errorMask) {
if (isset($error['context'])) {
$errorMask |= $error['context'];
}
Expand All @@ -95,10 +89,8 @@ public function getErrors(int $errorContext = Validator::ERROR_ALL): array
return $this->errors;
}

return array_filter($this->errors, function ($error) use ($errorContext) {
if ($errorContext & $error['context']) {
return true;
}
return array_filter($this->errors, static function ($error) use ($errorContext) {
return (bool) ($errorContext & $error['context']);
});
}

Expand All @@ -120,7 +112,7 @@ public function isValid(): bool
}

/**
* Clears any reported errors. Should be used between
* Clears any reported errors. Should be used between
* multiple validation checks.
*/
public function reset(): void
Expand All @@ -145,15 +137,15 @@ public function getErrorMask(): int
public static function arrayToObjectRecursive(array $array): object
{
$json = json_encode($array);
if (json_last_error() !== \JSON_ERROR_NONE) {
if (json_last_error() !== JSON_ERROR_NONE) {
$message = 'Unable to encode schema array as JSON';
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new InvalidArgumentException($message);
}

return (object) json_decode($json);
return (object) json_decode($json, false);
}

/**
Expand All @@ -164,13 +156,10 @@ public static function jsonPatternToPhpRegex(string $pattern): string
return '~' . str_replace('~', '\\~', $pattern) . '~u';
}

/**
* @return string property path
*/
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer): string
{
$result = array_map(
function ($path) {
static function ($path) {
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
},
$pointer->getPropertyPaths()
Expand Down