From 05578062e0031cc66d860e7cb0be4bf86444c4f9 Mon Sep 17 00:00:00 2001 From: Jack Worman Date: Thu, 6 Feb 2025 09:11:44 -0500 Subject: [PATCH] More-phpstan-typing --- .gitignore | 1 + CHANGELOG.md | 1 + bin/validate-json | 4 +- composer.json | 3 +- phpstan-baseline.neon | 195 +----------------- phpstan.neon | 1 + src/JsonSchema/Constraints/BaseConstraint.php | 44 ++-- .../Constraints/CollectionConstraint.php | 11 +- .../Constraints/ConstConstraint.php | 2 +- src/JsonSchema/Constraints/Constraint.php | 95 ++++----- .../Constraints/ConstraintInterface.php | 28 +-- src/JsonSchema/Constraints/EnumConstraint.php | 2 +- src/JsonSchema/Constraints/Factory.php | 55 +++-- .../Constraints/FormatConstraint.php | 2 +- .../Constraints/NumberConstraint.php | 2 +- .../Constraints/ObjectConstraint.php | 2 +- .../Constraints/SchemaConstraint.php | 2 +- .../Constraints/StringConstraint.php | 2 +- src/JsonSchema/Constraints/TypeConstraint.php | 2 +- .../Constraints/UndefinedConstraint.php | 6 +- tests/Constraints/FactoryTest.php | 2 +- 21 files changed, 120 insertions(+), 342 deletions(-) diff --git a/.gitignore b/.gitignore index e5ad919d..59e2f7db 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ coverage composer.lock docs-api phpunit.xml +.phpunit.result.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index e91d27af..2110a85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changed - Used PHPStan's int-mask-of type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779)) +- Fixed some PHPStan errors ([#781](https://github.com/jsonrainbow/json-schema/pull/781)) ## [6.1.0] - 2025-02-04 ### Added diff --git a/bin/validate-json b/bin/validate-json index 62c3ab0a..83f0e2a8 100755 --- a/bin/validate-json +++ b/bin/validate-json @@ -60,10 +60,8 @@ if (count($arArgs) == 1) { /** * Show the json parse error that happened last - * - * @return void */ -function showJsonError() +function showJsonError(): void { $constants = get_defined_constants(true); $json_errors = []; diff --git a/composer.json b/composer.json index df307fbc..bbeca2f7 100644 --- a/composer.json +++ b/composer.json @@ -78,6 +78,7 @@ "style-fix": "php-cs-fixer fix --verbose", "test": "phpunit", "testOnly": "phpunit --colors --filter", - "phpstan": "@php phpstan" + "phpstan": "@php phpstan", + "phpstan-generate-baseline": "@php phpstan --generate-baseline" } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c6277a6d..d4116654 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,20 +1,10 @@ parameters: ignoreErrors: - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:addError\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:addError\\(\\) has parameter \\$more with no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Constraints/BaseConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:addErrors\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:addErrors\\(\\) has parameter \\$errors with no value type specified in iterable type array\\.$#" count: 1 @@ -26,32 +16,7 @@ parameters: path: src/JsonSchema/Constraints/BaseConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:getErrors\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:getErrors\\(\\) has parameter \\$errorContext with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:isValid\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:numErrors\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:numErrors\\(\\) has parameter \\$errorContext with no type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/BaseConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:reset\\(\\) has no return type specified\\.$#" + message: "#^Method JsonSchema\\\\Constraints\\\\BaseConstraint\\:\\:getErrors\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Constraints/BaseConstraint.php @@ -65,11 +30,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/BaseConstraint.php - - - message: "#^Ternary operator condition is always true\\.$#" - count: 2 - path: src/JsonSchema/Constraints/BaseConstraint.php - - message: "#^Cannot access property \\$additionalItems on stdClass\\|null\\.$#" count: 3 @@ -80,16 +40,6 @@ parameters: count: 6 path: src/JsonSchema/Constraints/CollectionConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\CollectionConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\CollectionConstraint\\:\\:validateItems\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/CollectionConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\CollectionConstraint\\:\\:validateItems\\(\\) has parameter \\$value with no value type specified in iterable type array\\.$#" count: 1 @@ -105,61 +55,11 @@ parameters: count: 1 path: src/JsonSchema/Constraints/CollectionConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ConstConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkArray\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkConst\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkEnum\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkFormat\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkNumber\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkObject\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.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\\:\\:checkString\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkType\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Constraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Constraint\\:\\:checkUndefined\\(\\) has no return 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 @@ -170,31 +70,16 @@ parameters: count: 1 path: src/JsonSchema/Constraints/Constraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addError\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ConstraintInterface.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addError\\(\\) has parameter \\$more with no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Constraints/ConstraintInterface.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addErrors\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ConstraintInterface.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:addErrors\\(\\) has parameter \\$errors with no value type specified in iterable type array\\.$#" count: 1 path: src/JsonSchema/Constraints/ConstraintInterface.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ConstraintInterface.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ConstraintInterface\\:\\:getErrors\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -205,49 +90,24 @@ parameters: count: 1 path: src/JsonSchema/Constraints/ConstraintInterface.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\EnumConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/EnumConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:addConfig\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Factory.php - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:createInstanceFor\\(\\) should return JsonSchema\\\\Constraints\\\\BaseConstraint&JsonSchema\\\\Constraints\\\\ConstraintInterface but returns object\\.$#" count: 1 path: src/JsonSchema/Constraints/Factory.php - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getErrorContext\\(\\) should return 1\\|2 but returns int\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Factory.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getSchemaStorage\\(\\) has no return type specified\\.$#" + message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getConfig\\(\\) should return int\\<0, 511\\> but returns int\\.$#" count: 1 path: src/JsonSchema/Constraints/Factory.php - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getTypeCheck\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Factory.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:removeConfig\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/Factory.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:setConfig\\(\\) has no return type specified\\.$#" + message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:getErrorContext\\(\\) should return 1\\|2 but returns int\\.$#" count: 1 path: src/JsonSchema/Constraints/Factory.php - - message: "#^Method JsonSchema\\\\Constraints\\\\Factory\\:\\:setErrorContext\\(\\) has no return type specified\\.$#" - count: 1 + message: "#^Property JsonSchema\\\\Constraints\\\\Factory\\:\\:\\$checkMode \\(int\\<0, 511\\>\\) does not accept int\\.$#" + count: 2 path: src/JsonSchema/Constraints/Factory.php - @@ -265,11 +125,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/Factory.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\FormatConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/FormatConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\FormatConstraint\\:\\:validateColor\\(\\) has no return type specified\\.$#" count: 1 @@ -335,11 +190,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/FormatConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\NumberConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/NumberConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\NumberConstraint\\:\\:fmod\\(\\) has no return type specified\\.$#" count: 1 @@ -365,11 +215,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/ObjectConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/ObjectConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\ObjectConstraint\\:\\:check\\(\\) has parameter \\$additionalProp with no type specified\\.$#" count: 1 @@ -430,16 +275,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/ObjectConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\SchemaConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/SchemaConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\StringConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/StringConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\StringConstraint\\:\\:strlen\\(\\) has no return type specified\\.$#" count: 1 @@ -700,11 +535,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/TypeCheck/TypeCheckInterface.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\TypeConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/TypeConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\TypeConstraint\\:\\:implodeWith\\(\\) has parameter \\$elements with no value type specified in iterable type array\\.$#" count: 1 @@ -750,16 +580,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/TypeConstraint.php - - - message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:applyDefaultValues\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/UndefinedConstraint.php - - - - message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:check\\(\\) has no return type specified\\.$#" - count: 1 - path: src/JsonSchema/Constraints/UndefinedConstraint.php - - message: "#^Method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:check\\(\\) has parameter \\$fromDefault with no type specified\\.$#" count: 1 @@ -805,11 +625,6 @@ parameters: count: 1 path: src/JsonSchema/Constraints/UndefinedConstraint.php - - - message: "#^Parameter \\#1 \\$requiredOnly of method JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:shouldApplyDefaultValue\\(\\) expects bool, int given\\.$#" - count: 3 - path: src/JsonSchema/Constraints/UndefinedConstraint.php - - message: "#^Property JsonSchema\\\\Constraints\\\\UndefinedConstraint\\:\\:\\$appliedDefaults type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index 2248ce81..26b360ae 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,6 +3,7 @@ parameters: paths: - ./src/ ignoreErrors: [] + phpVersion: 70200 includes: - phpstan-baseline.neon diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index 0b661e0f..72b0db27 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -39,18 +39,15 @@ class BaseConstraint */ protected $factory; - /** - * @param Factory $factory - */ public function __construct(?Factory $factory = null) { $this->factory = $factory ?: new Factory(); } - public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []) + public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []): void { - $message = $constraint ? $constraint->getMessage() : ''; - $name = $constraint ? $constraint->getValue() : ''; + $message = $constraint->getMessage(); + $name = $constraint->getValue(); $error = [ 'property' => $this->convertJsonPointerIntoPropertyPath($path ?: new JsonPointer('')), 'pointer' => ltrim(strval($path ?: new JsonPointer('')), '#'), @@ -76,7 +73,7 @@ public function addError(ConstraintError $constraint, ?JsonPointer $path = null, $this->errorMask |= $error['context']; } - public function addErrors(array $errors) + public function addErrors(array $errors): void { if ($errors) { $this->errors = array_merge($this->errors, $errors); @@ -89,7 +86,10 @@ public function addErrors(array $errors) } } - public function getErrors($errorContext = Validator::ERROR_ALL) + /** + * @phpstan-param int-mask-of $errorContext + */ + public function getErrors(int $errorContext = Validator::ERROR_ALL): array { if ($errorContext === Validator::ERROR_ALL) { return $this->errors; @@ -102,7 +102,10 @@ public function getErrors($errorContext = Validator::ERROR_ALL) }); } - public function numErrors($errorContext = Validator::ERROR_ALL) + /** + * @phpstan-param int-mask-of $errorContext + */ + public function numErrors(int $errorContext = Validator::ERROR_ALL): int { if ($errorContext === Validator::ERROR_ALL) { return count($this->errors); @@ -111,7 +114,7 @@ public function numErrors($errorContext = Validator::ERROR_ALL) return count($this->getErrors($errorContext)); } - public function isValid() + public function isValid(): bool { return !$this->getErrors(); } @@ -120,7 +123,7 @@ public function isValid() * Clears any reported errors. Should be used between * multiple validation checks. */ - public function reset() + public function reset(): void { $this->errors = []; $this->errorMask = Validator::ERROR_NONE; @@ -129,22 +132,17 @@ public function reset() /** * Get the error mask * - * @return int * @phpstan-return int-mask-of */ - public function getErrorMask() + public function getErrorMask(): int { return $this->errorMask; } /** * Recursively cast an associative array to an object - * - * @param array $array - * - * @return object */ - public static function arrayToObjectRecursive($array) + public static function arrayToObjectRecursive(array $array): object { $json = json_encode($array); if (json_last_error() !== \JSON_ERROR_NONE) { @@ -160,22 +158,16 @@ public static function arrayToObjectRecursive($array) /** * Transform a JSON pattern into a PCRE regex - * - * @param string $pattern - * - * @return string */ - public static function jsonPatternToPhpRegex($pattern) + public static function jsonPatternToPhpRegex(string $pattern): string { return '~' . str_replace('~', '\\~', $pattern) . '~u'; } /** - * @param JsonPointer $pointer - * * @return string property path */ - protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer) + protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer): string { $result = array_map( function ($path) { diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 297bc24c..13e62a17 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -25,7 +25,7 @@ class CollectionConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { // Verify minItems if (isset($schema->minItems) && count($value) < $schema->minItems) { @@ -59,12 +59,11 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n /** * Validates the items * - * @param array $value - * @param \stdClass $schema - * @param JsonPointer|null $path - * @param string $i + * @param array $value + * @param \stdClass $schema + * @param string $i */ - protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { if (is_object($schema->items)) { // just one type definition for the whole array diff --git a/src/JsonSchema/Constraints/ConstConstraint.php b/src/JsonSchema/Constraints/ConstConstraint.php index 9d5a1cfb..6e3a14bc 100644 --- a/src/JsonSchema/Constraints/ConstConstraint.php +++ b/src/JsonSchema/Constraints/ConstConstraint.php @@ -25,7 +25,7 @@ class ConstConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { // Only validate const if the attribute exists if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 2a45f0da..49cec0a3 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -39,12 +39,10 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface * * @param JsonPointer|null $path Current path * @param mixed $i What to append to the path - * - * @return JsonPointer; */ - protected function incrementPath(?JsonPointer $path, $i) + protected function incrementPath(?JsonPointer $path, $i): JsonPointer { - $path = $path ?: new JsonPointer(''); + $path = $path ?? new JsonPointer(''); if ($i === null || $i === '') { return $path; @@ -63,12 +61,11 @@ protected function incrementPath(?JsonPointer $path, $i) /** * Validates an array * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('collection'); $validator->check($value, $schema, $path, $i); @@ -79,15 +76,14 @@ protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null /** * Validates an object * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $properties - * @param mixed $additionalProperties - * @param mixed $patternProperties + * @param mixed $value + * @param mixed $schema + * @param mixed $properties + * @param mixed $additionalProperties + * @param mixed $patternProperties */ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = null, $properties = null, - $additionalProperties = null, $patternProperties = null, $appliedDefaults = []) + $additionalProperties = null, $patternProperties = null, $appliedDefaults = []): void { /** @var ObjectConstraint $validator */ $validator = $this->factory->createInstanceFor('object'); @@ -99,12 +95,11 @@ protected function checkObject(&$value, $schema = null, ?JsonPointer $path = nul /** * Validates the type of a property * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('type'); $validator->check($value, $schema, $path, $i); @@ -115,12 +110,11 @@ protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, /** * Checks a undefined element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false) + protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void { /** @var UndefinedConstraint $validator */ $validator = $this->factory->createInstanceFor('undefined'); @@ -133,12 +127,11 @@ protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = /** * Checks a string element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkString($value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkString($value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('string'); $validator->check($value, $schema, $path, $i); @@ -149,12 +142,11 @@ protected function checkString($value, $schema = null, ?JsonPointer $path = null /** * Checks a number element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkNumber($value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkNumber($value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('number'); $validator->check($value, $schema, $path, $i); @@ -165,12 +157,11 @@ protected function checkNumber($value, $schema = null, ?JsonPointer $path = null /** * Checks a enum element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('enum'); $validator->check($value, $schema, $path, $i); @@ -181,12 +172,11 @@ protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, /** * Checks a const element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkConst($value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkConst($value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('const'); $validator->check($value, $schema, $path, $i); @@ -197,12 +187,11 @@ protected function checkConst($value, $schema = null, ?JsonPointer $path = null, /** * Checks format of an element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i */ - protected function checkFormat($value, $schema = null, ?JsonPointer $path = null, $i = null) + protected function checkFormat($value, $schema = null, ?JsonPointer $path = null, $i = null): void { $validator = $this->factory->createInstanceFor('format'); $validator->check($value, $schema, $path, $i); @@ -212,10 +201,8 @@ protected function checkFormat($value, $schema = null, ?JsonPointer $path = null /** * Get the type check based on the set check mode. - * - * @return TypeCheck\TypeCheckInterface */ - protected function getTypeCheck() + protected function getTypeCheck(): TypeCheck\TypeCheckInterface { return $this->factory->getTypeCheck(); } diff --git a/src/JsonSchema/Constraints/ConstraintInterface.php b/src/JsonSchema/Constraints/ConstraintInterface.php index 28d5ac5a..9e43ecb5 100644 --- a/src/JsonSchema/Constraints/ConstraintInterface.php +++ b/src/JsonSchema/Constraints/ConstraintInterface.php @@ -23,45 +23,37 @@ interface ConstraintInterface { /** * returns all collected errors - * - * @return array */ - public function getErrors(); + public function getErrors(): array; /** * adds errors to this validator - * - * @param array $errors */ - public function addErrors(array $errors); + public function addErrors(array $errors): void; /** * adds an error * - * @param ConstraintError $constraint the constraint/rule that is broken, e.g.: ConstraintErrors::LENGTH_MIN() - * @param JsonPointer|null $path - * @param array $more more array elements to add to the error + * @param ConstraintError $constraint the constraint/rule that is broken, e.g.: ConstraintErrors::LENGTH_MIN() + * @param array $more more array elements to add to the error */ - public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []); + public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = []): void; /** * checks if the validator has not raised errors - * - * @return bool */ - public function isValid(); + public function isValid(): bool; /** * invokes the validation of an element * * @abstract * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer|null $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param mixed $i * * @throws \JsonSchema\Exception\ExceptionInterface */ - public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null); + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void; } diff --git a/src/JsonSchema/Constraints/EnumConstraint.php b/src/JsonSchema/Constraints/EnumConstraint.php index 2bf4e88e..1f30fadf 100644 --- a/src/JsonSchema/Constraints/EnumConstraint.php +++ b/src/JsonSchema/Constraints/EnumConstraint.php @@ -26,7 +26,7 @@ class EnumConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { // Only validate enum if the attribute exists if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { diff --git a/src/JsonSchema/Constraints/Factory.php b/src/JsonSchema/Constraints/Factory.php index 7861fc83..b9220b9d 100644 --- a/src/JsonSchema/Constraints/Factory.php +++ b/src/JsonSchema/Constraints/Factory.php @@ -24,7 +24,7 @@ class Factory { /** - * @var SchemaStorage + * @var SchemaStorageInterface */ protected $schemaStorage; @@ -35,11 +35,13 @@ class Factory /** * @var int + * @phpstan-var int-mask-of */ private $checkMode = Constraint::CHECK_MODE_NORMAL; /** - * @var TypeCheck\TypeCheckInterface[] + * @var array + * @phpstan-var array, TypeCheck\TypeCheckInterface> */ private $typeCheck = []; @@ -72,14 +74,12 @@ class Factory private $instanceCache = []; /** - * @param ?SchemaStorage $schemaStorage - * @param ?UriRetrieverInterface $uriRetriever - * @param int $checkMode + * @phpstan-param int-mask-of $checkMode */ public function __construct( ?SchemaStorageInterface $schemaStorage = null, ?UriRetrieverInterface $uriRetriever = null, - $checkMode = Constraint::CHECK_MODE_NORMAL + int $checkMode = Constraint::CHECK_MODE_NORMAL ) { // set provided config options $this->setConfig($checkMode); @@ -92,8 +92,9 @@ public function __construct( * Set config values * * @param int $checkMode Set checkMode options - does not preserve existing flags + * @phpstan-param int-mask-of $checkMode */ - public function setConfig($checkMode = Constraint::CHECK_MODE_NORMAL) + public function setConfig(int $checkMode = Constraint::CHECK_MODE_NORMAL): void { $this->checkMode = $checkMode; } @@ -101,9 +102,9 @@ public function setConfig($checkMode = Constraint::CHECK_MODE_NORMAL) /** * Enable checkMode flags * - * @param int $options + * @phpstan-param int-mask-of $options */ - public function addConfig($options) + public function addConfig(int $options): void { $this->checkMode |= $options; } @@ -111,9 +112,9 @@ public function addConfig($options) /** * Disable checkMode flags * - * @param int $options + * @phpstan-param int-mask-of $options */ - public function removeConfig($options) + public function removeConfig(int $options): void { $this->checkMode &= ~$options; } @@ -121,11 +122,12 @@ public function removeConfig($options) /** * Get checkMode option * - * @param int $options Options to get, if null then return entire bitmask + * @param int|null $options Options to get, if null then return entire bitmask + * @phpstan-param int-mask-of|null $options Options to get, if null then return entire bitmask * - * @return int + * @phpstan-return int-mask-of */ - public function getConfig($options = null) + public function getConfig(?int $options = null): int { if ($options === null) { return $this->checkMode; @@ -134,20 +136,17 @@ public function getConfig($options = null) return $this->checkMode & $options; } - /** - * @return UriRetrieverInterface - */ - public function getUriRetriever() + public function getUriRetriever(): UriRetrieverInterface { return $this->uriRetriever; } - public function getSchemaStorage() + public function getSchemaStorage(): SchemaStorageInterface { return $this->schemaStorage; } - public function getTypeCheck() + public function getTypeCheck(): TypeCheck\TypeCheckInterface { if (!isset($this->typeCheck[$this->checkMode])) { $this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST) @@ -158,13 +157,7 @@ public function getTypeCheck() return $this->typeCheck[$this->checkMode]; } - /** - * @param string $name - * @param string $class - * - * @return Factory - */ - public function setConstraintClass($name, $class) + public function setConstraintClass(string $name, string $class): Factory { // Ensure class exists if (!class_exists($class)) { @@ -186,7 +179,7 @@ public function setConstraintClass($name, $class) * * @throws InvalidArgumentException if is not possible create the constraint instance * - * @return ConstraintInterface|ObjectConstraint + * @return ConstraintInterface&BaseConstraint * @phpstan-return ConstraintInterface&BaseConstraint */ public function createInstanceFor($constraintName) @@ -205,10 +198,9 @@ public function createInstanceFor($constraintName) /** * Get the error context * - * @return int * @phpstan-return Validator::ERROR_DOCUMENT_VALIDATION|Validator::ERROR_SCHEMA_VALIDATION */ - public function getErrorContext() + public function getErrorContext(): int { return $this->errorContext; } @@ -216,10 +208,9 @@ public function getErrorContext() /** * Set the error context * - * @param int $errorContext * @phpstan-param Validator::ERROR_DOCUMENT_VALIDATION|Validator::ERROR_SCHEMA_VALIDATION $errorContext */ - public function setErrorContext($errorContext) + public function setErrorContext(int $errorContext): void { $this->errorContext = $errorContext; } diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index afb27b0f..5b07c0b5 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -27,7 +27,7 @@ class FormatConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { if (!isset($schema->format) || $this->factory->getConfig(self::CHECK_MODE_DISABLE_FORMAT)) { return; diff --git a/src/JsonSchema/Constraints/NumberConstraint.php b/src/JsonSchema/Constraints/NumberConstraint.php index 2a3e7dee..e1b2ffc3 100644 --- a/src/JsonSchema/Constraints/NumberConstraint.php +++ b/src/JsonSchema/Constraints/NumberConstraint.php @@ -25,7 +25,7 @@ class NumberConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { // Verify minimum if (isset($schema->exclusiveMinimum)) { diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index ec25ff06..011e600c 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -31,7 +31,7 @@ class ObjectConstraint extends Constraint * {@inheritdoc} */ public function check(&$element, $schema = null, ?JsonPointer $path = null, $properties = null, - $additionalProp = null, $patternProperties = null, $appliedDefaults = []) + $additionalProp = null, $patternProperties = null, $appliedDefaults = []): void { if ($element instanceof UndefinedConstraint) { return; diff --git a/src/JsonSchema/Constraints/SchemaConstraint.php b/src/JsonSchema/Constraints/SchemaConstraint.php index 5712e9d6..7852e851 100644 --- a/src/JsonSchema/Constraints/SchemaConstraint.php +++ b/src/JsonSchema/Constraints/SchemaConstraint.php @@ -31,7 +31,7 @@ class SchemaConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { if ($schema !== null) { // passed schema diff --git a/src/JsonSchema/Constraints/StringConstraint.php b/src/JsonSchema/Constraints/StringConstraint.php index ca53c2b1..7b811614 100644 --- a/src/JsonSchema/Constraints/StringConstraint.php +++ b/src/JsonSchema/Constraints/StringConstraint.php @@ -25,7 +25,7 @@ class StringConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null): void { // Verify maxLength if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) { diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index 3e9735f1..fc1aa4eb 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -42,7 +42,7 @@ class TypeConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value = null, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$value = null, $schema = null, ?JsonPointer $path = null, $i = null): void { $type = isset($schema->type) ? $schema->type : null; $isValid = false; diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index 05cc89a1..4d955cff 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -34,7 +34,7 @@ class UndefinedConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false): void { if (is_null($schema) || !is_object($schema)) { return; @@ -243,7 +243,7 @@ private function shouldApplyDefaultValue($requiredOnly, $schema, $name = null, $ * @param mixed $schema * @param JsonPointer $path */ - protected function applyDefaultValues(&$value, $schema, $path) + protected function applyDefaultValues(&$value, $schema, $path): void { // only apply defaults if feature is enabled if (!$this->factory->getConfig(self::CHECK_MODE_APPLY_DEFAULTS)) { @@ -251,7 +251,7 @@ protected function applyDefaultValues(&$value, $schema, $path) } // apply defaults if appropriate - $requiredOnly = $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS); + $requiredOnly = (bool) $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS); if (isset($schema->properties) && LooseTypeCheck::isObject($value)) { // $value is an object or assoc array, and properties are defined - treat as an object foreach ($schema->properties as $currentProperty => $propertyDefinition) { diff --git a/tests/Constraints/FactoryTest.php b/tests/Constraints/FactoryTest.php index 922d1a18..9136f50d 100644 --- a/tests/Constraints/FactoryTest.php +++ b/tests/Constraints/FactoryTest.php @@ -25,7 +25,7 @@ class MyBadConstraint class MyStringConstraint extends Constraint { - public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null): void { } }