Skip to content

Commit 13c5e8d

Browse files
Update constraint class to PHP 7.2 language level (#824)
This brings the `Constraint` class to PHP 7.2 language level and does some cleanup along the way.
1 parent 708a160 commit 13c5e8d

File tree

6 files changed

+41
-103
lines changed

6 files changed

+41
-103
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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)
14+
- Update constraint class to PHP 7.2 language level ([#824](https://github.com/jsonrainbow/json-schema/pull/824)
15+
1416

1517
## [6.4.1] - 2025-04-04
1618
### Fixed

phpstan-baseline.neon

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ parameters:
4040
count: 1
4141
path: src/JsonSchema/Constraints/CollectionConstraint.php
4242

43-
-
44-
message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkObject\\(\\) has parameter \\$appliedDefaults with no type specified\\.$#"
45-
count: 1
46-
path: src/JsonSchema/Constraints/Constraint.php
47-
48-
-
49-
message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkUndefined\\(\\) has parameter \\$fromDefault with no type specified\\.$#"
50-
count: 1
51-
path: src/JsonSchema/Constraints/Constraint.php
52-
53-
-
54-
message: "#^Property JsonSchema\\\\Constraints\\\\Constraint\\:\\:\\$inlineSchemaProperty has no type specified\\.$#"
55-
count: 1
56-
path: src/JsonSchema/Constraints/Constraint.php
57-
5843
-
5944
message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addError\\(\\) has parameter \\$more with no value type specified in iterable type array\\.$#"
6045
count: 1
@@ -205,11 +190,6 @@ parameters:
205190
count: 1
206191
path: src/JsonSchema/Constraints/ObjectConstraint.php
207192

208-
-
209-
message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has parameter \\$appliedDefaults with no type specified\\.$#"
210-
count: 1
211-
path: src/JsonSchema/Constraints/ObjectConstraint.php
212-
213193
-
214194
message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has parameter \\$patternProperties with no type specified\\.$#"
215195
count: 1
@@ -255,11 +235,6 @@ parameters:
255235
count: 1
256236
path: src/JsonSchema/Constraints/ObjectConstraint.php
257237

258-
-
259-
message: "#^Property JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:\\$appliedDefaults type has no value type specified in iterable type array\\.$#"
260-
count: 1
261-
path: src/JsonSchema/Constraints/ObjectConstraint.php
262-
263238
-
264239
message: "#^Method JsonSchema\\\\Constraints\\\\StringConstraint\\:\\:strlen\\(\\) has no return type specified\\.$#"
265240
count: 1
@@ -565,11 +540,6 @@ parameters:
565540
count: 1
566541
path: src/JsonSchema/Constraints/TypeConstraint.php
567542

568-
-
569-
message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:check\\(\\) has parameter \\$fromDefault with no type specified\\.$#"
570-
count: 1
571-
path: src/JsonSchema/Constraints/UndefinedConstraint.php
572-
573543
-
574544
message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:shouldApplyDefaultValue\\(\\) has parameter \\$name with no type specified\\.$#"
575545
count: 1
@@ -610,21 +580,11 @@ parameters:
610580
count: 1
611581
path: src/JsonSchema/Constraints/UndefinedConstraint.php
612582

613-
-
614-
message: "#^Property JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:\\$appliedDefaults type has no value type specified in iterable type array\\.$#"
615-
count: 1
616-
path: src/JsonSchema/Constraints/UndefinedConstraint.php
617-
618583
-
619584
message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:encodePropertyPaths\\(\\) return type has no value type specified in iterable type array\\.$#"
620585
count: 1
621586
path: src/JsonSchema/Entity/JsonPointer.php
622587

623-
-
624-
message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:setFromDefault\\(\\) has no return type specified\\.$#"
625-
count: 1
626-
path: src/JsonSchema/Entity/JsonPointer.php
627-
628588
-
629589
message: "#^Method JsonSchema\\\\Entity\\\\JsonPointer\\:\\:withPropertyPaths\\(\\) has parameter \\$propertyPaths with no value type specified in iterable type array\\.$#"
630590
count: 1

src/JsonSchema/Constraints/Constraint.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@
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

147
use JsonSchema\Entity\JsonPointer;
158

16-
/**
17-
* The Base Constraints, all Validators should extend this class
18-
*
19-
* @author Robert Schönthal <[email protected]>
20-
* @author Bruno Prieto Reis <[email protected]>
21-
*/
229
abstract class Constraint extends BaseConstraint implements ConstraintInterface
2310
{
11+
/** @var string */
2412
protected $inlineSchemaProperty = '$schema';
2513

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

51-
$path = $path->withPropertyPaths(
39+
return $path->withPropertyPaths(
5240
array_merge(
5341
$path->getPropertyPaths(),
5442
[$i]
5543
)
5644
);
57-
58-
return $path;
5945
}
6046

6147
/**
@@ -76,15 +62,22 @@ protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null
7662
/**
7763
* Validates an object
7864
*
79-
* @param mixed $value
80-
* @param mixed $schema
81-
* @param mixed $properties
82-
* @param mixed $additionalProperties
83-
* @param mixed $patternProperties
65+
* @param mixed $value
66+
* @param mixed $schema
67+
* @param mixed $properties
68+
* @param mixed $additionalProperties
69+
* @param mixed $patternProperties
70+
* @param array<string> $appliedDefaults
8471
*/
85-
protected function checkObject(&$value, $schema = null, ?JsonPointer $path = null, $properties = null,
86-
$additionalProperties = null, $patternProperties = null, $appliedDefaults = []): void
87-
{
72+
protected function checkObject(
73+
&$value,
74+
$schema = null,
75+
?JsonPointer $path = null,
76+
$properties = null,
77+
$additionalProperties = null,
78+
$patternProperties = null,
79+
array $appliedDefaults = []
80+
): void {
8881
/** @var ObjectConstraint $validator */
8982
$validator = $this->factory->createInstanceFor('object');
9083
$validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
@@ -93,7 +86,7 @@ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = nul
9386
}
9487

9588
/**
96-
* Validates the type of a property
89+
* Validates the type of the value
9790
*
9891
* @param mixed $value
9992
* @param mixed $schema
@@ -114,7 +107,7 @@ protected function checkType(&$value, $schema = null, ?JsonPointer $path = null,
114107
* @param mixed $schema
115108
* @param mixed $i
116109
*/
117-
protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void
110+
protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, bool $fromDefault = false): void
118111
{
119112
/** @var UndefinedConstraint $validator */
120113
$validator = $this->factory->createInstanceFor('undefined');

src/JsonSchema/Constraints/ObjectConstraint.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@
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

147
use JsonSchema\ConstraintError;
158
use JsonSchema\Entity\JsonPointer;
169

17-
/**
18-
* The ObjectConstraint Constraints, validates an object against a given schema
19-
*
20-
* @author Robert Schönthal <[email protected]>
21-
* @author Bruno Prieto Reis <[email protected]>
22-
*/
2310
class ObjectConstraint extends Constraint
2411
{
2512
/**
26-
* @var array List of properties to which a default value has been applied
13+
* @var list<string> List of properties to which a default value has been applied
2714
*/
2815
protected $appliedDefaults = [];
2916

3017
/**
3118
* {@inheritdoc}
19+
*
20+
* @param list<string> $appliedDefaults
3221
*/
33-
public function check(&$element, $schema = null, ?JsonPointer $path = null, $properties = null,
34-
$additionalProp = null, $patternProperties = null, $appliedDefaults = []): void
35-
{
22+
public function check(
23+
&$element,
24+
$schema = null,
25+
?JsonPointer $path = null,
26+
$properties = null,
27+
$additionalProp = null,
28+
$patternProperties = null,
29+
$appliedDefaults = []
30+
): void {
3631
if ($element instanceof UndefinedConstraint) {
3732
return;
3833
}

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
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

147
use JsonSchema\ConstraintError;
@@ -18,24 +11,18 @@
1811
use JsonSchema\Tool\DeepCopy;
1912
use JsonSchema\Uri\UriResolver;
2013

21-
/**
22-
* The UndefinedConstraint Constraints
23-
*
24-
* @author Robert Schönthal <[email protected]>
25-
* @author Bruno Prieto Reis <[email protected]>
26-
*/
2714
#[\AllowDynamicProperties]
2815
class UndefinedConstraint extends Constraint
2916
{
3017
/**
31-
* @var array List of properties to which a default value has been applied
18+
* @var list<string> List of properties to which a default value has been applied
3219
*/
3320
protected $appliedDefaults = [];
3421

3522
/**
3623
* {@inheritdoc}
37-
*/
38-
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void
24+
* */
25+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, bool $fromDefault = false): void
3926
{
4027
if (is_null($schema) || !is_object($schema)) {
4128
return;
@@ -72,9 +59,10 @@ public function validateTypes(&$value, $schema, JsonPointer $path, $i = null)
7259
}
7360

7461
// check object
75-
if (LooseTypeCheck::isObject($value)) { // object processing should always be run on assoc arrays,
76-
// so use LooseTypeCheck here even if CHECK_MODE_TYPE_CAST
77-
// is not set (i.e. don't use $this->getTypeCheck() here).
62+
if (LooseTypeCheck::isObject($value)) {
63+
// object processing should always be run on assoc arrays,
64+
// so use LooseTypeCheck here even if CHECK_MODE_TYPE_CAST
65+
// is not set (i.e. don't use $this->getTypeCheck() here).
7866
$this->checkObject(
7967
$value,
8068
$schema,

src/JsonSchema/Entity/JsonPointer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function __toString()
146146
/**
147147
* Mark the value at this path as being set from a schema default
148148
*/
149-
public function setFromDefault()
149+
public function setFromDefault(): void
150150
{
151151
$this->fromDefault = true;
152152
}

0 commit comments

Comments
 (0)