From f35f40f63f162d3e18a14f500df6ecc2756b95ce Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 20:58:25 +0200 Subject: [PATCH 1/6] refactor: update constraint.php to php 7.2 level --- src/JsonSchema/Constraints/Constraint.php | 22 ++++++++++++------- .../Constraints/ObjectConstraint.php | 12 +++++++--- .../Constraints/UndefinedConstraint.php | 9 ++++---- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 49cec0a3..811ce8ce 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -21,6 +21,7 @@ */ abstract class Constraint extends BaseConstraint implements ConstraintInterface { + /** @var string */ protected $inlineSchemaProperty = '$schema'; public const CHECK_MODE_NONE = 0x00000000; @@ -48,14 +49,12 @@ protected function incrementPath(?JsonPointer $path, $i): JsonPointer return $path; } - $path = $path->withPropertyPaths( + return $path->withPropertyPaths( array_merge( $path->getPropertyPaths(), [$i] ) ); - - return $path; } /** @@ -81,10 +80,17 @@ protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null * @param mixed $properties * @param mixed $additionalProperties * @param mixed $patternProperties + * @param array $appliedDefaults */ - protected function checkObject(&$value, $schema = null, ?JsonPointer $path = null, $properties = null, - $additionalProperties = null, $patternProperties = null, $appliedDefaults = []): void - { + protected function checkObject( + &$value, + $schema = null, + ?JsonPointer $path = null, + $properties = null, + $additionalProperties = null, + $patternProperties = null, + array $appliedDefaults = [] + ): void { /** @var ObjectConstraint $validator */ $validator = $this->factory->createInstanceFor('object'); $validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults); @@ -93,7 +99,7 @@ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = nul } /** - * Validates the type of a property + * Validates the type of the property * * @param mixed $value * @param mixed $schema @@ -114,7 +120,7 @@ protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, * @param mixed $schema * @param mixed $i */ - protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void + protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, bool $fromDefault = false): void { /** @var UndefinedConstraint $validator */ $validator = $this->factory->createInstanceFor('undefined'); diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index e55d7656..c77c1e7f 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -30,9 +30,15 @@ class ObjectConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $properties = null, - $additionalProp = null, $patternProperties = null, $appliedDefaults = []): void - { + public function check( + &$element, + $schema = null, + ?JsonPointer $path = null, + $properties = null, + $additionalProp = null, + $patternProperties = null, + $appliedDefaults = [] + ): void { if ($element instanceof UndefinedConstraint) { return; } diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index b9e00121..3152de29 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -28,7 +28,7 @@ class UndefinedConstraint extends Constraint { /** - * @var array List of properties to which a default value has been applied + * @var list List of properties to which a default value has been applied */ protected $appliedDefaults = []; @@ -72,9 +72,10 @@ public function validateTypes(&$value, $schema, JsonPointer $path, $i = null) } // check object - if (LooseTypeCheck::isObject($value)) { // object processing should always be run on assoc arrays, - // so use LooseTypeCheck here even if CHECK_MODE_TYPE_CAST - // is not set (i.e. don't use $this->getTypeCheck() here). + if (LooseTypeCheck::isObject($value)) { + // object processing should always be run on assoc arrays, + // so use LooseTypeCheck here even if CHECK_MODE_TYPE_CAST + // is not set (i.e. don't use $this->getTypeCheck() here). $this->checkObject( $value, $schema, From 4d1127607caefbf8bca0a61a4ebb5f18fbe2cfba Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 21:00:38 +0200 Subject: [PATCH 2/6] docs: remove redundant license reference and authors --- src/JsonSchema/Constraints/Constraint.php | 13 ------------- src/JsonSchema/Constraints/ObjectConstraint.php | 13 ------------- src/JsonSchema/Constraints/UndefinedConstraint.php | 13 ------------- 3 files changed, 39 deletions(-) diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 811ce8ce..6a139938 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -2,23 +2,10 @@ 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 JsonSchema\Entity\JsonPointer; -/** - * The Base Constraints, all Validators should extend this class - * - * @author Robert Schönthal - * @author Bruno Prieto Reis - */ abstract class Constraint extends BaseConstraint implements ConstraintInterface { /** @var string */ diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index c77c1e7f..a64a7616 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -2,24 +2,11 @@ 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 JsonSchema\ConstraintError; use JsonSchema\Entity\JsonPointer; -/** - * The ObjectConstraint Constraints, validates an object against a given schema - * - * @author Robert Schönthal - * @author Bruno Prieto Reis - */ class ObjectConstraint extends Constraint { /** diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 3152de29..8c5649f6 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -2,13 +2,6 @@ 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 JsonSchema\ConstraintError; @@ -18,12 +11,6 @@ use JsonSchema\Tool\DeepCopy; use JsonSchema\Uri\UriResolver; -/** - * The UndefinedConstraint Constraints - * - * @author Robert Schönthal - * @author Bruno Prieto Reis - */ #[\AllowDynamicProperties] class UndefinedConstraint extends Constraint { From 69ea53056997fdcb7dbd070c1ea43d1107c064f7 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 21:03:52 +0200 Subject: [PATCH 3/6] docs: small correction in function docblock --- src/JsonSchema/Constraints/Constraint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 6a139938..5cce849c 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -86,7 +86,7 @@ protected function checkObject( } /** - * Validates the type of the property + * Validates the type of the value * * @param mixed $value * @param mixed $schema From 3a57d1cad594af6db8a28631a2e300d87fc6da5f Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 21:06:12 +0200 Subject: [PATCH 4/6] style: correct code style violations --- src/JsonSchema/Constraints/Constraint.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 5cce849c..8e818f0a 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -8,7 +8,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface { - /** @var string */ + /** @var string */ protected $inlineSchemaProperty = '$schema'; public const CHECK_MODE_NONE = 0x00000000; @@ -62,11 +62,11 @@ protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null /** * Validates an object * - * @param mixed $value - * @param mixed $schema - * @param mixed $properties - * @param mixed $additionalProperties - * @param mixed $patternProperties + * @param mixed $value + * @param mixed $schema + * @param mixed $properties + * @param mixed $additionalProperties + * @param mixed $patternProperties * @param array $appliedDefaults */ protected function checkObject( From 1b5978cff3d68769ee31dd595821ad1e36c0aad5 Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 21:15:33 +0200 Subject: [PATCH 5/6] refactor: update phpstan baseline and fix similair errors --- phpstan-baseline.neon | 40 ------------------- .../Constraints/ObjectConstraint.php | 4 +- .../Constraints/UndefinedConstraint.php | 4 +- src/JsonSchema/Entity/JsonPointer.php | 2 +- 4 files changed, 6 insertions(+), 44 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 7e4ba4a4..42bea6be 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -40,21 +40,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/CollectionConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkObject\\(\\) has parameter \\$appliedDefaults with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkUndefined\\(\\) has parameter \\$fromDefault with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Property JsonSchema\\\\Constraints\\\\Constraint\\:\\:\\$inlineSchemaProperty has no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addError\\(\\) has parameter \\$more with no value type specified in iterable type array\\.$#" count: 1 @@ -205,11 +190,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/ObjectConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has parameter \\$appliedDefaults with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ObjectConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has parameter \\$patternProperties with no type specified\\.$#" count: 1 @@ -255,11 +235,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/ObjectConstraint.php - - - message: "#^Property JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:\\$appliedDefaults type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ObjectConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\StringConstraint\\:\\:strlen\\(\\) has no return type specified\\.$#" count: 1 @@ -565,11 +540,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/TypeConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:check\\(\\) has parameter \\$fromDefault with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/UndefinedConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:shouldApplyDefaultValue\\(\\) has parameter \\$name with no type specified\\.$#" count: 1 @@ -610,21 +580,11 @@ parameters: count: 1 path: src/JsonSchema/Constraints/UndefinedConstraint.php - - - message: "#^Property JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:\\$appliedDefaults type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/JsonSchema/Constraints/UndefinedConstraint.php - - message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:encodePropertyPaths\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Entity/JsonPointer.php - - - message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:setFromDefault\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Entity/JsonPointer.php - - message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:withPropertyPaths\\(\\) has parameter \\$propertyPaths with no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index a64a7616..c2c614a9 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -10,12 +10,14 @@ class ObjectConstraint extends Constraint { /** - * @var array List of properties to which a default value has been applied + * @var list List of properties to which a default value has been applied */ protected $appliedDefaults = []; /** * {@inheritdoc} + * + * @param list $appliedDefaults */ public function check( &$element, diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 8c5649f6..1ea0a7bc 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -21,8 +21,8 @@ class UndefinedConstraint extends Constraint /** * {@inheritdoc} - */ - public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void + * */ + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, bool $fromDefault = false): void { if (is_null($schema) || !is_object($schema)) { return; diff --git a/src/JsonSchema/Entity/JsonPointer.php b/src/JsonSchema/Entity/JsonPointer.php index ee4a1d35..e674fa6f 100644 --- a/src/JsonSchema/Entity/JsonPointer.php +++ b/src/JsonSchema/Entity/JsonPointer.php @@ -146,7 +146,7 @@ public function __toString() /** * Mark the value at this path as being set from a schema default */ - public function setFromDefault() + public function setFromDefault(): void { $this->fromDefault = true; } From fed8f66f92fdfe133c78de3eda5b6350f05453ab Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Tue, 6 May 2025 21:30:20 +0200 Subject: [PATCH 6/6] docs: add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6e05a9b..90ded89e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Added extra breaking change to UPDATE-6.0.md regarding BaseConstraint::addError signature change ([#823](https://github.com/jsonrainbow/json-schema/pull/823) +- Update constraint class to PHP 7.2 language level ([#824](https://github.com/jsonrainbow/json-schema/pull/824) + ## [6.4.1] - 2025-04-04 ### Fixed