Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b209eb8

Browse files
refactor: update BaseConstraint.php to PHP 7.2 language level (#826)
This pull request refactors the `BaseConstraint` class in the `JsonSchema` package to improve code readability, enforce immutability, and align with modern PHP practices. The changes include replacing anonymous functions with static closures, updating type casting, and removing outdated comments. ### Code modernization and immutability: * Replaced anonymous functions with `static` closures in methods like `addError`, `addErrors`, and `convertJsonPointerIntoPropertyPath` to enforce immutability and improve performance. [[1]](diffhunk://#diff-05cd63f6b003f185a215e10402525e33d94466ec4d83a685c5a6ccd1befcf921L53-R48) [[2]](diffhunk://#diff-05cd63f6b003f185a215e10402525e33d94466ec4d83a685c5a6ccd1befcf921L81-R75) [[3]](diffhunk://#diff-05cd63f6b003f185a215e10402525e33d94466ec4d83a685c5a6ccd1befcf921L167-R162) * Updated type casting for better readability and adherence to modern PHP conventions, e.g., `(string)` instead of `strval`. ### Code readability and cleanup: * Removed outdated docblock comments and unnecessary annotations, such as the `@return` tag in `convertJsonPointerIntoPropertyPath`. * Replaced `\JSON_ERROR_NONE` with the `JSON_ERROR_NONE` constant for consistency with the `use` statement. ### Other improvements: * Adjusted the `json_decode` call to explicitly set the `associative` parameter to `false` for clarity.
1 parent 95e5d61 commit b209eb8

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313
- Added extra breaking change to UPDATE-6.0.md regarding BaseConstraint::addError signature change ([#823](https://github.com/jsonrainbow/json-schema/pull/823))
1414
- Update constraint class to PHP 7.2 language level ([#824](https://github.com/jsonrainbow/json-schema/pull/824))
15+
- Update base constraint class to PHP 7.2 language level ([#826](https://github.com/jsonrainbow/json-schema/pull/826))
1516

1617
### Added
1718
- Introduce 32 bits CI workflow on latest php version ([#825](https://github.com/jsonrainbow/json-schema/pull/825))

‎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(staticfunction ($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, staticfunction ($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+
staticfunction ($path) {
174163
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
175164
},
176165
$pointer->getPropertyPaths()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /