diff --git a/LICENSE b/LICENSE index bb61236..7b2b327 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2021 Dominik Zogg +Copyright (c) 2022 Dominik Zogg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index b785d7d..d762ef1 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,14 @@ A simple validation. ## Requirements - * php: ^7.4|^8.0 - * psr/log: ^1.0 + * php: ^8.0 + * psr/log: ^1.1.4|^2.0|^3.0 ## Suggest - * chubbyphp/chubbyphp-container: ^1.0 - * pimple/pimple: ^3.2.3 - * psr/container: ^1.0 + * chubbyphp/chubbyphp-container: ^2.1 + * pimple/pimple: ^3.5 + * psr/container: ^1.1.2|^2.0.2 ## Installation @@ -223,7 +223,7 @@ $errors = $validator->validate( ## Copyright -Dominik Zogg 2021 +Dominik Zogg 2022 [1]: https://packagist.org/packages/chubbyphp/chubbyphp-validation diff --git a/composer.json b/composer.json index d60d816..d32360c 100644 --- a/composer.json +++ b/composer.json @@ -13,23 +13,23 @@ "php": "^8.0", "ext-filter": "*", "ext-json": "*", - "psr/log": "^1.0" + "psr/log": "^1.1.4|^2.0|^3.0" }, "require-dev": { - "chubbyphp/chubbyphp-container": "^1.0|^2.0", + "chubbyphp/chubbyphp-container": "^2.1", "chubbyphp/chubbyphp-dev-helper": "dev-master", "chubbyphp/chubbyphp-laminas-config-factory": "^1.0", "chubbyphp/chubbyphp-mock": "^1.6.1", - "doctrine/persistence": "^1.3|^2.0", - "infection/infection": "^0.25.3", - "php-coveralls/php-coveralls": "^2.5.1", + "doctrine/persistence": "^2.3", + "infection/infection": "^0.26.5", + "php-coveralls/php-coveralls": "^2.5.2", "phploc/phploc": "^7.0.2", "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^1.1.2", - "phpunit/phpunit": "^9.5.10", - "pimple/pimple": "^3.3", - "psr/container": "^1.0|^2.0", - "symfony/validator": "^5.3.11|^6.0.0" + "phpstan/phpstan": "^1.4.8", + "phpunit/phpunit": "^9.5.17", + "pimple/pimple": "^3.5", + "psr/container": "^2.0.2", + "symfony/validator": "^5.4.5|^6.0" }, "autoload": { "psr-4": { "Chubbyphp\\Validation\\": "src/" } @@ -38,7 +38,11 @@ "psr-4": { "Chubbyphp\\Tests\\Validation\\": "tests/" } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "infection/extension-installer": true, + "phpstan/extension-installer": true + } }, "extra": { "branch-alias": { @@ -57,7 +61,7 @@ "@test:cs" ], "test:cs": "mkdir -p build && PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --cache-file=build/phpcs.cache", - "test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=90 --verbose --coverage=build/phpunit", + "test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=91 --verbose --coverage=build/phpunit", "test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-result-file=build/phpunit/result.cache", "test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log", "test:loc": "mkdir -p build && vendor/bin/phploc src | tee build/phploc.log", diff --git a/infection.json b/infection.json index 70dba91..96e37a6 100644 --- a/infection.json +++ b/infection.json @@ -6,9 +6,12 @@ }, "timeout": 10, "logs": { - "text": "build/phpinfection.log", - "badge": { - "branch": "master" + "text": "build/phpinfection/infection.log", + "html": "build/phpinfection/infection.html", + "json": "build/phpinfection/infection.json", + "summary": "build/phpinfection/summary.log", + "stryker": { + "report": "master" } }, "mutators": { diff --git a/phpstan.neon b/phpstan.neon index 29c6d9b..a919395 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,3 +9,6 @@ parameters: - message: '/PHPDoc tag @var for variable \$parentReflectionClass contains generic class ReflectionClass but does not specify its types\: T/' path: %currentWorkingDirectory%/src/Mapping/ValidationMappingProviderRegistry.php + - + message: '/Property Chubbyphp\\Validation\\Error\\ErrorMessages\:\:\$errorMessages \(array\\>\|null\) does not accept array\\./' + path: %currentWorkingDirectory%/src/Error/ErrorMessages.php diff --git a/src/Accessor/MethodAccessor.php b/src/Accessor/MethodAccessor.php index 8cd1b16..fd1c681 100644 --- a/src/Accessor/MethodAccessor.php +++ b/src/Accessor/MethodAccessor.php @@ -8,11 +8,8 @@ final class MethodAccessor implements AccessorInterface { - private string $property; - - public function __construct(string $property) + public function __construct(private string $property) { - $this->property = $property; } /** @@ -39,7 +36,7 @@ public function getValue(object $object) } throw ValidatorLogicException::createMissingMethod( - \get_class($object), + $object::class, [$getMethodName, $hasMethodName, $isMethodName] ); } diff --git a/src/Accessor/PropertyAccessor.php b/src/Accessor/PropertyAccessor.php index 0d9e356..684a8ce 100644 --- a/src/Accessor/PropertyAccessor.php +++ b/src/Accessor/PropertyAccessor.php @@ -9,11 +9,8 @@ final class PropertyAccessor implements AccessorInterface { - private string $property; - - public function __construct(string $property) + public function __construct(private string $property) { - $this->property = $property; } /** @@ -49,6 +46,6 @@ private function getClass(object $object): string } } - return \get_class($object); + return $object::class; } } diff --git a/src/Constraint/AllConstraint.php b/src/Constraint/AllConstraint.php index 55e76c3..1fff845 100644 --- a/src/Constraint/AllConstraint.php +++ b/src/Constraint/AllConstraint.php @@ -49,7 +49,7 @@ public function validate( return [new Error( $path.'[_all]', 'constraint.all.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/ChoiceConstraint.php b/src/Constraint/ChoiceConstraint.php index 65a4fb9..a770d97 100644 --- a/src/Constraint/ChoiceConstraint.php +++ b/src/Constraint/ChoiceConstraint.php @@ -11,17 +11,11 @@ final class ChoiceConstraint implements ConstraintInterface { - /** - * @var array - */ - private array $choices = []; - /** * @param array $choices */ - public function __construct(array $choices) + public function __construct(private array $choices) { - $this->choices = $choices; } /** diff --git a/src/Constraint/CoordinateArrayConstraint.php b/src/Constraint/CoordinateArrayConstraint.php index 07e8593..1d80414 100644 --- a/src/Constraint/CoordinateArrayConstraint.php +++ b/src/Constraint/CoordinateArrayConstraint.php @@ -30,19 +30,19 @@ public function validate( return [new Error( $path, 'constraint.coordinatearray.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } if (!isset($value['lat']) || !isset($value['lon'])) { - return [new Error($path, 'constraint.coordinatearray.invalidformat', ['value' => json_encode($value)])]; + return [new Error($path, 'constraint.coordinatearray.invalidformat', ['value' => json_encode($value, JSON_THROW_ON_ERROR)])]; } $lat = $value['lat']; $lon = $value['lon']; if ($lat < -90 || $lat > 90 || $lon < -180 || $lon > 180) { - return [new Error($path, 'constraint.coordinatearray.invalidvalue', ['value' => json_encode($value)])]; + return [new Error($path, 'constraint.coordinatearray.invalidvalue', ['value' => json_encode($value, JSON_THROW_ON_ERROR)])]; } return []; diff --git a/src/Constraint/CoordinateConstraint.php b/src/Constraint/CoordinateConstraint.php index 055cbfb..2729611 100644 --- a/src/Constraint/CoordinateConstraint.php +++ b/src/Constraint/CoordinateConstraint.php @@ -32,7 +32,7 @@ public function validate( return [new Error( $path, 'constraint.coordinate.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/CountConstraint.php b/src/Constraint/CountConstraint.php index 5bb4845..962572d 100644 --- a/src/Constraint/CountConstraint.php +++ b/src/Constraint/CountConstraint.php @@ -11,14 +11,8 @@ final class CountConstraint implements ConstraintInterface { - private ?int $min = null; - - private ?int $max = null; - - public function __construct(?int $min = null, ?int $max = null) + public function __construct(private ?int $min = null, private ?int $max = null) { - $this->min = $min; - $this->max = $max; } /** @@ -40,7 +34,7 @@ public function validate( return [new Error( $path, 'constraint.count.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/DateTimeConstraint.php b/src/Constraint/DateTimeConstraint.php index 07d534f..d9a94f2 100644 --- a/src/Constraint/DateTimeConstraint.php +++ b/src/Constraint/DateTimeConstraint.php @@ -11,11 +11,8 @@ final class DateTimeConstraint implements ConstraintInterface { - private string $format; - - public function __construct(string $format = 'Y-m-d H:i:s') + public function __construct(private string $format = 'Y-m-d H:i:s') { - $this->format = $format; } /** @@ -41,7 +38,7 @@ public function validate( return [new Error( $path, 'constraint.datetime.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/Doctrine/UniqueConstraint.php b/src/Constraint/Doctrine/UniqueConstraint.php index 55be568..f58f452 100644 --- a/src/Constraint/Doctrine/UniqueConstraint.php +++ b/src/Constraint/Doctrine/UniqueConstraint.php @@ -14,20 +14,11 @@ final class UniqueConstraint implements ConstraintInterface { - private ObjectManager $objectManager; - - /** - * @var array - */ - private array $uniqueProperties; - /** * @param array $uniqueProperties */ - public function __construct(ObjectManager $objectManager, array $uniqueProperties) + public function __construct(private ObjectManager $objectManager, private array $uniqueProperties) { - $this->objectManager = $objectManager; - $this->uniqueProperties = $uniqueProperties; } /** @@ -48,7 +39,7 @@ public function validate( $criteria = $this->getCriteria($model); /** @var class-string $modelClass */ - $modelClass = \get_class($model); + $modelClass = $model::class; if (null === $persistedModel = $this->objectManager->getRepository($modelClass)->findOneBy($criteria)) { return []; diff --git a/src/Constraint/EmailConstraint.php b/src/Constraint/EmailConstraint.php index a26a406..0ca1c95 100644 --- a/src/Constraint/EmailConstraint.php +++ b/src/Constraint/EmailConstraint.php @@ -35,7 +35,7 @@ public function validate( return [new Error( $path, 'constraint.email.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/MapConstraint.php b/src/Constraint/MapConstraint.php index 538c44e..93aa16c 100644 --- a/src/Constraint/MapConstraint.php +++ b/src/Constraint/MapConstraint.php @@ -62,7 +62,7 @@ public function validate( return [new Error( $path, 'constraint.map.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/NumericConstraint.php b/src/Constraint/NumericConstraint.php index 170626c..7f648cf 100644 --- a/src/Constraint/NumericConstraint.php +++ b/src/Constraint/NumericConstraint.php @@ -30,7 +30,7 @@ public function validate( return [new Error( $path, 'constraint.numeric.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } diff --git a/src/Constraint/NumericRangeConstraint.php b/src/Constraint/NumericRangeConstraint.php index 61079f7..8ecb892 100644 --- a/src/Constraint/NumericRangeConstraint.php +++ b/src/Constraint/NumericRangeConstraint.php @@ -11,14 +11,8 @@ final class NumericRangeConstraint implements ConstraintInterface { - private ?int $min = null; - - private ?int $max = null; - - public function __construct(?int $min = null, ?int $max = null) + public function __construct(private ?int $min = null, private ?int $max = null) { - $this->min = $min; - $this->max = $max; } /** diff --git a/src/Constraint/SortConstraint.php b/src/Constraint/SortConstraint.php index 52ab426..09ee896 100644 --- a/src/Constraint/SortConstraint.php +++ b/src/Constraint/SortConstraint.php @@ -14,17 +14,11 @@ final class SortConstraint implements ConstraintInterface { private const ALLOWED_ORDERS = ['asc', 'desc']; - /** - * @var array - */ - private array $allowedFields; - /** * @param array $allowedFields */ - public function __construct(array $allowedFields) + public function __construct(private array $allowedFields) { - $this->allowedFields = $allowedFields; } /** @@ -44,7 +38,7 @@ public function validate( return [new Error( $path, 'constraint.sort.invalidtype', - ['type' => \is_object($sort) ? \get_class($sort) : \gettype($sort)] + ['type' => get_debug_type($sort)] )]; } @@ -78,7 +72,7 @@ private function validateFieldAndOrder(string $path, string $field, $order): arr $errors[] = new Error( $path.'.'.$field, 'constraint.sort.order.invalidtype', - ['field' => $field, 'type' => \is_object($order) ? \get_class($order) : \gettype($order)] + ['field' => $field, 'type' => get_debug_type($order)] ); return $errors; diff --git a/src/Constraint/Symfony/ConstraintAdapter.php b/src/Constraint/Symfony/ConstraintAdapter.php index f35751f..12750ee 100644 --- a/src/Constraint/Symfony/ConstraintAdapter.php +++ b/src/Constraint/Symfony/ConstraintAdapter.php @@ -13,14 +13,8 @@ final class ConstraintAdapter implements ConstraintInterface { - private Constraint $symfonyConstraint; - - private ConstraintValidatorInterface $symfonyConstraintValidator; - - public function __construct(Constraint $symfonyConstraint, ConstraintValidatorInterface $symfonyConstraintValidator) + public function __construct(private Constraint $symfonyConstraint, private ConstraintValidatorInterface $symfonyConstraintValidator) { - $this->symfonyConstraint = $symfonyConstraint; - $this->symfonyConstraintValidator = $symfonyConstraintValidator; } /** diff --git a/src/Constraint/Symfony/ConstraintViolationBuilder.php b/src/Constraint/Symfony/ConstraintViolationBuilder.php index f9a4e29..fe8d1ed 100644 --- a/src/Constraint/Symfony/ConstraintViolationBuilder.php +++ b/src/Constraint/Symfony/ConstraintViolationBuilder.php @@ -11,54 +11,26 @@ final class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface { - /** - * @var ConstraintViolationListInterface - */ - private ConstraintViolationListInterface $violations; - - private string $path; - - private string $message; - - /** - * @var array - */ - private array $parameters; - - /** - * @var mixed - */ - private $invalidValue; + private mixed $invalidValue = null; private ?int $plural = null; private ?string $code = null; - /** - * @var mixed - */ - private $cause; + private mixed $cause = null; /** * @param ConstraintViolationListInterface $violations * @param array $parameters */ public function __construct( - ConstraintViolationListInterface $violations, - string $message, - array $parameters, - string $path + private ConstraintViolationListInterface $violations, + private string $message, + private array $parameters, + private string $path ) { - $this->violations = $violations; - - $this->message = $message; - $this->parameters = $parameters; - $this->path = $path; } - /** - * @return ConstraintViolationBuilder - */ public function atPath(string $path): static { $this->path = $path; @@ -66,9 +38,6 @@ public function atPath(string $path): static return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setParameter(string $key, string $value): static { $this->parameters[$key] = $value; @@ -78,8 +47,6 @@ public function setParameter(string $key, string $value): static /** * @param array $parameters - * - * @return ConstraintViolationBuilder */ public function setParameters(array $parameters): static { @@ -88,17 +55,11 @@ public function setParameters(array $parameters): static return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setTranslationDomain(string $translationDomain): static { return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setInvalidValue(mixed $invalidValue): static { $this->invalidValue = $invalidValue; @@ -106,9 +67,6 @@ public function setInvalidValue(mixed $invalidValue): static return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setPlural(int $number): static { $this->plural = $number; @@ -116,9 +74,6 @@ public function setPlural(int $number): static return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setCode(?string $code): static { $this->code = $code; @@ -126,9 +81,6 @@ public function setCode(?string $code): static return $this; } - /** - * @return ConstraintViolationBuilder - */ public function setCause(mixed $cause): static { $this->cause = $cause; diff --git a/src/Constraint/Symfony/ExecutionContext.php b/src/Constraint/Symfony/ExecutionContext.php index 946bc9e..f46956f 100644 --- a/src/Constraint/Symfony/ExecutionContext.php +++ b/src/Constraint/Symfony/ExecutionContext.php @@ -19,25 +19,12 @@ final class ExecutionContext implements ExecutionContextInterface { private ConstraintViolationList $violations; - private string $path; - - /** - * @var mixed - */ - private $value; - - private ValidatorContextInterface $context; - /** * @param mixed $value */ - public function __construct(string $path, $value, ValidatorContextInterface $context) + public function __construct(private string $path, private $value, private ValidatorContextInterface $context) { $this->violations = new ConstraintViolationList(); - - $this->path = $path; - $this->value = $value; - $this->context = $context; } /** @@ -170,6 +157,7 @@ public function getPropertyPath(string $subPath = ''): string public function getErrors(): array { $errors = []; + /** @var ConstraintViolation $violation */ foreach ($this->violations as $violation) { $errors[] = new Error( diff --git a/src/Constraint/TypeConstraint.php b/src/Constraint/TypeConstraint.php index 31d69e7..9733729 100644 --- a/src/Constraint/TypeConstraint.php +++ b/src/Constraint/TypeConstraint.php @@ -11,11 +11,8 @@ final class TypeConstraint implements ConstraintInterface { - private string $wishedType; - - public function __construct(string $wishedType) + public function __construct(private string $wishedType) { - $this->wishedType = $wishedType; } /** @@ -40,7 +37,7 @@ public function validate( return []; } - return [$this->getInvalidTypeErrorByPathAndType($path, (string) \get_class($value))]; + return [$this->getInvalidTypeErrorByPathAndType($path, (string) $value::class)]; } if ($type === $this->wishedType) { diff --git a/src/Constraint/ValidConstraint.php b/src/Constraint/ValidConstraint.php index 77c93be..0285ccd 100644 --- a/src/Constraint/ValidConstraint.php +++ b/src/Constraint/ValidConstraint.php @@ -33,7 +33,7 @@ public function validate( throw ValidatorLogicException::createMissingValidator($path); } - if (\is_array($value) || $value instanceof \Traversable) { + if (is_iterable($value)) { $errors = []; foreach ($value as $i => $subValue) { $errors = array_merge($errors, $validator->validate($subValue, $context, $path.'['.$i.']')); @@ -49,7 +49,7 @@ public function validate( return [new Error( $path, 'constraint.valid.invalidtype', - ['type' => \is_object($value) ? \get_class($value) : \gettype($value)] + ['type' => get_debug_type($value)] )]; } } diff --git a/src/Error/Error.php b/src/Error/Error.php index fd3e078..15c5569 100644 --- a/src/Error/Error.php +++ b/src/Error/Error.php @@ -6,23 +6,11 @@ final class Error implements ErrorInterface { - private string $path; - - private string $key; - - /** - * @var array - */ - private array $arguments; - /** * @param array $arguments */ - public function __construct(string $path, string $key, array $arguments = []) + public function __construct(private string $path, private string $key, private array $arguments = []) { - $this->path = $path; - $this->key = $key; - $this->arguments = $arguments; } public function getPath(): string diff --git a/src/Mapping/CallableValidationMappingProvider.php b/src/Mapping/CallableValidationMappingProvider.php index 8c78f31..35d7ebf 100644 --- a/src/Mapping/CallableValidationMappingProvider.php +++ b/src/Mapping/CallableValidationMappingProvider.php @@ -6,8 +6,6 @@ final class CallableValidationMappingProvider implements ValidationMappingProviderInterface { - private string $class; - /** * @var callable */ @@ -15,9 +13,8 @@ final class CallableValidationMappingProvider implements ValidationMappingProvid private ?ValidationMappingProviderInterface $mapping = null; - public function __construct(string $class, callable $callable) + public function __construct(private string $class, callable $callable) { - $this->class = $class; $this->callable = $callable; } diff --git a/src/Mapping/LazyValidationMappingProvider.php b/src/Mapping/LazyValidationMappingProvider.php index e4f9360..b91f1e3 100644 --- a/src/Mapping/LazyValidationMappingProvider.php +++ b/src/Mapping/LazyValidationMappingProvider.php @@ -8,20 +8,11 @@ final class LazyValidationMappingProvider implements ValidationMappingProviderInterface { - private ContainerInterface $container; - - private string $serviceId; - - private string $class; - /** * @param string $serviceId */ - public function __construct(ContainerInterface $container, $serviceId, string $class) + public function __construct(private ContainerInterface $container, private $serviceId, private string $class) { - $this->container = $container; - $this->serviceId = $serviceId; - $this->class = $class; } public function getClass(): string diff --git a/src/Mapping/ValidationClassMapping.php b/src/Mapping/ValidationClassMapping.php index 0ef4869..469c62d 100644 --- a/src/Mapping/ValidationClassMapping.php +++ b/src/Mapping/ValidationClassMapping.php @@ -8,24 +8,12 @@ final class ValidationClassMapping implements ValidationClassMappingInterface { - /** - * @var array - */ - private array $constraints; - - /** - * @var array - */ - private array $groups; - /** * @param array $constraints * @param array $groups */ - public function __construct(array $constraints, array $groups) + public function __construct(private array $constraints, private array $groups) { - $this->constraints = $constraints; - $this->groups = $groups; } /** diff --git a/src/Mapping/ValidationPropertyMapping.php b/src/Mapping/ValidationPropertyMapping.php index 11c400d..f9e6df7 100644 --- a/src/Mapping/ValidationPropertyMapping.php +++ b/src/Mapping/ValidationPropertyMapping.php @@ -9,34 +9,16 @@ final class ValidationPropertyMapping implements ValidationPropertyMappingInterface { - private string $name; - - /** - * @var array - */ - private array $constraints; - - /** - * @var array - */ - private array $groups; - - private AccessorInterface $accessor; - /** * @param array $constraints * @param array $groups */ public function __construct( - string $name, - array $constraints, - array $groups, - AccessorInterface $accessor + private string $name, + private array $constraints, + private array $groups, + private AccessorInterface $accessor ) { - $this->name = $name; - $this->constraints = $constraints; - $this->groups = $groups; - $this->accessor = $accessor; } public function getName(): string diff --git a/src/Validator.php b/src/Validator.php index cb67048..38635fa 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -16,15 +16,12 @@ final class Validator implements ValidatorInterface { - private ValidationMappingProviderRegistryInterface $validatorObjectMappingRegistry; - private LoggerInterface $logger; public function __construct( - ValidationMappingProviderRegistryInterface $validatorObjectMappingRegistry, + private ValidationMappingProviderRegistryInterface $validatorObjectMappingRegistry, ?LoggerInterface $logger = null ) { - $this->validatorObjectMappingRegistry = $validatorObjectMappingRegistry; $this->logger = $logger ?? new NullLogger(); } @@ -35,19 +32,16 @@ public function validate(object $object, ?ValidatorContextInterface $context = n { $context ??= ValidatorContextBuilder::create()->getContext(); - $class = \get_class($object); + $class = $object::class; $objectMapping = $this->getObjectMapping($class); - $errors = []; - foreach ($this->validateClass( + $errors = $this->validateClass( $context, $objectMapping->getValidationClassMapping($path), $path, $object - ) as $classError) { - $errors[] = $classError; - } + ); foreach ($objectMapping->getValidationPropertyMappings($path) as $propertyMapping) { foreach ($this->validateProperty($context, $propertyMapping, $path, $object) as $propertyError) { @@ -157,10 +151,7 @@ private function isWithinGroup( return false; } - /** - * @param int|string $name - */ - private function getSubPathByName(string $path, $name): string + private function getSubPathByName(string $path, int|string $name): string { return '' === $path ? (string) $name : $path.'.'.$name; } @@ -174,7 +165,7 @@ private function logConstraint(string $path, ConstraintInterface $constraint): v { $this->logger->debug('validate: path {path}, constraint {constraint}', [ 'path' => $path, - 'constraint' => \get_class($constraint), + 'constraint' => $constraint::class, ]); } @@ -182,7 +173,7 @@ private function logError(string $path, ConstraintInterface $constraint, ErrorIn { $this->logger->notice('validate: path {path}, constraint {constraint}, error {error}', [ 'path' => $path, - 'constraint' => \get_class($constraint), + 'constraint' => $constraint::class, 'error' => [ 'key' => $error->getKey(), 'arguments' => $error->getArguments(), diff --git a/src/ValidatorContext.php b/src/ValidatorContext.php index 9d4730a..fefadd5 100644 --- a/src/ValidatorContext.php +++ b/src/ValidatorContext.php @@ -6,17 +6,11 @@ final class ValidatorContext implements ValidatorContextInterface { - /** - * @var array - */ - private array $groups = []; - /** * @param array $groups */ - public function __construct(array $groups = []) + public function __construct(private array $groups = []) { - $this->groups = $groups; } /** diff --git a/tests/Integration/ValidatorTest.php b/tests/Integration/ValidatorTest.php index 86627dd..64471fe 100644 --- a/tests/Integration/ValidatorTest.php +++ b/tests/Integration/ValidatorTest.php @@ -61,10 +61,7 @@ public function getNumeric(): int return $this->numeric; } - /** - * @param int|string $numeric - */ - public function setNumeric($numeric): self + public function setNumeric(int|string $numeric): self { $this->numeric = $numeric; @@ -103,16 +100,13 @@ public function setAll(\ArrayIterator $all): self $validatorObjectMappingRegistry = new ValidationMappingProviderRegistry([ new class($object) implements ValidationMappingProviderInterface { - private object $object; - - public function __construct(object $object) + public function __construct(private object $object) { - $this->object = $object; } public function getClass(): string { - return \get_class($this->object); + return $this->object::class; } public function getValidationClassMapping(string $path): ValidationClassMappingInterface diff --git a/tests/Unit/Constraint/CoordinateArrayConstraintTest.php b/tests/Unit/Constraint/CoordinateArrayConstraintTest.php index 8458563..ec11d0c 100644 --- a/tests/Unit/Constraint/CoordinateArrayConstraintTest.php +++ b/tests/Unit/Constraint/CoordinateArrayConstraintTest.php @@ -72,7 +72,7 @@ public function testWithInvalidCoordinateArray(array $coordinate): void { $constraint = new CoordinateArrayConstraint(); - $error = new Error('coordinatearray', 'constraint.coordinatearray.invalidvalue', ['value' => json_encode($coordinate)]); + $error = new Error('coordinatearray', 'constraint.coordinatearray.invalidvalue', ['value' => json_encode($coordinate, JSON_THROW_ON_ERROR)]); self::assertEquals([$error], $constraint->validate('coordinatearray', $coordinate, $this->getContext())); } diff --git a/tests/Unit/Constraint/Doctrine/UniqueConstraintTest.php b/tests/Unit/Constraint/Doctrine/UniqueConstraintTest.php index ccb0c1f..eadc235 100644 --- a/tests/Unit/Constraint/Doctrine/UniqueConstraintTest.php +++ b/tests/Unit/Constraint/Doctrine/UniqueConstraintTest.php @@ -41,7 +41,7 @@ public function testWithNewModel(): void private string $name = 'name'; }; - $modelClass = \get_class($model); + $modelClass = $model::class; /** @var MockObject|ObjectRepository $repository */ $repository = $this->getMockByCalls(ObjectRepository::class, [ @@ -65,7 +65,7 @@ public function testWithSameExistingModel(): void private string $name = 'name'; }; - $modelClass = \get_class($model); + $modelClass = $model::class; /** @var MockObject|ObjectRepository $repository */ $repository = $this->getMockByCalls(ObjectRepository::class, [ @@ -102,7 +102,7 @@ public function testWithSameDifferentModel(): void private string $name = 'name'; }; - $modelClass = \get_class($model); + $modelClass = $model::class; /** @var MockObject|ObjectRepository $repository */ $repository = $this->getMockByCalls(ObjectRepository::class, [ diff --git a/tests/Unit/Constraint/SortConstraintTest.php b/tests/Unit/Constraint/SortConstraintTest.php index 36d0080..a4d37c0 100644 --- a/tests/Unit/Constraint/SortConstraintTest.php +++ b/tests/Unit/Constraint/SortConstraintTest.php @@ -97,7 +97,7 @@ public function testValidateWithUnsupportedFieldAndUnsupportedOrderType(): void new Error( 'path.unknown2', 'constraint.sort.order.invalidtype', - ['field' => 'unknown2', 'type' => 'integer'] + ['field' => 'unknown2', 'type' => 'int'] ), ], $constraint->validate('path', ['name' => 'asc', 'unknown1' => new \stdClass(), 'unknown2' => 1], $context) diff --git a/tests/Unit/Mapping/ValidationMappingProviderRegistryTest.php b/tests/Unit/Mapping/ValidationMappingProviderRegistryTest.php index 7ab56dd..03c2c48 100644 --- a/tests/Unit/Mapping/ValidationMappingProviderRegistryTest.php +++ b/tests/Unit/Mapping/ValidationMappingProviderRegistryTest.php @@ -26,7 +26,7 @@ public function testGetObjectMapping(): void $this->getValidationObjectMapping(), ]); - $mapping = $registry->provideMapping(\get_class($object)); + $mapping = $registry->provideMapping($object::class); self::assertInstanceOf(ValidationMappingProviderInterface::class, $mapping); } @@ -38,7 +38,7 @@ public function testGetMissingObjectMapping(): void $registry = new ValidationMappingProviderRegistry([]); - $registry->provideMapping(\get_class(new \stdClass())); + $registry->provideMapping((new \stdClass())::class); } public function testGetObjectMappingFromDoctrineProxy(): void @@ -49,7 +49,7 @@ public function testGetObjectMappingFromDoctrineProxy(): void $this->getValidationProxyObjectMapping(), ]); - $mapping = $registry->provideMapping(\get_class($object)); + $mapping = $registry->provideMapping($object::class); self::assertInstanceOf(ValidationMappingProviderInterface::class, $mapping); } @@ -65,7 +65,7 @@ private function getValidationObjectMapping(): ValidationMappingProviderInterfac $object = $this->getObject(); $objectMapping->expects(self::any())->method('getClass')->willReturnCallback( - static fn () => \get_class($object) + static fn () => $object::class ); return $objectMapping; diff --git a/tests/Unit/ValidatorTest.php b/tests/Unit/ValidatorTest.php index c3b93f0..a426e64 100644 --- a/tests/Unit/ValidatorTest.php +++ b/tests/Unit/ValidatorTest.php @@ -32,7 +32,7 @@ final class ValidatorTest extends TestCase public function testValidateMissingMappingExpectValidatorLogicException(): void { $model = $this->getModel(); - $class = \get_class($model); + $class = $model::class; $exceptionMessage = sprintf('There is no mapping for class: "%s"', $class); @@ -58,7 +58,7 @@ public function testValidateMissingMappingExpectValidatorLogicException(): void public function testValidateWithoutClassMappingAndWithoutPropertyMapping(): void { $model = $this->getModel(); - $class = \get_class($model); + $class = $model::class; /** @var ValidationMappingProviderInterface $mapping */ $mapping = $this->getMockByCalls(ValidationMappingProviderInterface::class, [ @@ -82,7 +82,7 @@ public function testValidateWithoutClassMappingAndWithoutPropertyMapping(): void public function testValidateWithClassMappingAndWithPropertyMapping(): void { $model = $this->getModel(); - $class = \get_class($model); + $class = $model::class; /** @var ErrorInterface $classError */ $classError = $this->getMockByCalls(ErrorInterface::class, [ @@ -153,26 +153,26 @@ public function testValidateWithClassMappingAndWithPropertyMapping(): void Call::create('info')->with('validate: path {path}', ['path' => '']), Call::create('debug')->with( 'validate: path {path}, constraint {constraint}', - ['path' => '', 'constraint' => \get_class($classConstraint)] + ['path' => '', 'constraint' => $classConstraint::class] ), Call::create('notice')->with( 'validate: path {path}, constraint {constraint}, error {error}', [ 'path' => '', - 'constraint' => \get_class($classConstraint), + 'constraint' => $classConstraint::class, 'error' => ['key' => 'key', 'arguments' => ['key' => 'value']], ] ), Call::create('info')->with('validate: path {path}', ['path' => 'name']), Call::create('debug')->with( 'validate: path {path}, constraint {constraint}', - ['path' => 'name', 'constraint' => \get_class($propertyConstraint)] + ['path' => 'name', 'constraint' => $propertyConstraint::class] ), Call::create('notice')->with( 'validate: path {path}, constraint {constraint}, error {error}', [ 'path' => 'name', - 'constraint' => \get_class($propertyConstraint), + 'constraint' => $propertyConstraint::class, 'error' => ['key' => 'key', 'arguments' => ['key' => 'value']], ] ), @@ -191,7 +191,7 @@ public function testValidateWithClassMappingAndWithPropertyMapping(): void public function testValidateWithClassMappingAndWithPropertyMappingWithoutUsedGroup(): void { $model = $this->getModel(); - $class = \get_class($model); + $class = $model::class; /** @var ValidationClassMappingInterface $classMapping */ $classMapping = $this->getMockByCalls(ValidationClassMappingInterface::class, [ @@ -230,7 +230,7 @@ public function testValidateWithClassMappingAndWithPropertyMappingWithoutUsedGro public function testValidateWithClassMappingAndWithPropertyMappingWithUsedGroup(): void { $model = $this->getModel(); - $class = \get_class($model); + $class = $model::class; /** @var ErrorInterface $classError */ $classError = $this->getMockByCalls(ErrorInterface::class, [ @@ -303,26 +303,26 @@ public function testValidateWithClassMappingAndWithPropertyMappingWithUsedGroup( Call::create('info')->with('validate: path {path}', ['path' => '']), Call::create('debug')->with( 'validate: path {path}, constraint {constraint}', - ['path' => '', 'constraint' => \get_class($classConstraint)] + ['path' => '', 'constraint' => $classConstraint::class] ), Call::create('notice')->with( 'validate: path {path}, constraint {constraint}, error {error}', [ 'path' => '', - 'constraint' => \get_class($classConstraint), + 'constraint' => $classConstraint::class, 'error' => ['key' => 'key', 'arguments' => ['key' => 'value']], ] ), Call::create('info')->with('validate: path {path}', ['path' => 'name']), Call::create('debug')->with( 'validate: path {path}, constraint {constraint}', - ['path' => 'name', 'constraint' => \get_class($propertyConstraint)] + ['path' => 'name', 'constraint' => $propertyConstraint::class] ), Call::create('notice')->with( 'validate: path {path}, constraint {constraint}, error {error}', [ 'path' => 'name', - 'constraint' => \get_class($propertyConstraint), + 'constraint' => $propertyConstraint::class, 'error' => ['key' => 'key', 'arguments' => ['key' => 'value']], ] ),