From 5f8305ece60e6aa26f004c840ca1626630d6d7d3 Mon Sep 17 00:00:00 2001 From: pawelpawlik Date: Tue, 5 Sep 2023 17:02:38 +0200 Subject: [PATCH 1/8] IBX-6481 change keys in returned array by VersionValidator:validate() --- src/lib/Repository/Validator/VersionValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Repository/Validator/VersionValidator.php b/src/lib/Repository/Validator/VersionValidator.php index 22e908b32d..5971c1b4cb 100644 --- a/src/lib/Repository/Validator/VersionValidator.php +++ b/src/lib/Repository/Validator/VersionValidator.php @@ -86,7 +86,7 @@ public function validate( $fieldValue ); if (!empty($fieldErrors)) { - $allFieldErrors[$fieldDefinition->identifier][$languageCode] = $fieldErrors; + $allFieldErrors[$fieldDefinition->id][$languageCode] = $fieldErrors; } } } From 47d23d11fb9f3f717ef974bb72b3067f643fb4d6 Mon Sep 17 00:00:00 2001 From: pawelpawlik Date: Tue, 5 Sep 2023 17:02:38 +0200 Subject: [PATCH 2/8] IBX-6481 change keys in returned array by VersionValidator:validate() --- src/lib/Repository/Validator/VersionValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Repository/Validator/VersionValidator.php b/src/lib/Repository/Validator/VersionValidator.php index 5971c1b4cb..c9e5580209 100644 --- a/src/lib/Repository/Validator/VersionValidator.php +++ b/src/lib/Repository/Validator/VersionValidator.php @@ -73,7 +73,7 @@ public function validate( if ($fieldType->isEmptyValue($fieldValue)) { if ($fieldDefinition->isRequired) { - $allFieldErrors[$fieldDefinition->identifier][$languageCode] = new ValidationError( + $allFieldErrors[$fieldDefinition->id][$languageCode] = new ValidationError( "Value for required field definition '%identifier%' with language '%languageCode%' is empty", null, ['%identifier%' => $fieldDefinition->identifier, '%languageCode%' => $languageCode], From ef7c9311bc7df3a47029ef091c2fe179c4804e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Fri, 15 Sep 2023 11:35:47 +0200 Subject: [PATCH 3/8] Fixed Phpstan declaration for Content Validators --- phpstan-baseline.neon | 40 ------------------- .../Repository/Validator/ContentValidator.php | 10 ++++- .../ContentType/FieldDefinitionCollection.php | 3 ++ .../Repository/Validator/VersionValidator.php | 6 +++ 4 files changed, 18 insertions(+), 41 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index de22585388..023f1c9570 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -7070,11 +7070,6 @@ parameters: count: 1 path: src/contracts/Repository/Validator/ContentValidator.php - - - message: "#^Method Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Validator\\\\ContentValidator\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/contracts/Repository/Validator/ContentValidator.php - - message: "#^Class Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Bookmark\\\\BookmarkList implements generic interface IteratorAggregate but does not specify its types\\: TKey, TValue$#" count: 1 @@ -7500,11 +7495,6 @@ parameters: count: 1 path: src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php - - - message: "#^Interface Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinitionCollection extends generic interface IteratorAggregate but does not specify its types\\: TKey, TValue$#" - count: 1 - path: src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php - - message: "#^Method Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\ContentType\\\\FieldDefinitionCollection\\:\\:map\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -21405,11 +21395,6 @@ parameters: count: 1 path: src/lib/Repository/Strategy/ContentValidator/ContentValidatorStrategy.php - - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\Strategy\\\\ContentValidator\\\\ContentValidatorStrategy\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Repository/Strategy/ContentValidator/ContentValidatorStrategy.php - - message: "#^Property Ibexa\\\\Core\\\\Repository\\\\Strategy\\\\ContentValidator\\\\ContentValidatorStrategy\\:\\:\\$contentValidators \\(array\\\\) does not accept iterable\\.$#" count: 1 @@ -21670,11 +21655,6 @@ parameters: count: 1 path: src/lib/Repository/Validator/ContentCreateStructValidator.php - - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\Validator\\\\ContentCreateStructValidator\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Repository/Validator/ContentCreateStructValidator.php - - message: "#^Cannot access property \\$value on Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Field\\|null\\.$#" count: 2 @@ -21685,11 +21665,6 @@ parameters: count: 1 path: src/lib/Repository/Validator/ContentUpdateStructValidator.php - - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\Validator\\\\ContentUpdateStructValidator\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Repository/Validator/ContentUpdateStructValidator.php - - message: "#^Property Ibexa\\\\Core\\\\Repository\\\\Validator\\\\ContentUpdateStructValidator\\:\\:\\$contentLanguageHandler \\(Ibexa\\\\Core\\\\Persistence\\\\Legacy\\\\Content\\\\Language\\\\Handler\\) does not accept Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Language\\\\Handler\\.$#" count: 1 @@ -21720,16 +21695,6 @@ parameters: count: 1 path: src/lib/Repository/Validator/UserPasswordValidator.php - - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\Validator\\\\VersionValidator\\:\\:validate\\(\\) has parameter \\$context with no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Repository/Validator/VersionValidator.php - - - - message: "#^Method Ibexa\\\\Core\\\\Repository\\\\Validator\\\\VersionValidator\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: src/lib/Repository/Validator/VersionValidator.php - - message: "#^Call to method getValue\\(\\) on an unknown class eZ\\\\Publish\\\\API\\\\Repository\\\\Values\\\\Content\\\\Field\\.$#" count: 1 @@ -56730,11 +56695,6 @@ parameters: count: 1 path: tests/lib/Repository/ContentValidator/ContentValidatorStrategyTest.php - - - message: "#^Method class@anonymous/tests/lib/Repository/ContentValidator/ContentValidatorStrategyTest\\.php\\:87\\:\\:validate\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/lib/Repository/ContentValidator/ContentValidatorStrategyTest.php - - message: "#^Property class@anonymous/tests/lib/Repository/ContentValidator/ContentValidatorStrategyTest\\.php\\:87\\:\\:\\$validationReturn type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/src/contracts/Repository/Validator/ContentValidator.php b/src/contracts/Repository/Validator/ContentValidator.php index e8303da534..333a0cae08 100644 --- a/src/contracts/Repository/Validator/ContentValidator.php +++ b/src/contracts/Repository/Validator/ContentValidator.php @@ -21,7 +21,15 @@ public function supports(ValueObject $object): bool; * @param string[]|null $fieldIdentifiers List of field identifiers for partial validation or null for * case of full validation. Empty identifiers array is equal to no validation. * - * @return array Grouped validation errors by field definition and language code, in format: + * @phpstan-return array< + * int|string, + * array< + * string, + * \Ibexa\Contracts\Core\FieldType\ValidationError|\Ibexa\Contracts\Core\FieldType\ValidationError[] + * > + * > + * + * @return array Grouped validation errors by field definition ID and language code, in format: * $returnValue[string|int $fieldDefinitionId][string $languageCode] = $fieldErrors; * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException diff --git a/src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php b/src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php index 30417f23e9..f43b4c749d 100644 --- a/src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php +++ b/src/contracts/Repository/Values/ContentType/FieldDefinitionCollection.php @@ -13,6 +13,9 @@ use Countable; use IteratorAggregate; +/** + * @phpstan-extends \IteratorAggregate<\Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition> + */ interface FieldDefinitionCollection extends Countable, IteratorAggregate, ArrayAccess { /** diff --git a/src/lib/Repository/Validator/VersionValidator.php b/src/lib/Repository/Validator/VersionValidator.php index c9e5580209..7057780bc1 100644 --- a/src/lib/Repository/Validator/VersionValidator.php +++ b/src/lib/Repository/Validator/VersionValidator.php @@ -35,6 +35,12 @@ public function supports(ValueObject $object): bool return $object instanceof VersionInfo; } + /** + * @phpstan-param array{ + * content?: \Ibexa\Contracts\Core\Repository\Values\Content\Content, + * translations?: string[], + * } $context + */ public function validate( ValueObject $object, array $context = [], From edae60534d122aab598d3fd0f05aaa2ddfc94128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 18 Sep 2023 13:18:55 +0200 Subject: [PATCH 4/8] Update src/contracts/Repository/Validator/ContentValidator.php --- src/contracts/Repository/Validator/ContentValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contracts/Repository/Validator/ContentValidator.php b/src/contracts/Repository/Validator/ContentValidator.php index 333a0cae08..787e0bbf42 100644 --- a/src/contracts/Repository/Validator/ContentValidator.php +++ b/src/contracts/Repository/Validator/ContentValidator.php @@ -22,7 +22,7 @@ public function supports(ValueObject $object): bool; * case of full validation. Empty identifiers array is equal to no validation. * * @phpstan-return array< - * int|string, + * int, * array< * string, * \Ibexa\Contracts\Core\FieldType\ValidationError|\Ibexa\Contracts\Core\FieldType\ValidationError[] From 8d5565134c422db13d7d81692afa5e5e494265ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 18 Sep 2023 15:25:21 +0200 Subject: [PATCH 5/8] Fixed Phpstan declaration for Content Validators --- .../Values/ContentType/FieldDefinition.php | 4 ++-- .../ContentFieldValidationException.php | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/contracts/Repository/Values/ContentType/FieldDefinition.php b/src/contracts/Repository/Values/ContentType/FieldDefinition.php index 69d4aaa233..f27f93bb68 100644 --- a/src/contracts/Repository/Values/ContentType/FieldDefinition.php +++ b/src/contracts/Repository/Values/ContentType/FieldDefinition.php @@ -17,7 +17,7 @@ * * @property-read array $fieldSettings calls getFieldSettings() * @property-read array $validatorConfiguration calls getValidatorConfiguration() - * @property-read mixed $id the id of the field definition + * @property-read int $id the id of the field definition * @property-read string $identifier the identifier of the field definition * @property-read string $fieldGroup the field group name * @property-read int $position the position of the field definition in the content type @@ -35,7 +35,7 @@ abstract class FieldDefinition extends ValueObject implements MultiLanguageName, /** * the unique id of this field definition. * - * @var mixed + * @var int */ protected $id; diff --git a/src/lib/Base/Exceptions/ContentFieldValidationException.php b/src/lib/Base/Exceptions/ContentFieldValidationException.php index a5c253ddb8..e0f646df1e 100644 --- a/src/lib/Base/Exceptions/ContentFieldValidationException.php +++ b/src/lib/Base/Exceptions/ContentFieldValidationException.php @@ -25,10 +25,10 @@ class ContentFieldValidationException extends APIContentFieldValidationException * Example: * * $fieldErrors = $exception->getFieldErrors(); - * $fieldErrors["43"]["eng-GB"]->getTranslatableMessage(); + * $fieldErrors[43]["eng-GB"]->getTranslatableMessage(); * * - * @var array> + * @var array> */ protected $errors; @@ -40,7 +40,7 @@ class ContentFieldValidationException extends APIContentFieldValidationException * * Also sets the given $fieldErrors to the internal property, retrievable by getFieldErrors() * - * @param array> $errors + * @param array> $errors */ public function __construct(array $errors) { @@ -52,7 +52,7 @@ public function __construct(array $errors) /** * Generates: Content fields did not validate exception with additional information on affected fields. * - * @param array> $errors + * @param array> $errors */ public static function createNewWithMultiline(array $errors, ?string $contentName = null): self { @@ -72,7 +72,7 @@ public static function createNewWithMultiline(array $errors, ?string $contentNam /** * Returns an array of field validation error messages. * - * @return array> + * @return array> */ public function getFieldErrors() { @@ -89,11 +89,17 @@ private function generateValidationErrorsMessages(): string $validationErrors[] = sprintf('Limit: %d of validation errors has been exceeded.', $maxMessagesNumber); } + /** @var callable(string|\Ibexa\Contracts\Core\Repository\Values\Translation): string $convertToString */ + $convertToString = function ($error): string { + return (string)$error; + }; + $validationErrors = array_map($convertToString, $validationErrors); + return "\n- " . implode("\n- ", $validationErrors); } /** - * @return array<\Ibexa\Core\FieldType\ValidationError> + * @return array<\Ibexa\Contracts\Core\Repository\Values\Translation> */ private function collectValidationErrors(): array { From 929e75ec7fab6679cd09619bccd546b09f72a68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 18 Sep 2023 15:28:27 +0200 Subject: [PATCH 6/8] Updated PHPStan baseline --- phpstan-baseline.neon | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 023f1c9570..6cf3b7b003 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -8105,11 +8105,6 @@ parameters: count: 1 path: src/lib/Base/Exceptions/ContentFieldValidationException.php - - - message: "#^Parameter \\#2 \\$array of function implode expects array\\, array\\ given\\.$#" - count: 1 - path: src/lib/Base/Exceptions/ContentFieldValidationException.php - - message: "#^Property Ibexa\\\\Core\\\\Base\\\\Exceptions\\\\ContentFieldValidationException\\:\\:\\$messageTemplate has no type specified\\.$#" count: 1 @@ -20205,6 +20200,11 @@ parameters: count: 1 path: src/lib/Repository/ContentService.php + - + message: "#^Parameter \\#5 \\$fieldDefinitionId of method Ibexa\\\\Core\\\\Repository\\\\Helper\\\\RelationProcessor\\:\\:appendFieldRelations\\(\\) expects string, int given\\.$#" + count: 2 + path: src/lib/Repository/ContentService.php + - message: "#^Property Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\MetadataUpdateStruct\\:\\:\\$publicationDate \\(int\\) does not accept int\\|null\\.$#" count: 1 @@ -21605,11 +21605,6 @@ parameters: count: 1 path: src/lib/Repository/UserService.php - - - message: "#^Parameter \\#1 \\$errors of static method Ibexa\\\\Core\\\\Base\\\\Exceptions\\\\ContentFieldValidationException\\:\\:createNewWithMultiline\\(\\) expects array\\\\>, array\\\\>\\> given\\.$#" - count: 1 - path: src/lib/Repository/UserService.php - - message: "#^Parameter \\#1 \\$identifier of method Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\ContentTypeService\\:\\:loadContentTypeByIdentifier\\(\\) expects string, string\\|false given\\.$#" count: 2 From 5030c81a9827d0a8ef53c94b5c747e001ded526c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 18 Sep 2023 15:30:07 +0200 Subject: [PATCH 7/8] Update src/contracts/Repository/Validator/ContentValidator.php --- src/contracts/Repository/Validator/ContentValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/contracts/Repository/Validator/ContentValidator.php b/src/contracts/Repository/Validator/ContentValidator.php index 787e0bbf42..e71607afde 100644 --- a/src/contracts/Repository/Validator/ContentValidator.php +++ b/src/contracts/Repository/Validator/ContentValidator.php @@ -30,7 +30,7 @@ public function supports(ValueObject $object): bool; * > * * @return array Grouped validation errors by field definition ID and language code, in format: - * $returnValue[string|int $fieldDefinitionId][string $languageCode] = $fieldErrors; + * $returnValue[int $fieldDefinitionId][string $languageCode] = $fieldErrors; * * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException */ From 2e0375dccaf44d7b0f8a5144e3f8f8f6ce911886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Niedzielski?= Date: Mon, 18 Sep 2023 16:45:41 +0200 Subject: [PATCH 8/8] Fixed code style --- src/lib/Base/Exceptions/ContentFieldValidationException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/Base/Exceptions/ContentFieldValidationException.php b/src/lib/Base/Exceptions/ContentFieldValidationException.php index e0f646df1e..0b8091dca4 100644 --- a/src/lib/Base/Exceptions/ContentFieldValidationException.php +++ b/src/lib/Base/Exceptions/ContentFieldValidationException.php @@ -90,7 +90,7 @@ private function generateValidationErrorsMessages(): string } /** @var callable(string|\Ibexa\Contracts\Core\Repository\Values\Translation): string $convertToString */ - $convertToString = function ($error): string { + $convertToString = static function ($error): string { return (string)$error; }; $validationErrors = array_map($convertToString, $validationErrors);