Skip to content

Commit

Permalink
Add typehints and return types
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 9, 2022
1 parent a0275d3 commit ee734f1
Show file tree
Hide file tree
Showing 101 changed files with 717 additions and 1,956 deletions.
12 changes: 3 additions & 9 deletions src/Admin/BaseBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,14 @@
*/
abstract class BaseBlockAdmin extends AbstractAdmin
{
/**
* @var BlockServiceManagerInterface
*/
protected $blockManager;
protected BlockServiceManagerInterface $blockManager;

/**
* @var bool
*/
protected $inValidate = false;
protected bool $inValidate = false;

/**
* @var array<string>
*/
protected $containerBlockTypes = [];
protected array $containerBlockTypes = [];

public function __construct(BlockServiceManagerInterface $blockManager)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ protected function configureFormFields(FormMapper $form): void
}
}

/**
* @return string|null
*/
private function getDefaultTemplate(BlockServiceInterface $blockService)
private function getDefaultTemplate(BlockServiceInterface $blockService): ?string
{
$resolver = new OptionsResolver();
$blockService->configureSettings($resolver);
Expand Down
8 changes: 3 additions & 5 deletions src/Admin/PageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ protected function configureRoutes(RouteCollectionInterface $collection): void
$collection->add('tree', 'tree');
}

protected function preUpdate($object): void
protected function preUpdate(object $object): void
{
$object->setEdited(true);
}

protected function prePersist($object): void
protected function prePersist(object $object): void
{
$object->setEdited(true);
}
Expand Down Expand Up @@ -392,10 +392,8 @@ protected function configureTabMenu(ItemInterface $menu, string $action, ?AdminI

/**
* @throws \RuntimeException
*
* @return SiteInterface|null
*/
private function getSite()
private function getSite(): ?SiteInterface
{
if (!$this->hasRequest()) {
return null;
Expand Down
2 changes: 0 additions & 2 deletions src/Admin/SharedBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\PageBundle\Entity\BaseBlock;
use Sonata\PageBundle\Mapper\PageFormMapper;
use Sonata\PageBundle\Model\PageBlockInterface;

Expand Down Expand Up @@ -63,7 +62,6 @@ protected function configureListFields(ListMapper $list): void

protected function configureFormFields(FormMapper $form): void
{
/** @var BaseBlock $block */
$block = $this->getSubject();

// New block
Expand Down
10 changes: 2 additions & 8 deletions src/Block/BreadcrumbBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,12 @@ protected function getMenu(BlockContextInterface $blockContext): ItemInterface
return $menu;
}

/**
* @return string
*/
private function getName()
private function getName(): string
{
return 'sonata.page.block.breadcrumb';
}

/**
* @return PageInterface|null
*/
private function getCurrentPage()
private function getCurrentPage(): ?PageInterface
{
$cms = $this->cmsSelector->retrieve();

Expand Down
2 changes: 0 additions & 2 deletions src/Block/SharedBlockBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Sonata\Doctrine\Model\ManagerInterface;
use Sonata\Form\Type\ImmutableArrayType;
use Sonata\Form\Validator\ErrorElement;
use Sonata\PageBundle\Model\Block;
use Sonata\PageBundle\Model\PageBlockInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -71,7 +70,6 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
$this->load($block);
}

/** @var Block $sharedBlock */
$sharedBlock = $block->getSetting('blockId');

$template = $blockContext->getTemplate();
Expand Down
26 changes: 10 additions & 16 deletions src/CmsManager/BaseCmsPageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
*/
abstract class BaseCmsPageManager implements CmsManagerInterface
{
/**
* @var PageInterface|null
*/
protected $currentPage = null;
protected ?PageInterface $currentPage = null;

/**
* @var array<PageBlockInterface|null>
*/
protected $blocks = [];
protected array $blocks = [];

public function getCurrentPage()
public function getCurrentPage(): ?PageInterface
{
return $this->currentPage;
}
Expand All @@ -42,43 +39,40 @@ public function setCurrentPage(PageInterface $page): void
$this->currentPage = $page;
}

public function getBlocks()
public function getBlocks(): array
{
return $this->blocks;
}

public function getPageByUrl(SiteInterface $site, $slug): PageInterface
public function getPageByUrl(SiteInterface $site, string $slug): PageInterface
{
return $this->getPageBy($site, 'url', $slug);
}

public function getPageByRouteName(SiteInterface $site, $routeName)
public function getPageByRouteName(SiteInterface $site, string $routeName): PageInterface
{
return $this->getPageBy($site, 'routeName', $routeName);
}

public function getPageByPageAlias(SiteInterface $site, $pageAlias)
public function getPageByPageAlias(SiteInterface $site, string $pageAlias): PageInterface
{
return $this->getPageBy($site, 'pageAlias', $pageAlias);
}

public function getPageByName(SiteInterface $site, $name)
public function getPageByName(SiteInterface $site, string $name): PageInterface
{
return $this->getPageBy($site, 'name', $name);
}

public function getPageById($id)
public function getPageById($id): PageInterface
{
return $this->getPageBy(null, 'id', $id);
}

/**
* @param string $fieldName
* @param int|string $value
*
* @return PageInterface
*
* @phpstan-param 'id'|'url'|'routeName'|'pageAlias'|'name' $fieldName
*/
abstract protected function getPageBy(?SiteInterface $site, $fieldName, $value);
abstract protected function getPageBy(?SiteInterface $site, string $fieldName, $value): PageInterface;
}
64 changes: 12 additions & 52 deletions src/CmsManager/CmsManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,79 +25,39 @@
*/
interface CmsManagerInterface
{
/**
* @param string $name
*
* @return PageBlockInterface|null
*/
public function findContainer($name, PageInterface $page, ?PageBlockInterface $parentContainer = null);
public function findContainer(string $name, PageInterface $page, ?PageBlockInterface $parentContainer = null): ?PageBlockInterface;

/**
* @param string $slug
*/
public function getPageByUrl(SiteInterface $site, $slug): PageInterface;
public function getPageByUrl(SiteInterface $site, string $slug): PageInterface;

/**
* @param string $routeName
*
* @return PageInterface
*/
public function getPageByRouteName(SiteInterface $site, $routeName);
public function getPageByRouteName(SiteInterface $site, string $routeName): PageInterface;

/**
* @param string $pageAlias
*
* @return PageInterface
*/
public function getPageByPageAlias(SiteInterface $site, $pageAlias);
public function getPageByPageAlias(SiteInterface $site, string $pageAlias): PageInterface;

/**
* @param string $routeName
*
* @return PageInterface
*/
public function getInternalRoute(SiteInterface $site, $routeName);
public function getInternalRoute(SiteInterface $site, string $routeName): PageInterface;

/**
* @param string $name
*
* @return PageInterface
*/
public function getPageByName(SiteInterface $site, $name);
public function getPageByName(SiteInterface $site, string $name): PageInterface;

/**
* @param int|string $id
*
* @return PageInterface
*/
public function getPageById($id);
public function getPageById($id): PageInterface;

/**
* @param int|string $id
*
* @return PageBlockInterface|null
*/
public function getBlock($id);
public function getBlock($id): ?PageBlockInterface;

/**
* @return PageInterface|null
*/
public function getCurrentPage();
public function getCurrentPage(): ?PageInterface;

/**
* @return void
*/
public function setCurrentPage(PageInterface $page);
public function setCurrentPage(PageInterface $page): void;

/**
* @return array<PageBlockInterface|null>
*/
public function getBlocks();
public function getBlocks(): array;

/**
* @param int|string|null $page
*
* @return PageInterface
*/
public function getPage(SiteInterface $site, $page);
public function getPage(SiteInterface $site, $page): PageInterface;
}
6 changes: 4 additions & 2 deletions src/CmsManager/CmsManagerSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
final class CmsManagerSelector implements CmsManagerSelectorInterface, BCLogoutHandlerInterface
{
private CmsPageManager $cmsPageManager;

private CmsSnapshotManager $cmsSnapshotManager;

/**
Expand All @@ -41,6 +42,7 @@ final class CmsManagerSelector implements CmsManagerSelectorInterface, BCLogoutH
private AdminInterface $pageAdmin;

private TokenStorageInterface $tokenStorage;

private RequestStack $requestStack;

/**
Expand All @@ -60,7 +62,7 @@ public function __construct(
$this->requestStack = $requestStack;
}

public function retrieve()
public function retrieve(): CmsManagerInterface
{
return $this->isEditor() ? $this->cmsPageManager : $this->cmsSnapshotManager;
}
Expand All @@ -70,7 +72,7 @@ public function retrieve()
* by the router chain, so we need to use another mechanism. It is not perfect
* but do the job for now.
*/
public function isEditor()
public function isEditor(): bool
{
$request = $this->requestStack->getCurrentRequest();

Expand Down
10 changes: 2 additions & 8 deletions src/CmsManager/CmsManagerSelectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@
*/
interface CmsManagerSelectorInterface
{
/**
* @return CmsManagerInterface
*/
public function retrieve();
public function retrieve(): CmsManagerInterface;

/**
* @return bool
*/
public function isEditor();
public function isEditor(): bool;
}
12 changes: 6 additions & 6 deletions src/CmsManager/CmsPageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class CmsPageManager extends BaseCmsPageManager
];

/**
* @var PageInterface[]
* @var array<PageInterface>
*/
private array $pages = [];

Expand All @@ -55,7 +55,7 @@ public function __construct(PageManagerInterface $pageManager, BlockInteractorIn
$this->blockInteractor = $blockInteractor;
}

public function getPage(SiteInterface $site, $page)
public function getPage(SiteInterface $site, $page): PageInterface
{
if (\is_string($page) && '/' === substr($page, 0, 1)) {
$page = $this->getPageByUrl($site, $page);
Expand All @@ -74,7 +74,7 @@ public function getPage(SiteInterface $site, $page)
return $page;
}

public function getInternalRoute(SiteInterface $site, $routeName)
public function getInternalRoute(SiteInterface $site, string $routeName): PageInterface
{
if ('error' === substr($routeName, 0, 5)) {
throw new \RuntimeException(sprintf('Illegal internal route name : %s, an internal page cannot start with `error`', $routeName));
Expand All @@ -100,7 +100,7 @@ public function getInternalRoute(SiteInterface $site, $routeName)
return $page;
}

public function findContainer($name, PageInterface $page, ?PageBlockInterface $parentContainer = null)
public function findContainer(string $name, PageInterface $page, ?PageBlockInterface $parentContainer = null): ?PageBlockInterface
{
$container = null;

Expand Down Expand Up @@ -134,7 +134,7 @@ public function findContainer($name, PageInterface $page, ?PageBlockInterface $p
return $container;
}

public function getBlock($id)
public function getBlock($id): ?PageBlockInterface
{
if (!\array_key_exists($id, $this->blocks)) {
$this->blocks[$id] = $this->blockInteractor->getBlock($id);
Expand All @@ -143,7 +143,7 @@ public function getBlock($id)
return $this->blocks[$id];
}

protected function getPageBy(?SiteInterface $site, $fieldName, $value)
protected function getPageBy(?SiteInterface $site, string $fieldName, $value): PageInterface
{
if ('id' === $fieldName) {
$id = $value;
Expand Down
Loading

0 comments on commit ee734f1

Please sign in to comment.