From 1bd833be1cb4ede10b9630289ccb7b4750b5304b Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 23 Oct 2020 16:42:54 +0200 Subject: [PATCH] add type hints --- src/Util/AdminAclManipulator.php | 16 +--- src/Util/AdminAclManipulatorInterface.php | 10 +- src/Util/AdminObjectAclData.php | 106 ++++----------------- src/Util/AdminObjectAclManipulator.php | 33 ++----- src/Util/FormBuilderIterator.php | 33 ++----- src/Util/FormViewIterator.php | 10 +- src/Util/ObjectAclManipulator.php | 2 +- src/Util/ObjectAclManipulatorInterface.php | 4 +- 8 files changed, 50 insertions(+), 164 deletions(-) diff --git a/src/Util/AdminAclManipulator.php b/src/Util/AdminAclManipulator.php index 1a28d2858f6..3da186457cd 100644 --- a/src/Util/AdminAclManipulator.php +++ b/src/Util/AdminAclManipulator.php @@ -18,7 +18,6 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Security\Acl\Domain\ObjectIdentity; use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; -use Symfony\Component\Security\Acl\Model\AclInterface; use Symfony\Component\Security\Acl\Model\MutableAclInterface; /** @@ -34,11 +33,9 @@ final class AdminAclManipulator implements AdminAclManipulatorInterface private $maskBuilderClass; /** - * @param string $maskBuilderClass - * * @phpstan-param class-string $maskBuilderClass */ - public function __construct($maskBuilderClass) + public function __construct(string $maskBuilderClass) { $this->maskBuilderClass = $maskBuilderClass; } @@ -73,17 +70,10 @@ public function configureAcls(OutputInterface $output, AdminInterface $admin): v public function addAdminClassAces( OutputInterface $output, - AclInterface $acl, + MutableAclInterface $acl, AclSecurityHandlerInterface $securityHandler, array $roleInformation = [] - ) { - if (!$acl instanceof MutableAclInterface) { - throw new \TypeError(sprintf( - 'Argument 2 passed to "%s()" must implement "%s".', - __METHOD__, - MutableAclInterface::class - )); - } + ): bool { if (\count($securityHandler->getAdminPermissions()) > 0) { $builder = new $this->maskBuilderClass(); diff --git a/src/Util/AdminAclManipulatorInterface.php b/src/Util/AdminAclManipulatorInterface.php index bcbebf17dd8..5aeee591985 100644 --- a/src/Util/AdminAclManipulatorInterface.php +++ b/src/Util/AdminAclManipulatorInterface.php @@ -16,7 +16,7 @@ use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Security\Acl\Model\AclInterface; +use Symfony\Component\Security\Acl\Model\MutableAclInterface; /** * @author Thomas Rabaix @@ -25,10 +25,8 @@ interface AdminAclManipulatorInterface { /** * Batch configure the ACLs for all objects handled by an Admin. - * - * @return void */ - public function configureAcls(OutputInterface $output, AdminInterface $admin); + public function configureAcls(OutputInterface $output, AdminInterface $admin): void; /** * Add the class ACE's to the admin ACL. @@ -39,8 +37,8 @@ public function configureAcls(OutputInterface $output, AdminInterface $admin); */ public function addAdminClassAces( OutputInterface $output, - AclInterface $acl, + MutableAclInterface $acl, AclSecurityHandlerInterface $securityHandler, array $roleInformation = [] - ); + ): bool; } diff --git a/src/Util/AdminObjectAclData.php b/src/Util/AdminObjectAclData.php index 6d1002212b8..28a4da6f2e9 100644 --- a/src/Util/AdminObjectAclData.php +++ b/src/Util/AdminObjectAclData.php @@ -15,9 +15,7 @@ use Sonata\AdminBundle\Admin\AdminInterface; use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface; -use Symfony\Component\Form\Form; use Symfony\Component\Form\FormInterface; -use Symfony\Component\Security\Acl\Domain\Acl; use Symfony\Component\Security\Acl\Model\MutableAclInterface; /** @@ -82,16 +80,13 @@ class AdminObjectAclData protected $maskBuilderClass; /** - * @param object $object - * @param string $maskBuilderClass - * * @phpstan-param class-string $maskBuilderClass */ public function __construct( AdminInterface $admin, - $object, + object $object, \Traversable $aclUsers, - $maskBuilderClass, + string $maskBuilderClass, ?\Traversable $aclRoles = null ) { $this->admin = $admin; @@ -106,138 +101,85 @@ public function __construct( $this->updateMasks(); } - /** - * Gets admin. - * - * @return AdminInterface - */ - public function getAdmin() + public function getAdmin(): AdminInterface { return $this->admin; } - /** - * Gets object. - * - * @return object - */ - public function getObject() + public function getObject(): object { return $this->object; } - /** - * Gets ACL users. - * - * @return \Traversable - */ - public function getAclUsers() + public function getAclUsers(): \Traversable { return $this->aclUsers; } - /** - * Gets ACL roles. - * - * @return \Traversable - */ - public function getAclRoles() + public function getAclRoles(): \Traversable { return $this->aclRoles; } - /** - * Sets ACL. - * - * @return AdminObjectAclData - */ - public function setAcl(MutableAclInterface $acl) + public function setAcl(MutableAclInterface $acl): self { $this->acl = $acl; return $this; } - /** - * Gets ACL. - * - * @return MutableAclInterface - */ - public function getAcl() + public function getAcl(): ?MutableAclInterface { return $this->acl; } /** - * Gets masks. - * * @return array */ - public function getMasks() + public function getMasks(): array { return $this->masks; } /** - * Sets ACL users form. - * * @return AdminObjectAclData */ - public function setAclUsersForm(FormInterface $form) + public function setAclUsersForm(FormInterface $form): self { $this->aclUsersForm = $form; return $this; } - /** - * Gets ACL users form. - * - * @return FormInterface - */ - public function getAclUsersForm() + public function getAclUsersForm(): ?FormInterface { return $this->aclUsersForm; } /** - * Sets ACL roles form. - * * @return AdminObjectAclData */ - public function setAclRolesForm(FormInterface $form) + public function setAclRolesForm(FormInterface $form): self { $this->aclRolesForm = $form; return $this; } - /** - * Gets ACL roles form. - * - * @return FormInterface - */ - public function getAclRolesForm() + public function getAclRolesForm(): ?FormInterface { return $this->aclRolesForm; } - /** - * Gets permissions. - * - * @return array - */ - public function getPermissions() + public function getPermissions(): array { return $this->getSecurityHandler()->getObjectPermissions(); } /** * Get permissions that the current user can set. - * - * @return array */ - public function getUserPermissions() + public function getUserPermissions(): array { $permissions = $this->getPermissions(); @@ -253,28 +195,21 @@ public function getUserPermissions() return $permissions; } - public function getOwnerPermissions() + public function getOwnerPermissions(): array { return self::$ownerPermissions; } /** * Tests if the current user has the OWNER right. - * - * @return bool */ - public function isOwner() + public function isOwner(): bool { // Only a owner can set MASTER and OWNER ACL return $this->admin->isGranted('OWNER', $this->object); } - /** - * Gets security handler. - * - * @return AclSecurityHandlerInterface - */ - public function getSecurityHandler() + public function getSecurityHandler(): AclSecurityHandlerInterface { $securityHandler = $this->admin->getSecurityHandler(); \assert($securityHandler instanceof AclSecurityHandlerInterface); @@ -282,10 +217,7 @@ public function getSecurityHandler() return $securityHandler; } - /** - * @return array - */ - public function getSecurityInformation() + public function getSecurityInformation(): array { return $this->getSecurityHandler()->buildSecurityInformation($this->admin); } diff --git a/src/Util/AdminObjectAclManipulator.php b/src/Util/AdminObjectAclManipulator.php index 1d557232568..74924fb52f0 100644 --- a/src/Util/AdminObjectAclManipulator.php +++ b/src/Util/AdminObjectAclManipulator.php @@ -15,7 +15,6 @@ use Sonata\AdminBundle\Form\Type\AclMatrixType; use Symfony\Component\Form\Extension\Core\Type\FormType; -use Symfony\Component\Form\Form; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; @@ -23,6 +22,7 @@ use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity; use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; use Symfony\Component\Security\Acl\Exception\NoAceFoundException; +use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface; use Symfony\Component\Security\Core\User\UserInterface; /** @@ -51,32 +51,23 @@ class AdminObjectAclManipulator protected $maskBuilderClass; /** - * @param string $maskBuilderClass - * * @phpstan-param class-string $maskBuilderClass */ - public function __construct(FormFactoryInterface $formFactory, $maskBuilderClass) + public function __construct(FormFactoryInterface $formFactory, string $maskBuilderClass) { $this->formFactory = $formFactory; $this->maskBuilderClass = $maskBuilderClass; } - /** - * Gets mask builder class name. - * - * @return string - */ - public function getMaskBuilderClass() + public function getMaskBuilderClass(): string { return $this->maskBuilderClass; } /** * Gets the ACL users form. - * - * @return FormInterface */ - public function createAclUsersForm(AdminObjectAclData $data) + public function createAclUsersForm(AdminObjectAclData $data): FormInterface { $aclValues = $data->getAclUsers(); $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_USERS_FORM_NAME, FormType::class); @@ -88,10 +79,8 @@ public function createAclUsersForm(AdminObjectAclData $data) /** * Gets the ACL roles form. - * - * @return FormInterface */ - public function createAclRolesForm(AdminObjectAclData $data) + public function createAclRolesForm(AdminObjectAclData $data): FormInterface { $aclValues = $data->getAclRoles(); $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_ROLES_FORM_NAME, FormType::class); @@ -101,9 +90,6 @@ public function createAclRolesForm(AdminObjectAclData $data) return $form; } - /** - * Updates ACL users. - */ public function updateAclUsers(AdminObjectAclData $data): void { $aclValues = $data->getAclUsers(); @@ -112,9 +98,6 @@ public function updateAclUsers(AdminObjectAclData $data): void $this->buildAcl($data, $form, $aclValues); } - /** - * Updates ACL roles. - */ public function updateAclRoles(AdminObjectAclData $data): void { $aclValues = $data->getAclRoles(); @@ -195,11 +178,9 @@ protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Trav /** * @param \Traversable $aclValues * - * @return FormInterface - * * @phpstan-param \Traversable $aclValues */ - protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues) + protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues): FormInterface { // Retrieve object identity $objectIdentity = ObjectIdentity::fromDomainObject($data->getObject()); @@ -258,7 +239,7 @@ protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $for * * @return RoleSecurityIdentity|UserSecurityIdentity */ - protected function getSecurityIdentity($aclValue) + protected function getSecurityIdentity($aclValue): SecurityIdentityInterface { return ($aclValue instanceof UserInterface) ? UserSecurityIdentity::fromAccount($aclValue) diff --git a/src/Util/FormBuilderIterator.php b/src/Util/FormBuilderIterator.php index 31ef533609d..acae3e26d32 100644 --- a/src/Util/FormBuilderIterator.php +++ b/src/Util/FormBuilderIterator.php @@ -20,23 +20,13 @@ */ final class FormBuilderIterator extends \RecursiveArrayIterator { - /** - * @var \ReflectionProperty - */ - private static $reflection; - /** * @var FormBuilderInterface */ private $formBuilder; /** - * @var array - */ - private $keys = []; - - /** - * @var bool|string + * @var string */ private $prefix; @@ -45,14 +35,11 @@ final class FormBuilderIterator extends \RecursiveArrayIterator */ private $iterator; - /** - * @param bool $prefix - */ - public function __construct(FormBuilderInterface $formBuilder, $prefix = false) + public function __construct(FormBuilderInterface $formBuilder, ?string $prefix = null) { parent::__construct(); $this->formBuilder = $formBuilder; - $this->prefix = $prefix ? $prefix : $formBuilder->getName(); + $this->prefix = null !== $prefix ? $prefix : $formBuilder->getName(); $this->iterator = new \ArrayIterator(self::getKeys($formBuilder)); } @@ -61,12 +48,12 @@ public function rewind(): void $this->iterator->rewind(); } - public function valid() + public function valid(): bool { return $this->iterator->valid(); } - public function key() + public function key(): string { $name = $this->iterator->current(); @@ -78,17 +65,17 @@ public function next(): void $this->iterator->next(); } - public function current() + public function current(): FormBuilderInterface { return $this->formBuilder->get($this->iterator->current()); } - public function getChildren() + public function getChildren(): self { - return new self($this->formBuilder->get($this->iterator->current()), $this->current()); + return new self($this->current()); } - public function hasChildren() + public function hasChildren(): bool { return \count(self::getKeys($this->current())) > 0; } @@ -98,7 +85,7 @@ public function hasChildren() * * @phpstan-return array-key[] */ - private static function getKeys(FormBuilderInterface $formBuilder) + private static function getKeys(FormBuilderInterface $formBuilder): array { return array_keys($formBuilder->all()); } diff --git a/src/Util/FormViewIterator.php b/src/Util/FormViewIterator.php index 21b49546778..200b69911c2 100644 --- a/src/Util/FormViewIterator.php +++ b/src/Util/FormViewIterator.php @@ -30,17 +30,17 @@ public function __construct(FormView $formView) $this->iterator = $formView->getIterator(); } - public function getChildren() + public function getChildren(): self { return new self($this->current()); } - public function hasChildren() + public function hasChildren(): bool { return \count($this->current()->children) > 0; } - public function current() + public function current(): FormView { return $this->iterator->current(); } @@ -50,12 +50,12 @@ public function next(): void $this->iterator->next(); } - public function key() + public function key(): string { return $this->current()->vars['id']; } - public function valid() + public function valid(): bool { return $this->iterator->valid(); } diff --git a/src/Util/ObjectAclManipulator.php b/src/Util/ObjectAclManipulator.php index fee20484327..0ab0b75f3a1 100644 --- a/src/Util/ObjectAclManipulator.php +++ b/src/Util/ObjectAclManipulator.php @@ -37,7 +37,7 @@ public function configureAcls( AdminInterface $admin, \Traversable $oids, ?UserSecurityIdentity $securityIdentity = null - ) { + ): array { $countAdded = 0; $countUpdated = 0; $securityHandler = $admin->getSecurityHandler(); diff --git a/src/Util/ObjectAclManipulatorInterface.php b/src/Util/ObjectAclManipulatorInterface.php index 5d349a05dec..f6317a195f1 100644 --- a/src/Util/ObjectAclManipulatorInterface.php +++ b/src/Util/ObjectAclManipulatorInterface.php @@ -27,12 +27,10 @@ interface ObjectAclManipulatorInterface * Batch configure the ACLs for all objects handled by an Admin. * * @throws ModelManagerException - * - * @return void */ public function batchConfigureAcls( OutputInterface $output, AdminInterface $admin, ?UserSecurityIdentity $securityIdentity = null - ); + ): void; }