Skip to content

Commit ccd660a

Browse files
committed
More-phpstan-typing
1 parent 088bc5f commit ccd660a

17 files changed

+56
-170
lines changed

bin/validate-json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ if (count($arArgs) == 1) {
6060

6161
/**
6262
* Show the json parse error that happened last
63-
*
64-
* @return void
6563
*/
66-
function showJsonError()
64+
function showJsonError(): void
6765
{
6866
$constants = get_defined_constants(true);
6967
$json_errors = [];

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,6 @@ parameters:
580580
count: 1
581581
path: src/JsonSchema/Constraints/TypeConstraint.php
582582

583-
-
584-
message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:applyDefaultValues\\(\\) has no return type specified\\.$#"
585-
count: 1
586-
path: src/JsonSchema/Constraints/UndefinedConstraint.php
587-
588583
-
589584
message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:check\\(\\) has parameter \\$fromDefault with no type specified\\.$#"
590585
count: 1

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ public function __construct(?Factory $factory = null)
4444
$this->factory = $factory ?: new Factory();
4545
}
4646

47-
/**
48-
* @return void
49-
*/
50-
public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = [])
47+
public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []): void
5148
{
5249
$message = $constraint->getMessage();
5350
$name = $constraint->getValue();
@@ -76,10 +73,7 @@ public function addError(ConstraintError $constraint, ?JsonPointer $path = null,
7673
$this->errorMask |= $error['context'];
7774
}
7875

79-
/**
80-
* @return void
81-
*/
82-
public function addErrors(array $errors)
76+
public function addErrors(array $errors): void
8377
{
8478
if ($errors) {
8579
$this->errors = array_merge($this->errors, $errors);
@@ -93,12 +87,9 @@ public function addErrors(array $errors)
9387
}
9488

9589
/**
96-
* @param int $errorContext
9790
* @phpstan-param int-mask-of<Validator::ERROR_*> $errorContext
98-
*
99-
* @return array
10091
*/
101-
public function getErrors($errorContext = Validator::ERROR_ALL)
92+
public function getErrors(int $errorContext = Validator::ERROR_ALL): array
10293
{
10394
if ($errorContext === Validator::ERROR_ALL) {
10495
return $this->errors;
@@ -112,12 +103,9 @@ public function getErrors($errorContext = Validator::ERROR_ALL)
112103
}
113104

114105
/**
115-
* @param int $errorContext
116106
* @phpstan-param int-mask-of<Validator::ERROR_*> $errorContext
117-
*
118-
* @return int
119107
*/
120-
public function numErrors($errorContext = Validator::ERROR_ALL)
108+
public function numErrors(int $errorContext = Validator::ERROR_ALL): int
121109
{
122110
if ($errorContext === Validator::ERROR_ALL) {
123111
return count($this->errors);
@@ -126,21 +114,16 @@ public function numErrors($errorContext = Validator::ERROR_ALL)
126114
return count($this->getErrors($errorContext));
127115
}
128116

129-
/**
130-
* @return bool
131-
*/
132-
public function isValid()
117+
public function isValid(): bool
133118
{
134119
return !$this->getErrors();
135120
}
136121

137122
/**
138123
* Clears any reported errors. Should be used between
139124
* multiple validation checks.
140-
*
141-
* @return void
142125
*/
143-
public function reset()
126+
public function reset(): void
144127
{
145128
$this->errors = [];
146129
$this->errorMask = Validator::ERROR_NONE;
@@ -149,22 +132,17 @@ public function reset()
149132
/**
150133
* Get the error mask
151134
*
152-
* @return int
153135
* @phpstan-return int-mask-of<Validator::ERROR_*>
154136
*/
155-
public function getErrorMask()
137+
public function getErrorMask(): int
156138
{
157139
return $this->errorMask;
158140
}
159141

160142
/**
161143
* Recursively cast an associative array to an object
162-
*
163-
* @param array $array
164-
*
165-
* @return object
166144
*/
167-
public static function arrayToObjectRecursive($array)
145+
public static function arrayToObjectRecursive(array $array): object
168146
{
169147
$json = json_encode($array);
170148
if (json_last_error() !== \JSON_ERROR_NONE) {
@@ -180,22 +158,16 @@ public static function arrayToObjectRecursive($array)
180158

181159
/**
182160
* Transform a JSON pattern into a PCRE regex
183-
*
184-
* @param string $pattern
185-
*
186-
* @return string
187161
*/
188-
public static function jsonPatternToPhpRegex($pattern)
162+
public static function jsonPatternToPhpRegex(string $pattern): string
189163
{
190164
return '~' . str_replace('~', '\\~', $pattern) . '~u';
191165
}
192166

193167
/**
194-
* @param JsonPointer $pointer
195-
*
196168
* @return string property path
197169
*/
198-
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
170+
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer): string
199171
{
200172
$result = array_map(
201173
function ($path) {

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CollectionConstraint extends Constraint
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null)
28+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
2929
{
3030
// Verify minItems
3131
if (isset($schema->minItems) && count($value) < $schema->minItems) {
@@ -61,12 +61,9 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
6161
*
6262
* @param array $value
6363
* @param \stdClass $schema
64-
* @param JsonPointer|null $path
6564
* @param string $i
66-
*
67-
* @return void
6865
*/
69-
protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null)
66+
protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
7067
{
7168
if (is_object($schema->items)) {
7269
// just one type definition for the whole array

src/JsonSchema/Constraints/ConstConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ConstConstraint extends Constraint
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null)
28+
public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void
2929
{
3030
// Only validate const if the attribute exists
3131
if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) {

src/JsonSchema/Constraints/Constraint.php

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
3939
*
4040
* @param JsonPointer|null $path Current path
4141
* @param mixed $i What to append to the path
42-
*
43-
* @return JsonPointer
4442
*/
45-
protected function incrementPath(?JsonPointer $path, $i)
43+
protected function incrementPath(?JsonPointer $path, $i): JsonPointer
4644
{
47-
$path = $path ?: new JsonPointer('');
45+
$path = $path ?? new JsonPointer('');
4846

4947
if ($i === null || $i === '') {
5048
return $path;
@@ -65,12 +63,9 @@ protected function incrementPath(?JsonPointer $path, $i)
6563
*
6664
* @param mixed $value
6765
* @param mixed $schema
68-
* @param JsonPointer|null $path
6966
* @param mixed $i
70-
*
71-
* @return void
7267
*/
73-
protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null, $i = null)
68+
protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
7469
{
7570
$validator = $this->factory->createInstanceFor('collection');
7671
$validator->check($value, $schema, $path, $i);
@@ -83,15 +78,12 @@ protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null
8378
*
8479
* @param mixed $value
8580
* @param mixed $schema
86-
* @param JsonPointer|null $path
8781
* @param mixed $properties
8882
* @param mixed $additionalProperties
8983
* @param mixed $patternProperties
90-
*
91-
* @return void
9284
*/
9385
protected function checkObject(&$value, $schema = null, ?JsonPointer $path = null, $properties = null,
94-
$additionalProperties = null, $patternProperties = null, $appliedDefaults = [])
86+
$additionalProperties = null, $patternProperties = null, $appliedDefaults = []): void
9587
{
9688
/** @var ObjectConstraint $validator */
9789
$validator = $this->factory->createInstanceFor('object');
@@ -105,12 +97,9 @@ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = nul
10597
*
10698
* @param mixed $value
10799
* @param mixed $schema
108-
* @param JsonPointer|null $path
109100
* @param mixed $i
110-
*
111-
* @return void
112101
*/
113-
protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, $i = null)
102+
protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void
114103
{
115104
$validator = $this->factory->createInstanceFor('type');
116105
$validator->check($value, $schema, $path, $i);
@@ -123,12 +112,9 @@ protected function checkType(&$value, $schema = null, ?JsonPointer $path = null,
123112
*
124113
* @param mixed $value
125114
* @param mixed $schema
126-
* @param JsonPointer|null $path
127115
* @param mixed $i
128-
*
129-
* @return void
130116
*/
131-
protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false)
117+
protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void
132118
{
133119
/** @var UndefinedConstraint $validator */
134120
$validator = $this->factory->createInstanceFor('undefined');
@@ -143,12 +129,9 @@ protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path =
143129
*
144130
* @param mixed $value
145131
* @param mixed $schema
146-
* @param JsonPointer|null $path
147132
* @param mixed $i
148-
*
149-
* @return void
150133
*/
151-
protected function checkString($value, $schema = null, ?JsonPointer $path = null, $i = null)
134+
protected function checkString($value, $schema = null, ?JsonPointer $path = null, $i = null): void
152135
{
153136
$validator = $this->factory->createInstanceFor('string');
154137
$validator->check($value, $schema, $path, $i);
@@ -161,12 +144,9 @@ protected function checkString($value, $schema = null, ?JsonPointer $path = null
161144
*
162145
* @param mixed $value
163146
* @param mixed $schema
164-
* @param JsonPointer|null $path
165147
* @param mixed $i
166-
*
167-
* @return void
168148
*/
169-
protected function checkNumber($value, $schema = null, ?JsonPointer $path = null, $i = null)
149+
protected function checkNumber($value, $schema = null, ?JsonPointer $path = null, $i = null): void
170150
{
171151
$validator = $this->factory->createInstanceFor('number');
172152
$validator->check($value, $schema, $path, $i);
@@ -179,12 +159,9 @@ protected function checkNumber($value, $schema = null, ?JsonPointer $path = null
179159
*
180160
* @param mixed $value
181161
* @param mixed $schema
182-
* @param JsonPointer|null $path
183162
* @param mixed $i
184-
*
185-
* @return void
186163
*/
187-
protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, $i = null)
164+
protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, $i = null): void
188165
{
189166
$validator = $this->factory->createInstanceFor('enum');
190167
$validator->check($value, $schema, $path, $i);
@@ -197,12 +174,9 @@ protected function checkEnum($value, $schema = null, ?JsonPointer $path = null,
197174
*
198175
* @param mixed $value
199176
* @param mixed $schema
200-
* @param JsonPointer|null $path
201177
* @param mixed $i
202-
*
203-
* @return void
204178
*/
205-
protected function checkConst($value, $schema = null, ?JsonPointer $path = null, $i = null)
179+
protected function checkConst($value, $schema = null, ?JsonPointer $path = null, $i = null): void
206180
{
207181
$validator = $this->factory->createInstanceFor('const');
208182
$validator->check($value, $schema, $path, $i);
@@ -215,12 +189,9 @@ protected function checkConst($value, $schema = null, ?JsonPointer $path = null,
215189
*
216190
* @param mixed $value
217191
* @param mixed $schema
218-
* @param JsonPointer|null $path
219192
* @param mixed $i
220-
*
221-
* @return void
222193
*/
223-
protected function checkFormat($value, $schema = null, ?JsonPointer $path = null, $i = null)
194+
protected function checkFormat($value, $schema = null, ?JsonPointer $path = null, $i = null): void
224195
{
225196
$validator = $this->factory->createInstanceFor('format');
226197
$validator->check($value, $schema, $path, $i);
@@ -230,10 +201,8 @@ protected function checkFormat($value, $schema = null, ?JsonPointer $path = null
230201

231202
/**
232203
* Get the type check based on the set check mode.
233-
*
234-
* @return TypeCheck\TypeCheckInterface
235204
*/
236-
protected function getTypeCheck()
205+
protected function getTypeCheck(): TypeCheck\TypeCheckInterface
237206
{
238207
return $this->factory->getTypeCheck();
239208
}

src/JsonSchema/Constraints/ConstraintInterface.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,26 @@ interface ConstraintInterface
2323
{
2424
/**
2525
* returns all collected errors
26-
*
27-
* @return array
2826
*/
29-
public function getErrors();
27+
public function getErrors(): array;
3028

3129
/**
3230
* adds errors to this validator
33-
*
34-
* @param array $errors
35-
*
36-
* @return void
3731
*/
38-
public function addErrors(array $errors);
32+
public function addErrors(array $errors): void;
3933

4034
/**
4135
* adds an error
4236
*
4337
* @param ConstraintError $constraint the constraint/rule that is broken, e.g.: ConstraintErrors::LENGTH_MIN()
44-
* @param JsonPointer|null $path
4538
* @param array $more more array elements to add to the error
46-
*
47-
* @return void
4839
*/
49-
public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []);
40+
public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []): void;
5041

5142
/**
5243
* checks if the validator has not raised errors
53-
*
54-
* @return bool
5544
*/
56-
public function isValid();
45+
public function isValid(): bool;
5746

5847
/**
5948
* invokes the validation of an element
@@ -62,12 +51,9 @@ public function isValid();
6251
*
6352
* @param mixed $value
6453
* @param mixed $schema
65-
* @param JsonPointer|null $path
6654
* @param mixed $i
6755
*
6856
* @throws \JsonSchema\Exception\ExceptionInterface
69-
*
70-
* @return void
7157
*/
72-
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null);
58+
public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void;
7359
}

0 commit comments

Comments
 (0)