diff --git a/src/Util/AdminAclManipulator.php b/src/Util/AdminAclManipulator.php index dc1e5da5aa..8b4a742746 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; /** @@ -28,13 +27,14 @@ final class AdminAclManipulator implements AdminAclManipulatorInterface { /** * @var string + * @phpstan-var class-string */ private $maskBuilderClass; /** - * @param string $maskBuilderClass + * @phpstan-param class-string $maskBuilderClass */ - public function __construct($maskBuilderClass) + public function __construct(string $maskBuilderClass) { $this->maskBuilderClass = $maskBuilderClass; } @@ -69,17 +69,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 d00b3d63a6..6b3aac967a 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 @@ -26,17 +26,17 @@ interface AdminAclManipulatorInterface /** * Batch configure the ACLs for all objects handled by an Admin. */ - public function configureAcls(OutputInterface $output, AdminInterface $admin); + public function configureAcls(OutputInterface $output, AdminInterface $admin): void; /** * Add the class ACE's to the admin ACL. * - * @return bool TRUE if admin class ACEs are added, FALSE if not + * @param array $roleInformation */ 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 12e5fc789c..344e70cd58 100644 --- a/src/Util/AdminObjectAclData.php +++ b/src/Util/AdminObjectAclData.php @@ -15,7 +15,6 @@ 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; @@ -30,7 +29,7 @@ class AdminObjectAclData { /** - * @var array Permissions managed only by a OWNER + * @var string[] Permissions managed only by a OWNER */ protected static $ownerPermissions = ['MASTER', 'OWNER']; @@ -60,34 +59,37 @@ class AdminObjectAclData protected $masks = []; /** - * @var FormInterface + * @var FormInterface|null */ protected $aclUsersForm; /** - * @var FormInterface + * @var FormInterface|null */ protected $aclRolesForm; /** - * @var MutableAclInterface + * @var MutableAclInterface|null */ protected $acl; /** * @var string + * @phpstan-var class-string */ protected $maskBuilderClass; /** - * @param object $object - * @param string $maskBuilderClass + * @param \Traversable $aclUsers + * @param \Traversable|null $aclRoles + * + * @phpstan-param class-string $maskBuilderClass */ public function __construct( AdminInterface $admin, - $object, + object $object, \Traversable $aclUsers, - $maskBuilderClass, + string $maskBuilderClass, ?\Traversable $aclRoles = null ) { $this->admin = $admin; @@ -102,138 +104,79 @@ 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 { + \assert($this->acl !== null); + 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 { + \assert($this->aclUsersForm !== null); + 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 { + \assert($this->aclRolesForm !== null); + 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(); @@ -249,28 +192,24 @@ public function getUserPermissions() return $permissions; } - public function getOwnerPermissions() + /** + * @return string[] + */ + 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); @@ -278,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 9a9b440b1e..8128452f1e 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; @@ -42,36 +41,28 @@ class AdminObjectAclManipulator * @var FormFactoryInterface */ protected $formFactory; + /** * @var string + * @phpstan-var class-string */ 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); @@ -81,12 +72,7 @@ public function createAclUsersForm(AdminObjectAclData $data) return $form; } - /** - * 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); @@ -96,9 +82,6 @@ public function createAclRolesForm(AdminObjectAclData $data) return $form; } - /** - * Updates ACL users. - */ public function updateAclUsers(AdminObjectAclData $data): void { $aclValues = $data->getAclUsers(); @@ -107,9 +90,6 @@ public function updateAclUsers(AdminObjectAclData $data): void $this->buildAcl($data, $form, $aclValues); } - /** - * Updates ACL roles. - */ public function updateAclRoles(AdminObjectAclData $data): void { $aclValues = $data->getAclRoles(); @@ -119,7 +99,9 @@ public function updateAclRoles(AdminObjectAclData $data): void } /** - * Builds ACL. + * @param \Traversable $aclValues + * + * @phpstan-param \Traversable $aclValues */ protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Traversable $aclValues): void { @@ -175,7 +157,7 @@ protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Trav } } - if ($ace) { + if (null !== $ace && null !== $index) { $acl->updateObjectAce($index, $mask); } else { $acl->insertObjectAce($securityIdentity, $mask); @@ -186,11 +168,11 @@ protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Trav } /** - * Builds the form. + * @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()); diff --git a/src/Util/FormBuilderIterator.php b/src/Util/FormBuilderIterator.php index c7da9103d3..dface7ff43 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; @@ -46,13 +36,13 @@ final class FormBuilderIterator extends \RecursiveArrayIterator private $iterator; /** - * @param bool $prefix + * @param string|false $prefix */ public function __construct(FormBuilderInterface $formBuilder, $prefix = false) { parent::__construct(); $this->formBuilder = $formBuilder; - $this->prefix = $prefix ? $prefix : $formBuilder->getName(); + $this->prefix = false !== $prefix ? $prefix : $formBuilder->getName(); $this->iterator = new \ArrayIterator(self::getKeys($formBuilder)); } @@ -61,12 +51,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,27 +68,27 @@ 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->formBuilder->get($this->iterator->current())); } - public function hasChildren() + public function hasChildren(): bool { return \count(self::getKeys($this->current())) > 0; } /** - * @static + * @return array * - * @return array + * @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 21b4954677..200b69911c 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 b6047ec732..fee2048432 100644 --- a/src/Util/ObjectAclManipulator.php +++ b/src/Util/ObjectAclManipulator.php @@ -28,7 +28,9 @@ abstract class ObjectAclManipulator implements ObjectAclManipulatorInterface * * @throws \Exception * - * @return array [countAdded, countUpdated] + * @return array [countAdded, countUpdated] + * + * @phpstan-return array{0: int, 1: int} */ public function configureAcls( OutputInterface $output, diff --git a/src/Util/ObjectAclManipulatorInterface.php b/src/Util/ObjectAclManipulatorInterface.php index 91c54ee140..f6317a195f 100644 --- a/src/Util/ObjectAclManipulatorInterface.php +++ b/src/Util/ObjectAclManipulatorInterface.php @@ -32,5 +32,5 @@ public function batchConfigureAcls( OutputInterface $output, AdminInterface $admin, ?UserSecurityIdentity $securityIdentity = null - ); + ): void; }