diff --git a/src/Action/SetObjectFieldValueAction.php b/src/Action/SetObjectFieldValueAction.php index 7a03ea17e2c..6bc6c311e2f 100644 --- a/src/Action/SetObjectFieldValueAction.php +++ b/src/Action/SetObjectFieldValueAction.php @@ -123,9 +123,8 @@ public function __invoke(Request $request): JsonResponse $rootObject = $object; // If property path has more than 1 element, take the last object in order to validate it - if ($propertyPath->getLength() > 1) { - $parent = $propertyPath->getParent(); - \assert(null !== $parent); + $parent = $propertyPath->getParent(); + if (null !== $parent) { $object = $this->propertyAccessor->getValue($object, $parent); $elements = $propertyPath->getElements(); diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index beb26a9236c..6e71741a49d 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -230,18 +230,6 @@ abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterfa */ protected $menu; - /** - * @var array - */ - protected $loaded = [ - 'routes' => false, - 'tab_menu' => false, - 'show' => false, - 'list' => false, - 'form' => false, - 'datagrid' => false, - ]; - /** * @var string[] */ @@ -362,6 +350,18 @@ abstract class AbstractAdmin extends AbstractTaggedAdmin implements AdminInterfa */ private $showTabs = []; + /** + * @var array + */ + private $loaded = [ + 'routes' => false, + 'tab_menu' => false, + 'show' => false, + 'list' => false, + 'form' => false, + 'datagrid' => false, + ]; + public function getExportFormats(): array { return [ @@ -520,7 +520,11 @@ final public function getFilterParameters(): array } } - if (!isset($parameters['_per_page']) || !$this->determinedPerPageValue($parameters['_per_page'])) { + if ( + !isset($parameters['_per_page']) + || !\is_int($parameters['_per_page']) + || !$this->determinedPerPageValue($parameters['_per_page']) + ) { $parameters['_per_page'] = $this->getMaxPerPage(); } @@ -787,10 +791,12 @@ final public function getBatchActions(): array final public function getRoutes(): RouteCollectionInterface { - $this->buildRoutes(); - \assert(null !== $this->routes); + $routes = $this->buildRoutes(); + if (null === $routes) { + throw new \LogicException('Cannot access routes during the building process.'); + } - return $this->routes; + return $routes; } public function getRouterIdParameter(): string @@ -961,20 +967,22 @@ final public function getObject($id): ?object final public function getForm(): FormInterface { - $this->buildForm(); - - \assert(null !== $this->form); + $form = $this->buildForm(); + if (null === $form) { + throw new \LogicException('Cannot access form during the building process.'); + } - return $this->form; + return $form; } final public function getList(): FieldDescriptionCollection { - $this->buildList(); - - \assert(null !== $this->list); + $list = $this->buildList(); + if (null === $list) { + throw new \LogicException('Cannot access list during the building process.'); + } - return $this->list; + return $list; } final public function createQuery(): ProxyQueryInterface @@ -991,11 +999,12 @@ final public function createQuery(): ProxyQueryInterface final public function getDatagrid(): DatagridInterface { - $this->buildDatagrid(); - - \assert(null !== $this->datagrid); + $datagrid = $this->buildDatagrid(); + if (null === $datagrid) { + throw new \LogicException('Cannot access datagrid during the building process.'); + } - return $this->datagrid; + return $datagrid; } final public function getSideMenu(string $action, ?AdminInterface $childAdmin = null): ItemInterface @@ -1004,10 +1013,12 @@ final public function getSideMenu(string $action, ?AdminInterface $childAdmin = return $this->getParent()->getSideMenu($action, $this); } - $this->buildTabMenu($action, $childAdmin); - \assert(null !== $this->menu); + $menu = $this->buildTabMenu($action, $childAdmin); + if (null === $menu) { + throw new \LogicException('Cannot access menu during the building process.'); + } - return $this->menu; + return $menu; } final public function getRootCode(): string @@ -1627,10 +1638,12 @@ public function id(object $model): ?string final public function getShow(): FieldDescriptionCollection { - $this->buildShow(); - \assert(null !== $this->show); + $show = $this->buildShow(); + if (null === $show) { + throw new \LogicException('Cannot access show during the building process.'); + } - return $this->show; + return $show; } final public function setFormTheme(array $formTheme): void @@ -1695,12 +1708,10 @@ public function getPerPageOptions(): array /** * Returns true if the per page value is allowed, false otherwise. - * - * @param mixed $perPage */ - final public function determinedPerPageValue($perPage): bool + final public function determinedPerPageValue(int $perPage): bool { - return \is_int($perPage) && \in_array($perPage, $this->getPerPageOptions(), true); + return \in_array($perPage, $this->getPerPageOptions(), true); } final public function isAclEnabled(): bool @@ -2287,10 +2298,10 @@ final protected function appendParentObject(object $object): void } } - private function buildDatagrid(): void + private function buildDatagrid(): ?DatagridInterface { if ($this->loaded['datagrid']) { - return; + return $this->datagrid; } $this->loaded['datagrid'] = true; @@ -2342,12 +2353,14 @@ private function buildDatagrid(): void foreach ($this->getExtensions() as $extension) { $extension->configureDatagridFilters($mapper); } + + return $this->datagrid; } - private function buildShow(): void + private function buildShow(): ?FieldDescriptionCollection { if ($this->loaded['show']) { - return; + return $this->show; } $this->loaded['show'] = true; @@ -2360,12 +2373,14 @@ private function buildShow(): void foreach ($this->getExtensions() as $extension) { $extension->configureShowFields($mapper); } + + return $this->show; } - private function buildList(): void + private function buildList(): ?FieldDescriptionCollection { if ($this->loaded['list']) { - return; + return $this->list; } $this->loaded['list'] = true; @@ -2410,23 +2425,27 @@ private function buildList(): void $mapper->add($fieldDescription, ListMapper::TYPE_SELECT); } + + return $this->list; } - private function buildForm(): void + private function buildForm(): ?FormInterface { if ($this->loaded['form']) { - return; + return $this->form; } $this->loaded['form'] = true; $this->form = $this->getFormBuilder()->getForm(); + + return $this->form; } - private function buildRoutes(): void + private function buildRoutes(): ?RouteCollectionInterface { if ($this->loaded['routes']) { - return; + return $this->routes; } $this->loaded['routes'] = true; @@ -2445,34 +2464,36 @@ private function buildRoutes(): void foreach ($this->getExtensions() as $extension) { $extension->configureRoutes($this, $this->routes); } + + return $this->routes; } /** * @phpstan-param AdminInterface|null $childAdmin */ - private function buildTabMenu(string $action, ?AdminInterface $childAdmin = null): void + private function buildTabMenu(string $action, ?AdminInterface $childAdmin = null): ?ItemInterface { if ($this->loaded['tab_menu']) { - return; + return $this->menu; } $this->loaded['tab_menu'] = true; - $menu = $this->getMenuFactory()->createItem('root'); - $menu->setChildrenAttribute('class', 'nav navbar-nav'); - $menu->setExtra('translation_domain', $this->getTranslationDomain()); + $this->menu = $this->getMenuFactory()->createItem('root'); + $this->menu->setChildrenAttribute('class', 'nav navbar-nav'); + $this->menu->setExtra('translation_domain', $this->getTranslationDomain()); // Prevents BC break with KnpMenuBundle v1.x - if (method_exists($menu, 'setCurrentUri')) { - $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo()); + if (method_exists($this->menu, 'setCurrentUri')) { + $this->menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo()); } - $this->configureTabMenu($menu, $action, $childAdmin); + $this->configureTabMenu($this->menu, $action, $childAdmin); foreach ($this->getExtensions() as $extension) { - $extension->configureTabMenu($this, $menu, $action, $childAdmin); + $extension->configureTabMenu($this, $this->menu, $action, $childAdmin); } - $this->menu = $menu; + return $this->menu; } } diff --git a/src/DependencyInjection/Admin/AbstractTaggedAdmin.php b/src/DependencyInjection/Admin/AbstractTaggedAdmin.php index afc58cc47d1..ce7f56479f9 100644 --- a/src/DependencyInjection/Admin/AbstractTaggedAdmin.php +++ b/src/DependencyInjection/Admin/AbstractTaggedAdmin.php @@ -63,14 +63,14 @@ abstract class AbstractTaggedAdmin implements TaggedAdminInterface /** * @var string|null */ - protected $label; + private $label; /** * @var array> * * @phpstan-var array{list: array{class: string}, mosaic: array{class: string}} */ - protected $listModes = [ + private $listModes = [ 'list' => ['class' => 'fa fa-list fa-fw'], 'mosaic' => ['class' => self::MOSAIC_ICON_CLASS], ]; @@ -78,109 +78,109 @@ abstract class AbstractTaggedAdmin implements TaggedAdminInterface /** * @var string */ - protected $pagerType = Pager::TYPE_DEFAULT; + private $pagerType = Pager::TYPE_DEFAULT; /** * The manager type to use for the admin. * * @var string|null */ - protected $managerType; + private $managerType; /** * Roles and permissions per role. * * @var array 'role' => ['permission1', 'permission2'] */ - protected $securityInformation = []; + private $securityInformation = []; /** * Component responsible for persisting filters. * * @var FilterPersisterInterface|null */ - protected $filterPersister; + private $filterPersister; /** * The Entity or Document manager. * * @var ModelManagerInterface|null */ - protected $modelManager; + private $modelManager; /** * @var DataSourceInterface|null */ - protected $dataSource; + private $dataSource; /** * The related form contractor. * * @var FormContractorInterface|null */ - protected $formContractor; + private $formContractor; /** * The related view builder. * * @var ShowBuilderInterface|null */ - protected $showBuilder; + private $showBuilder; /** * The related list builder. * * @var ListBuilderInterface|null */ - protected $listBuilder; + private $listBuilder; /** * The related datagrid builder. * * @var DatagridBuilderInterface|null */ - protected $datagridBuilder; + private $datagridBuilder; /** * The translator component. * * @var TranslatorInterface|null */ - protected $translator; + private $translator; /** * The configuration pool. * * @var Pool|null */ - protected $configurationPool; + private $configurationPool; /** * The router instance. * * @var RouteGeneratorInterface|null */ - protected $routeGenerator; + private $routeGenerator; /** * @var SecurityHandlerInterface|null */ - protected $securityHandler; + private $securityHandler; /** * @var FactoryInterface|null */ - protected $menuFactory; + private $menuFactory; /** * @var RouteBuilderInterface|null */ - protected $routeBuilder; + private $routeBuilder; /** * @var LabelTranslatorStrategyInterface|null */ - protected $labelTranslatorStrategy; + private $labelTranslatorStrategy; /** * @var FieldDescriptionFactoryInterface|null