Skip to content

Commit 846cbf8

Browse files
committed
refactor: update BaseConstraint.php to PHP 7.2 language level
1 parent 13c5e8d commit 846cbf8

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
declare(strict_types=1);
44

5-
/*
6-
* This file is part of the JsonSchema package.
7-
*
8-
* For the full copyright and license information, please view the LICENSE
9-
* file that was distributed with this source code.
10-
*/
11-
125
namespace JsonSchema\Constraints;
136

7+
use const JSON_ERROR_NONE;
148
use JsonSchema\ConstraintError;
159
use JsonSchema\Entity\JsonPointer;
1610
use JsonSchema\Exception\InvalidArgumentException;
@@ -50,8 +44,8 @@ public function addError(ConstraintError $constraint, ?JsonPointer $path = null,
5044
$name = $constraint->getValue();
5145
$error = [
5246
'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')),
53-
'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'),
54-
'message' => ucfirst(vsprintf($message, array_map(function ($val) {
47+
'pointer' => ltrim((string) ($path ?: new JsonPointer('')), '#'),
48+
'message' => ucfirst(vsprintf($message, array_map(static function ($val) {
5549
if (is_scalar($val)) {
5650
return is_bool($val) ? var_export($val, true) : $val;
5751
}
@@ -78,7 +72,7 @@ public function addErrors(array $errors): void
7872
if ($errors) {
7973
$this->errors = array_merge($this->errors, $errors);
8074
$errorMask = &$this->errorMask;
81-
array_walk($errors, function ($error) use (&$errorMask) {
75+
array_walk($errors, static function ($error) use (&$errorMask) {
8276
if (isset($error['context'])) {
8377
$errorMask |= $error['context'];
8478
}
@@ -95,10 +89,8 @@ public function getErrors(int $errorContext = Validator::ERROR_ALL): array
9589
return $this->errors;
9690
}
9791

98-
return array_filter($this->errors, function ($error) use ($errorContext) {
99-
if ($errorContext & $error['context']) {
100-
return true;
101-
}
92+
return array_filter($this->errors, static function ($error) use ($errorContext) {
93+
return (bool) ($errorContext & $error['context']);
10294
});
10395
}
10496

@@ -120,7 +112,7 @@ public function isValid(): bool
120112
}
121113

122114
/**
123-
* Clears any reported errors. Should be used between
115+
* Clears any reported errors. Should be used between
124116
* multiple validation checks.
125117
*/
126118
public function reset(): void
@@ -145,15 +137,15 @@ public function getErrorMask(): int
145137
public static function arrayToObjectRecursive(array $array): object
146138
{
147139
$json = json_encode($array);
148-
if (json_last_error() !== \JSON_ERROR_NONE) {
140+
if (json_last_error() !== JSON_ERROR_NONE) {
149141
$message = 'Unable to encode schema array as JSON';
150142
if (function_exists('json_last_error_msg')) {
151143
$message .= ': ' . json_last_error_msg();
152144
}
153145
throw new InvalidArgumentException($message);
154146
}
155147

156-
return (object) json_decode($json);
148+
return (object) json_decode($json, false);
157149
}
158150

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

167-
/**
168-
* @return string property path
169-
*/
170159
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer): string
171160
{
172161
$result = array_map(
173-
function ($path) {
162+
static function ($path) {
174163
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
175164
},
176165
$pointer->getPropertyPaths()

0 commit comments

Comments
 (0)