Skip to content

Update constraint class to PHP 7.2 language level #824

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 6 commits into from
May 6, 2025
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 0 additions & 40 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 19 additions & 26 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,13 @@

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 <seroscho@googlemail.com>
* @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
*/
abstract class Constraint extends BaseConstraint implements ConstraintInterface
{
/** @var string */
protected $inlineSchemaProperty = '$schema';

public const CHECK_MODE_NONE = 0x00000000;
Expand Down Expand Up @@ -48,14 +36,12 @@ protected function incrementPath(?JsonPointer $path, $i): JsonPointer
return $path;
}

$path = $path->withPropertyPaths(
return $path->withPropertyPaths(
array_merge(
$path->getPropertyPaths(),
[$i]
)
);

return $path;
}

/**
Expand All @@ -76,15 +62,22 @@ 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<string> $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);
Expand All @@ -93,7 +86,7 @@ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = nul
}

/**
* Validates the type of a property
* Validates the type of the value
*
* @param mixed $value
* @param mixed $schema
Expand All @@ -114,7 +107,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');
Expand Down
29 changes: 12 additions & 17 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@

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 <seroscho@googlemail.com>
* @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
*/
class ObjectConstraint extends Constraint
{
/**
* @var array List of properties to which a default value has been applied
* @var list<string> List of properties to which a default value has been applied
*/
protected $appliedDefaults = [];

/**
* {@inheritdoc}
*
* @param list<string> $appliedDefaults
*/
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;
}
Expand Down
26 changes: 7 additions & 19 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,24 +11,18 @@
use JsonSchema\Tool\DeepCopy;
use JsonSchema\Uri\UriResolver;

/**
* The UndefinedConstraint Constraints
*
* @author Robert Schönthal <seroscho@googlemail.com>
* @author Bruno Prieto Reis <bruno.p.reis@gmail.com>
*/
#[\AllowDynamicProperties]
class UndefinedConstraint extends Constraint
{
/**
* @var array List of properties to which a default value has been applied
* @var list<string> List of properties to which a default value has been applied
*/
protected $appliedDefaults = [];

/**
* {@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;
Expand Down Expand Up @@ -72,9 +59,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,
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Entity/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down