From e3328bc9dab70f6a05016650b46220059fe4dbf2 Mon Sep 17 00:00:00 2001 From: Jordi Date: Sun, 9 Aug 2020 18:09:10 +0200 Subject: [PATCH] Narrow Form and Mapper API --- src/Admin/AbstractAdmin.php | 2 - src/Admin/AdminInterface.php | 7 +- .../FieldDescriptionRegistryInterface.php | 2 +- src/Datagrid/DatagridMapper.php | 4 +- src/Datagrid/ListMapper.php | 4 +- src/Form/ChoiceList/ModelChoiceLoader.php | 70 +++++++++++-------- .../ArrayToModelTransformer.php | 5 +- .../ModelToIdPropertyTransformer.php | 28 +++----- .../DataTransformer/ModelToIdTransformer.php | 5 +- .../ModelsToArrayTransformer.php | 11 ++- .../EventListener/MergeCollectionListener.php | 2 +- src/Form/Extension/ChoiceTypeExtension.php | 13 ++-- .../Field/Type/FormTypeFieldExtension.php | 32 +++++---- .../MopaCompatibilityTypeFieldExtension.php | 13 ++-- src/Form/FormMapper.php | 15 ++-- src/Form/Type/AclMatrixType.php | 2 +- src/Form/Type/AdminType.php | 10 ++- src/Form/Type/ChoiceFieldMaskType.php | 4 +- src/Form/Type/CollectionType.php | 4 +- src/Form/Type/Filter/ChoiceType.php | 2 +- src/Form/Type/Filter/DateRangeType.php | 2 +- src/Form/Type/Filter/DateTimeRangeType.php | 2 +- src/Form/Type/Filter/DateTimeType.php | 2 +- src/Form/Type/Filter/DateType.php | 2 +- src/Form/Type/Filter/DefaultType.php | 2 +- src/Form/Type/Filter/NumberType.php | 2 +- src/Form/Type/ModelAutocompleteType.php | 2 +- src/Form/Type/ModelHiddenType.php | 4 +- src/Form/Type/ModelListType.php | 4 +- src/Form/Type/ModelReferenceType.php | 4 +- src/Form/Type/ModelType.php | 4 +- .../Type/Operator/ContainsOperatorType.php | 6 +- src/Form/Type/Operator/DateOperatorType.php | 6 +- .../Type/Operator/DateRangeOperatorType.php | 6 +- src/Form/Type/Operator/EqualOperatorType.php | 6 +- src/Form/Type/Operator/NumberOperatorType.php | 8 +-- src/Form/Type/Operator/StringOperatorType.php | 6 +- src/Mapper/BaseGroupedMapper.php | 19 ++++- src/Mapper/BaseMapper.php | 9 +-- src/Show/ShowMapper.php | 5 +- .../ModelToIdPropertyTransformerTest.php | 18 ----- tests/Form/Type/ModelTypeTest.php | 8 +-- 42 files changed, 182 insertions(+), 180 deletions(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index dad781e3d9a..74f7fc5842e 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -1747,8 +1747,6 @@ public function isCurrentChild(): bool /** * Returns the current child admin instance. - * - * @return AdminInterface|null the current child admin instance */ public function getCurrentChildAdmin(): ?AdminInterface { diff --git a/src/Admin/AdminInterface.php b/src/Admin/AdminInterface.php index 87c29f44963..98c568e6fcd 100644 --- a/src/Admin/AdminInterface.php +++ b/src/Admin/AdminInterface.php @@ -135,12 +135,7 @@ public function getSecurityInformation(): array; public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription): void; - /** - * Get parent field description. - * - * @return FieldDescriptionInterface The parent field description - */ - public function getParentFieldDescription(): ?FieldDescriptionInterface; + public function getParentFieldDescription(): FieldDescriptionInterface; /** * Returns true if the Admin is linked to a parent FieldDescription. diff --git a/src/Admin/FieldDescriptionRegistryInterface.php b/src/Admin/FieldDescriptionRegistryInterface.php index ac06f624347..706d1efea75 100644 --- a/src/Admin/FieldDescriptionRegistryInterface.php +++ b/src/Admin/FieldDescriptionRegistryInterface.php @@ -23,7 +23,7 @@ interface FieldDescriptionRegistryInterface /** * Return FormFieldDescription. */ - public function getFormFieldDescription(string $name): ?FieldDescriptionInterface; + public function getFormFieldDescription(string $name): FieldDescriptionInterface; public function hasFormFieldDescription(string $name): bool; diff --git a/src/Datagrid/DatagridMapper.php b/src/Datagrid/DatagridMapper.php index f7497e64661..aa7fba84022 100644 --- a/src/Datagrid/DatagridMapper.php +++ b/src/Datagrid/DatagridMapper.php @@ -118,7 +118,7 @@ final public function keys(): array return array_keys($this->datagrid->getFilters()); } - public function remove(string $key): self + public function remove(string $key): BaseMapper { $this->admin->removeFilterFieldDescription($key); $this->datagrid->removeFilter($key); @@ -126,7 +126,7 @@ public function remove(string $key): self return $this; } - public function reorder(array $keys): self + public function reorder(array $keys): BaseMapper { $this->datagrid->reorderFilters($keys); diff --git a/src/Datagrid/ListMapper.php b/src/Datagrid/ListMapper.php index 2174de62137..1c358d6699e 100644 --- a/src/Datagrid/ListMapper.php +++ b/src/Datagrid/ListMapper.php @@ -156,7 +156,7 @@ public function has(string $key): bool return $this->list->has($key); } - public function remove(string $key): self + public function remove(string $key): BaseMapper { $this->admin->removeListFieldDescription($key); $this->list->remove($key); @@ -169,7 +169,7 @@ final public function keys(): array return array_keys($this->list->getElements()); } - public function reorder(array $keys): self + public function reorder(array $keys): BaseMapper { $this->list->reorder($keys); diff --git a/src/Form/ChoiceList/ModelChoiceLoader.php b/src/Form/ChoiceList/ModelChoiceLoader.php index e460b6ddda9..80895d776cb 100644 --- a/src/Form/ChoiceList/ModelChoiceLoader.php +++ b/src/Form/ChoiceList/ModelChoiceLoader.php @@ -17,21 +17,24 @@ use Sonata\AdminBundle\Model\ModelManagerInterface; use Sonata\Doctrine\Adapter\AdapterInterface; use Symfony\Component\Form\ChoiceList\ArrayChoiceList; +use Symfony\Component\Form\ChoiceList\ChoiceListInterface; use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; use Symfony\Component\Form\Exception\RuntimeException; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; -use Symfony\Component\PropertyAccess\PropertyPath; /** * @author Thomas Rabaix */ final class ModelChoiceLoader implements ChoiceLoaderInterface { + /** + * @var string[] + */ public $identifier; /** - * @var \Sonata\AdminBundle\Model\ModelManagerInterface + * @var ModelManagerInterface */ private $modelManager; @@ -40,36 +43,41 @@ final class ModelChoiceLoader implements ChoiceLoaderInterface */ private $class; + /** + * @var string|null + */ private $property; + /** + * @var mixed + */ private $query; - private $choices; - /** - * @var PropertyPath + * @var object[] */ - private $propertyPath; + private $choices; /** * @var PropertyAccessorInterface */ private $propertyAccessor; + /** + * @var ChoiceListInterface|null + */ private $choiceList; /** - * @param string $class - * @param string|null $property - * @param mixed|null $query - * @param array $choices + * @param mixed $query + * @param object[] $choices */ public function __construct( ModelManagerInterface $modelManager, - $class, - $property = null, + string $class, + ?string $property = null, $query = null, - $choices = [], + array $choices = [], ?PropertyAccessorInterface $propertyAccessor = null ) { $this->modelManager = $modelManager; @@ -77,33 +85,27 @@ public function __construct( $this->property = $property; $this->query = $query; $this->choices = $choices; + $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); $this->identifier = $this->modelManager->getIdentifierFieldNames($this->class); - - // The property option defines, which property (path) is used for - // displaying entities as strings - if ($property) { - $this->propertyPath = new PropertyPath($property); - $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor(); - } } - public function loadChoiceList($value = null) + public function loadChoiceList($value = null): ChoiceListInterface { if (!$this->choiceList) { if ($this->query) { $entities = $this->modelManager->executeQuery($this->query); - } elseif (\is_array($this->choices) && \count($this->choices) > 0) { + } elseif (\count($this->choices) > 0) { $entities = $this->choices; } else { $entities = $this->modelManager->findBy($this->class); } $choices = []; - foreach ($entities as $key => $model) { - if ($this->propertyPath) { + foreach ($entities as $model) { + if (null !== $this->property) { // If the property option was given, use it - $valueObject = $this->propertyAccessor->getValue($model, $this->propertyPath); + $valueObject = $this->propertyAccessor->getValue($model, $this->property); } else { // Otherwise expect a __toString() method in the entity try { @@ -143,20 +145,30 @@ public function loadChoiceList($value = null) return $this->choiceList; } - public function loadChoicesForValues(array $values, $value = null) + /** + * @param string[] $values + * + * @return object[] + */ + public function loadChoicesForValues(array $values, $value = null): array { return $this->loadChoiceList($value)->getChoicesForValues($values); } - public function loadValuesForChoices(array $choices, $value = null) + /** + * @param object[] $choices + * + * @return string[] + */ + public function loadValuesForChoices(array $choices, $value = null): array { return $this->loadChoiceList($value)->getValuesForChoices($choices); } /** - * @param object $model + * @return mixed[] */ - private function getIdentifierValues($model): array + private function getIdentifierValues(object $model): array { try { return $this->modelManager->getIdentifierValues($model); diff --git a/src/Form/DataTransformer/ArrayToModelTransformer.php b/src/Form/DataTransformer/ArrayToModelTransformer.php index e385e08fee3..7aea05fecb5 100644 --- a/src/Form/DataTransformer/ArrayToModelTransformer.php +++ b/src/Form/DataTransformer/ArrayToModelTransformer.php @@ -31,10 +31,7 @@ final class ArrayToModelTransformer implements DataTransformerInterface */ private $className; - /** - * @param string $className - */ - public function __construct(ModelManagerInterface $modelManager, $className) + public function __construct(ModelManagerInterface $modelManager, string $className) { $this->modelManager = $modelManager; $this->className = $className; diff --git a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php index a41ebf3957e..44fcbd211bf 100644 --- a/src/Form/DataTransformer/ModelToIdPropertyTransformer.php +++ b/src/Form/DataTransformer/ModelToIdPropertyTransformer.php @@ -49,18 +49,12 @@ final class ModelToIdPropertyTransformer implements DataTransformerInterface */ private $toStringCallback; - /** - * @param string $className - * @param string $property - * @param bool $multiple - * @param callable|null $toStringCallback - */ public function __construct( ModelManagerInterface $modelManager, - $className, - $property, - $multiple = false, - $toStringCallback = null + string $className, + string $property, + bool $multiple = false, + ?callable $toStringCallback = null ) { $this->modelManager = $modelManager; $this->className = $className; @@ -69,6 +63,9 @@ public function __construct( $this->toStringCallback = $toStringCallback; } + /** + * @throws \UnexpectedValueException + */ public function reverseTransform($value) { $collection = $this->modelManager->getModelCollectionInstance($this->className); @@ -100,6 +97,9 @@ public function reverseTransform($value) return $collection; } + /** + * @throws \InvalidArgumentException + */ public function transform($entityOrCollection) { $result = []; @@ -140,7 +140,7 @@ public function transform($entityOrCollection) } } - if (empty($this->property)) { + if ('' === $this->property) { throw new \RuntimeException('Please define "property" parameter.'); } @@ -148,12 +148,6 @@ public function transform($entityOrCollection) $id = current($this->modelManager->getIdentifierValues($model)); if (null !== $this->toStringCallback) { - if (!\is_callable($this->toStringCallback)) { - throw new \RuntimeException( - 'Callback in "to_string_callback" option doesn`t contain callable function.' - ); - } - $label = ($this->toStringCallback)($model, $this->property); } else { try { diff --git a/src/Form/DataTransformer/ModelToIdTransformer.php b/src/Form/DataTransformer/ModelToIdTransformer.php index bab4e189674..b3c29ffb8d7 100644 --- a/src/Form/DataTransformer/ModelToIdTransformer.php +++ b/src/Form/DataTransformer/ModelToIdTransformer.php @@ -31,10 +31,7 @@ final class ModelToIdTransformer implements DataTransformerInterface */ private $className; - /** - * @param string $className - */ - public function __construct(ModelManagerInterface $modelManager, $className) + public function __construct(ModelManagerInterface $modelManager, string $className) { $this->modelManager = $modelManager; $this->className = $className; diff --git a/src/Form/DataTransformer/ModelsToArrayTransformer.php b/src/Form/DataTransformer/ModelsToArrayTransformer.php index d899cde5bda..bc2fed1515f 100644 --- a/src/Form/DataTransformer/ModelsToArrayTransformer.php +++ b/src/Form/DataTransformer/ModelsToArrayTransformer.php @@ -17,7 +17,6 @@ use Sonata\AdminBundle\Model\ModelManagerInterface; use Sonata\Doctrine\Adapter\AdapterInterface; use Symfony\Component\Form\DataTransformerInterface; -use Symfony\Component\Form\Exception\RuntimeException; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\UnexpectedTypeException; @@ -36,9 +35,6 @@ final class ModelsToArrayTransformer implements DataTransformerInterface */ private $class; - /** - * @throws RuntimeException - */ public function __construct(ModelManagerInterface $modelManager, string $class) { $this->modelManager = $modelManager; @@ -61,6 +57,9 @@ public function transform($collection) return $array; } + /** + * @throws UnexpectedTypeException + */ public function reverseTransform($keys) { if (!\is_array($keys)) { @@ -90,9 +89,9 @@ public function reverseTransform($keys) } /** - * @param object $model + * @return mixed[] */ - private function getIdentifierValues($model): array + private function getIdentifierValues(object $model): array { try { return $this->modelManager->getIdentifierValues($model); diff --git a/src/Form/EventListener/MergeCollectionListener.php b/src/Form/EventListener/MergeCollectionListener.php index 1055b0dcc11..d8de7f15c21 100644 --- a/src/Form/EventListener/MergeCollectionListener.php +++ b/src/Form/EventListener/MergeCollectionListener.php @@ -33,7 +33,7 @@ public function __construct(ModelManagerInterface $modelManager) $this->modelManager = $modelManager; } - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { return [ FormEvents::SUBMIT => ['onBind', 10], diff --git a/src/Form/Extension/ChoiceTypeExtension.php b/src/Form/Extension/ChoiceTypeExtension.php index 2d8f24676db..4f2c98dd9fa 100644 --- a/src/Form/Extension/ChoiceTypeExtension.php +++ b/src/Form/Extension/ChoiceTypeExtension.php @@ -31,17 +31,18 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setDefined($optionalOptions); } + /** + * @param array $options + */ public function buildView(FormView $view, FormInterface $form, array $options): void { $view->vars['sortable'] = \array_key_exists('sortable', $options) && $options['sortable']; } - public function getExtendedType() - { - return ChoiceType::class; - } - - public static function getExtendedTypes() + /** + * @return string[] + */ + public static function getExtendedTypes(): array { return [ChoiceType::class]; } diff --git a/src/Form/Extension/Field/Type/FormTypeFieldExtension.php b/src/Form/Extension/Field/Type/FormTypeFieldExtension.php index 1cf12767a9a..63ae625ee24 100644 --- a/src/Form/Extension/Field/Type/FormTypeFieldExtension.php +++ b/src/Form/Extension/Field/Type/FormTypeFieldExtension.php @@ -29,21 +29,28 @@ final class FormTypeFieldExtension extends AbstractTypeExtension { /** - * @var array + * @var array */ private $defaultClasses = []; /** - * @var array + * @var array */ private $options = []; + /** + * @param array $defaultClasses + * @param array $options + */ public function __construct(array $defaultClasses, array $options) { $this->defaultClasses = $defaultClasses; $this->options = $options; } + /** + * @param array $options + */ public function buildForm(FormBuilderInterface $builder, array $options): void { $sonataAdmin = [ @@ -76,6 +83,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void $builder->setAttribute('sonata_admin', $sonataAdmin); } + /** + * @param array $options + */ public function buildView(FormView $view, FormInterface $form, array $options): void { $sonataAdmin = $form->getConfig()->getAttribute('sonata_admin'); @@ -148,12 +158,10 @@ public function buildView(FormView $view, FormInterface $form, array $options): $view->vars['sonata_admin'] = $sonataAdmin; } - public function getExtendedType() - { - return FormType::class; - } - - public static function getExtendedTypes() + /** + * @return string[] + */ + public static function getExtendedTypes(): array { return [FormType::class]; } @@ -174,18 +182,12 @@ public function configureOptions(OptionsResolver $resolver): void * return the value related to FieldDescription, if the associated object does no * exists => a temporary one is created. * - * @param object $object - * * @return mixed */ - public function getValueFromFieldDescription($object, FieldDescriptionInterface $fieldDescription) + public function getValueFromFieldDescription(object $object, FieldDescriptionInterface $fieldDescription) { $value = null; - if (!$object) { - return $value; - } - try { $value = $fieldDescription->getValue($object); } catch (NoValueException $e) { diff --git a/src/Form/Extension/Field/Type/MopaCompatibilityTypeFieldExtension.php b/src/Form/Extension/Field/Type/MopaCompatibilityTypeFieldExtension.php index f3e2c010ca5..99b77904b01 100644 --- a/src/Form/Extension/Field/Type/MopaCompatibilityTypeFieldExtension.php +++ b/src/Form/Extension/Field/Type/MopaCompatibilityTypeFieldExtension.php @@ -36,6 +36,9 @@ public function configureOptions(OptionsResolver $resolver): void ]); } + /** + * @param array $options + */ public function buildView(FormView $view, FormInterface $form, array $options): void { $view->vars['horizontal_label_class'] = $options['horizontal_label_class']; @@ -43,12 +46,10 @@ public function buildView(FormView $view, FormInterface $form, array $options): $view->vars['horizontal_input_wrapper_class'] = $options['horizontal_input_wrapper_class']; } - public function getExtendedType() - { - return FormType::class; - } - - public static function getExtendedTypes() + /** + * @return string[] + */ + public static function getExtendedTypes(): array { return [FormType::class]; } diff --git a/src/Form/FormMapper.php b/src/Form/FormMapper.php index f713857f4f6..c98bb22c08c 100644 --- a/src/Form/FormMapper.php +++ b/src/Form/FormMapper.php @@ -17,6 +17,7 @@ use Sonata\AdminBundle\Builder\FormContractorInterface; use Sonata\AdminBundle\Form\Type\CollectionType; use Sonata\AdminBundle\Mapper\BaseGroupedMapper; +use Sonata\AdminBundle\Mapper\BaseMapper; use Symfony\Component\Form\Extension\Core\Type\CollectionType as SymfonyCollectionType; use Symfony\Component\Form\FormBuilderInterface; @@ -48,7 +49,7 @@ public function __construct( $this->formBuilder = $formBuilder; } - public function reorder(array $keys): self + public function reorder(array $keys): BaseMapper { $this->admin->reorderFormGroup($this->getCurrentGroupName(), $keys); @@ -57,6 +58,8 @@ public function reorder(array $keys): self /** * @param FormBuilderInterface|string $name + * @param array $options + * @param array $fieldDescriptionOptions */ public function add($name, ?string $type = null, array $options = [], array $fieldDescriptionOptions = []): self { @@ -157,12 +160,15 @@ public function has(string $key): bool return $this->formBuilder->has($key); } + /** + * @return string[] + */ final public function keys(): array { return array_keys($this->formBuilder->all()); } - public function remove(string $key): self + public function remove(string $key): BaseMapper { $key = $this->sanitizeFieldName($key); $this->admin->removeFormFieldDescription($key); @@ -173,8 +179,6 @@ public function remove(string $key): self } /** - * Removes a group. - * * @param string $group The group to delete * @param string $tab The tab the group belongs to, defaults to 'default' * @param bool $deleteEmptyTab Whether or not the Tab should be deleted, when the deleted group leaves the tab empty after deletion @@ -216,6 +220,9 @@ public function getFormBuilder(): FormBuilderInterface return $this->formBuilder; } + /** + * @param array $options + */ public function create(string $name, ?string $type = null, array $options = []): FormBuilderInterface { return $this->formBuilder->create($name, $type, $options); diff --git a/src/Form/Type/AclMatrixType.php b/src/Form/Type/AclMatrixType.php index 4685a77dcb6..abcb7eea695 100644 --- a/src/Form/Type/AclMatrixType.php +++ b/src/Form/Type/AclMatrixType.php @@ -49,7 +49,7 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setAllowedTypes('acl_value', ['string', UserInterface::class]); } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_acl_matrix'; } diff --git a/src/Form/Type/AdminType.php b/src/Form/Type/AdminType.php index 7ce1fa7de15..22af8b124f5 100644 --- a/src/Form/Type/AdminType.php +++ b/src/Form/Type/AdminType.php @@ -32,6 +32,9 @@ */ final class AdminType extends AbstractType { + /** + * @param array $options + */ public function buildForm(FormBuilderInterface $builder, array $options): void { $admin = clone $this->getAdmin($options); @@ -125,12 +128,14 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_admin'; } /** + * @param array $options + * * @throws \RuntimeException */ private function getFieldDescription(array $options): FieldDescriptionInterface @@ -142,6 +147,9 @@ private function getFieldDescription(array $options): FieldDescriptionInterface return $options['sonata_field_description']; } + /** + * @param array $options + */ private function getAdmin(array $options): AdminInterface { return $this->getFieldDescription($options)->getAssociationAdmin(); diff --git a/src/Form/Type/ChoiceFieldMaskType.php b/src/Form/Type/ChoiceFieldMaskType.php index cab05a50249..aa9be418e1c 100644 --- a/src/Form/Type/ChoiceFieldMaskType.php +++ b/src/Form/Type/ChoiceFieldMaskType.php @@ -57,12 +57,12 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setAllowedTypes('map', 'array'); } - public function getParent() + public function getParent(): ?string { return ChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_choice_field_mask'; } diff --git a/src/Form/Type/CollectionType.php b/src/Form/Type/CollectionType.php index 7f326ec35d3..c485ca6015e 100644 --- a/src/Form/Type/CollectionType.php +++ b/src/Form/Type/CollectionType.php @@ -24,12 +24,12 @@ */ final class CollectionType extends AbstractType { - public function getParent() + public function getParent(): ?string { return SymfonyCollectionType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_native_collection'; } diff --git a/src/Form/Type/Filter/ChoiceType.php b/src/Form/Type/Filter/ChoiceType.php index 3a18bf8345e..8207ef04686 100644 --- a/src/Form/Type/Filter/ChoiceType.php +++ b/src/Form/Type/Filter/ChoiceType.php @@ -24,7 +24,7 @@ */ final class ChoiceType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_choice'; } diff --git a/src/Form/Type/Filter/DateRangeType.php b/src/Form/Type/Filter/DateRangeType.php index 377d8bb0455..bb56920850a 100644 --- a/src/Form/Type/Filter/DateRangeType.php +++ b/src/Form/Type/Filter/DateRangeType.php @@ -24,7 +24,7 @@ */ final class DateRangeType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_date_range'; } diff --git a/src/Form/Type/Filter/DateTimeRangeType.php b/src/Form/Type/Filter/DateTimeRangeType.php index 5c5cd4bd840..583d5e97a3d 100644 --- a/src/Form/Type/Filter/DateTimeRangeType.php +++ b/src/Form/Type/Filter/DateTimeRangeType.php @@ -24,7 +24,7 @@ */ final class DateTimeRangeType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_datetime_range'; } diff --git a/src/Form/Type/Filter/DateTimeType.php b/src/Form/Type/Filter/DateTimeType.php index 2424d4259de..a8ff41335d5 100644 --- a/src/Form/Type/Filter/DateTimeType.php +++ b/src/Form/Type/Filter/DateTimeType.php @@ -24,7 +24,7 @@ */ final class DateTimeType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_datetime'; } diff --git a/src/Form/Type/Filter/DateType.php b/src/Form/Type/Filter/DateType.php index 8629634474f..d1eef2ce1d6 100644 --- a/src/Form/Type/Filter/DateType.php +++ b/src/Form/Type/Filter/DateType.php @@ -24,7 +24,7 @@ */ final class DateType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_date'; } diff --git a/src/Form/Type/Filter/DefaultType.php b/src/Form/Type/Filter/DefaultType.php index beeda949837..1a5831eaed2 100644 --- a/src/Form/Type/Filter/DefaultType.php +++ b/src/Form/Type/Filter/DefaultType.php @@ -24,7 +24,7 @@ */ final class DefaultType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_default'; } diff --git a/src/Form/Type/Filter/NumberType.php b/src/Form/Type/Filter/NumberType.php index bf83d9ecf0a..4c8435e085c 100644 --- a/src/Form/Type/Filter/NumberType.php +++ b/src/Form/Type/Filter/NumberType.php @@ -24,7 +24,7 @@ */ final class NumberType extends AbstractType { - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_filter_number'; } diff --git a/src/Form/Type/ModelAutocompleteType.php b/src/Form/Type/ModelAutocompleteType.php index c247f592b65..7feaeff585e 100644 --- a/src/Form/Type/ModelAutocompleteType.php +++ b/src/Form/Type/ModelAutocompleteType.php @@ -147,7 +147,7 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setRequired(['property']); } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_model_autocomplete'; } diff --git a/src/Form/Type/ModelHiddenType.php b/src/Form/Type/ModelHiddenType.php index 4a4e428ed60..770b8554d04 100644 --- a/src/Form/Type/ModelHiddenType.php +++ b/src/Form/Type/ModelHiddenType.php @@ -44,12 +44,12 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getParent() + public function getParent(): ?string { return HiddenType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_model_hidden'; } diff --git a/src/Form/Type/ModelListType.php b/src/Form/Type/ModelListType.php index 13ef4d288dc..093ba745821 100644 --- a/src/Form/Type/ModelListType.php +++ b/src/Form/Type/ModelListType.php @@ -76,12 +76,12 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getParent() + public function getParent(): ?string { return TextType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_model_list'; } diff --git a/src/Form/Type/ModelReferenceType.php b/src/Form/Type/ModelReferenceType.php index 33a467b58dd..35be2f2fcd1 100644 --- a/src/Form/Type/ModelReferenceType.php +++ b/src/Form/Type/ModelReferenceType.php @@ -38,12 +38,12 @@ public function configureOptions(OptionsResolver $resolver): void ]); } - public function getParent() + public function getParent(): ?string { return TextType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_model_reference'; } diff --git a/src/Form/Type/ModelType.php b/src/Form/Type/ModelType.php index ce5854695a7..d1ac42c6a99 100644 --- a/src/Form/Type/ModelType.php +++ b/src/Form/Type/ModelType.php @@ -125,12 +125,12 @@ public function configureOptions(OptionsResolver $resolver): void ])); } - public function getParent() + public function getParent(): ?string { return ChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_model'; } diff --git a/src/Form/Type/Operator/ContainsOperatorType.php b/src/Form/Type/Operator/ContainsOperatorType.php index 28f2ec6d30a..d1f8379bf3c 100644 --- a/src/Form/Type/Operator/ContainsOperatorType.php +++ b/src/Form/Type/Operator/ContainsOperatorType.php @@ -23,7 +23,7 @@ final class ContainsOperatorType extends AbstractType public const TYPE_NOT_CONTAINS = 2; public const TYPE_EQUAL = 3; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', @@ -35,12 +35,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_contains'; } diff --git a/src/Form/Type/Operator/DateOperatorType.php b/src/Form/Type/Operator/DateOperatorType.php index cf815f9b6a1..0a31a36bf79 100644 --- a/src/Form/Type/Operator/DateOperatorType.php +++ b/src/Form/Type/Operator/DateOperatorType.php @@ -27,7 +27,7 @@ final class DateOperatorType extends AbstractType public const TYPE_NULL = 6; public const TYPE_NOT_NULL = 7; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', @@ -43,12 +43,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_date'; } diff --git a/src/Form/Type/Operator/DateRangeOperatorType.php b/src/Form/Type/Operator/DateRangeOperatorType.php index 8e998107a95..ba6258aeb9d 100644 --- a/src/Form/Type/Operator/DateRangeOperatorType.php +++ b/src/Form/Type/Operator/DateRangeOperatorType.php @@ -22,7 +22,7 @@ final class DateRangeOperatorType extends AbstractType public const TYPE_BETWEEN = 1; public const TYPE_NOT_BETWEEN = 2; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', @@ -33,12 +33,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_date_range'; } diff --git a/src/Form/Type/Operator/EqualOperatorType.php b/src/Form/Type/Operator/EqualOperatorType.php index f4ab1de92ae..fcf57bfdda6 100644 --- a/src/Form/Type/Operator/EqualOperatorType.php +++ b/src/Form/Type/Operator/EqualOperatorType.php @@ -22,7 +22,7 @@ final class EqualOperatorType extends AbstractType public const TYPE_EQUAL = 1; public const TYPE_NOT_EQUAL = 2; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', @@ -33,12 +33,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_equal'; } diff --git a/src/Form/Type/Operator/NumberOperatorType.php b/src/Form/Type/Operator/NumberOperatorType.php index e5c654024a4..ab6a459f89a 100644 --- a/src/Form/Type/Operator/NumberOperatorType.php +++ b/src/Form/Type/Operator/NumberOperatorType.php @@ -25,9 +25,9 @@ final class NumberOperatorType extends AbstractType public const TYPE_LESS_EQUAL = 4; public const TYPE_LESS_THAN = 5; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { - $resolver->setDefaults($defaultOptions = [ + $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', 'choices' => [ 'label_type_equal' => self::TYPE_EQUAL, @@ -39,12 +39,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_number'; } diff --git a/src/Form/Type/Operator/StringOperatorType.php b/src/Form/Type/Operator/StringOperatorType.php index fe4e667304f..386143468bd 100644 --- a/src/Form/Type/Operator/StringOperatorType.php +++ b/src/Form/Type/Operator/StringOperatorType.php @@ -25,7 +25,7 @@ final class StringOperatorType extends AbstractType public const TYPE_STARTS_WITH = 4; public const TYPE_ENDS_WITH = 5; - public function configureOptions(OptionsResolver $resolver) + public function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ 'choice_translation_domain' => 'SonataAdminBundle', @@ -39,12 +39,12 @@ public function configureOptions(OptionsResolver $resolver) ]); } - public function getParent() + public function getParent(): ?string { return FormChoiceType::class; } - public function getBlockPrefix() + public function getBlockPrefix(): string { return 'sonata_type_operator_string'; } diff --git a/src/Mapper/BaseGroupedMapper.php b/src/Mapper/BaseGroupedMapper.php index 184eb2c29f5..7a658445ea9 100644 --- a/src/Mapper/BaseGroupedMapper.php +++ b/src/Mapper/BaseGroupedMapper.php @@ -38,6 +38,8 @@ abstract class BaseGroupedMapper extends BaseMapper /** * Add new group or tab (if parameter "tab=true" is available in options). * + * @param array $options + * * @throws \LogicException */ public function with(string $name, array $options = []): self @@ -193,6 +195,8 @@ public function ifEnd(): self /** * Add new tab. + * + * @param array $options */ public function tab(string $name, array $options = []): self { @@ -229,19 +233,28 @@ public function hasOpenTab(): bool return null !== $this->currentTab; } + /** + * @return array + */ abstract protected function getGroups(): array; + /** + * @return array + */ abstract protected function getTabs(): array; + /** + * @param array $groups + */ abstract protected function setGroups(array $groups): void; + /** + * @param array $tabs + */ abstract protected function setTabs(array $tabs): void; abstract protected function getName(): string; - /** - * Add the field name to the current group. - */ protected function addFieldToCurrentGroup(string $fieldName): array { // Note this line must happen before the next line. diff --git a/src/Mapper/BaseMapper.php b/src/Mapper/BaseMapper.php index e0ae920c1bc..88f1b5303e2 100644 --- a/src/Mapper/BaseMapper.php +++ b/src/Mapper/BaseMapper.php @@ -51,10 +51,7 @@ abstract public function get(string $key); abstract public function has(string $key): bool; - /** - * @return $this - */ - abstract public function remove(string $key); + abstract public function remove(string $key): self; /** * Returns configured keys. @@ -65,8 +62,6 @@ abstract public function keys(): array; /** * @param string[] $keys field names - * - * @return $this */ - abstract public function reorder(array $keys); + abstract public function reorder(array $keys): self; } diff --git a/src/Show/ShowMapper.php b/src/Show/ShowMapper.php index f8d40acdb1c..7a3d4d08b4e 100644 --- a/src/Show/ShowMapper.php +++ b/src/Show/ShowMapper.php @@ -18,6 +18,7 @@ use Sonata\AdminBundle\Admin\FieldDescriptionInterface; use Sonata\AdminBundle\Builder\ShowBuilderInterface; use Sonata\AdminBundle\Mapper\BaseGroupedMapper; +use Sonata\AdminBundle\Mapper\BaseMapper; /** * This class is used to simulate the Form API. @@ -106,7 +107,7 @@ public function has(string $key): bool return $this->list->has($key); } - public function remove(string $key): self + public function remove(string $key): BaseMapper { $this->admin->removeShowFieldDescription($key); $this->list->remove($key); @@ -159,7 +160,7 @@ final public function keys(): array return array_keys($this->list->getElements()); } - public function reorder(array $keys): self + public function reorder(array $keys): BaseMapper { $this->admin->reorderShowGroup($this->getCurrentGroupName(), $keys); diff --git a/tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php b/tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php index 455569c43ee..f42cd7eb57b 100644 --- a/tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php +++ b/tests/Form/DataTransformer/ModelToIdPropertyTransformerTest.php @@ -196,24 +196,6 @@ public function testTransformToStringCallback(): void $this->assertSame([123, '_labels' => ['bazz']], $transformer->transform($model)); } - public function testTransformToStringCallbackException(): void - { - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Callback in "to_string_callback" option doesn`t contain callable function.'); - - $model = new Foo(); - $model->setBar('example'); - $model->setBaz('bazz'); - - $this->modelManager->expects($this->once()) - ->method('getIdentifierValues') - ->willReturn([123]); - - $transformer = new ModelToIdPropertyTransformer($this->modelManager, Foo::class, 'bar', false, '987654'); - - $transformer->transform($model); - } - public function testTransformMultiple(): void { $entity1 = new Foo(); diff --git a/tests/Form/Type/ModelTypeTest.php b/tests/Form/Type/ModelTypeTest.php index de20aa9de5a..131c30380b8 100644 --- a/tests/Form/Type/ModelTypeTest.php +++ b/tests/Form/Type/ModelTypeTest.php @@ -39,14 +39,14 @@ public function testGetDefaultOptions(): void $this->type->configureOptions($optionResolver); - $options = $optionResolver->resolve(['model_manager' => $modelManager, 'choices' => []]); + $options = $optionResolver->resolve(['model_manager' => $modelManager, 'class' => 'class-name', 'choices' => []]); $this->assertFalse($options['compound']); $this->assertSame('choice', $options['template']); $this->assertFalse($options['multiple']); $this->assertFalse($options['expanded']); $this->assertInstanceOf(ModelManagerInterface::class, $options['model_manager']); - $this->assertNull($options['class']); + $this->assertSame('class-name', $options['class']); $this->assertNull($options['property']); $this->assertNull($options['query']); $this->assertCount(0, $options['choices']); @@ -68,14 +68,14 @@ public function testCompoundOption(bool $expectedCompound, bool $multiple, bool $this->type->configureOptions($optionResolver); - $options = $optionResolver->resolve(['model_manager' => $modelManager, 'choices' => [], 'multiple' => $multiple, 'expanded' => $expanded]); + $options = $optionResolver->resolve(['model_manager' => $modelManager, 'class' => 'class-name', 'choices' => [], 'multiple' => $multiple, 'expanded' => $expanded]); $this->assertSame($expectedCompound, $options['compound']); $this->assertSame('choice', $options['template']); $this->assertSame($multiple, $options['multiple']); $this->assertSame($expanded, $options['expanded']); $this->assertInstanceOf(ModelManagerInterface::class, $options['model_manager']); - $this->assertNull($options['class']); + $this->assertSame('class-name', $options['class']); $this->assertNull($options['property']); $this->assertNull($options['query']); $this->assertCount(0, $options['choices']);