Skip to content

Commit

Permalink
Increase PHPStan to level 8
Browse files Browse the repository at this point in the history
  • Loading branch information
jordisala1991 committed Aug 6, 2022
1 parent f2137c1 commit 66fd634
Show file tree
Hide file tree
Showing 46 changed files with 265 additions and 220 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon

parameters:
level: 7
level: 8
symfony:
console_application_loader: ./phpstan-console-application.php
paths:
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ protected function configureFormFields(FormMapper $form): void
$block->setPage($page);
}

if ($block->getPage()->getId() !== $page->getId()) {
$blockPage = $block->getPage();

if (null === $blockPage || $blockPage->getId() !== $page->getId()) {
throw new \RuntimeException('The page reference on BlockAdmin and parent admin are not the same');
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Admin/Extension/CreateSnapshotAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public function postRemove(AdminInterface $admin, object $object): void
private function createByPage(object $object): void
{
if ($object instanceof PageBlockInterface) {
$page = $object->getPage();
} elseif ($object instanceof PageInterface) {
$page = $object;
} else {
$object = $object->getPage();
}

if (!$object instanceof PageInterface) {
return;
}

$this->createSnapshotByPage->createByPage($page);
$this->createSnapshotByPage->createByPage($object);
}
}
26 changes: 15 additions & 11 deletions src/Admin/PageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ final class PageAdmin extends AbstractAdmin
{
protected $classnameLabel = 'Page';

private ?PageManagerInterface $pageManager = null;
private PageManagerInterface $pageManager;

private ?SiteManagerInterface $siteManager = null;
private SiteManagerInterface $siteManager;

public function setPageManager(PageManagerInterface $pageManager): void
{
public function __construct(
PageManagerInterface $pageManager,
SiteManagerInterface $siteManager,
) {
$this->pageManager = $pageManager;
}

public function setSiteManager(SiteManagerInterface $siteManager): void
{
$this->siteManager = $siteManager;
}

Expand Down Expand Up @@ -360,10 +358,16 @@ protected function configureTabMenu(ItemInterface $menu, string $action, ?AdminI
if (!$page->isHybrid() && !$page->isInternal()) {
try {
$path = $page->getUrl();
$siteRelativePath = $page->getSite()->getRelativePath();
if (!empty($siteRelativePath)) {
$path = $siteRelativePath.$path;
$site = $page->getSite();

if (null !== $site) {
$siteRelativePath = $site->getRelativePath();

if (null !== $siteRelativePath) {
$path = $siteRelativePath.$path;
}
}

$menu->addChild('view_page', [
'uri' => $this->getRouteGenerator()->generate('page_slug', [
'path' => $path,
Expand Down
2 changes: 1 addition & 1 deletion src/Block/BreadcrumbBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function createMenuItem(ItemInterface $menu, PageInterface $page): void
$extras['translation_domain'] = 'SonataPageBundle';
}

$menu->addChild($label, [
$menu->addChild($label ?? '', [
'route' => 'page_slug',
'routeParameters' => [
'path' => $page->getUrl(),
Expand Down
17 changes: 13 additions & 4 deletions src/Block/ChildrenPagesBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
}
}

return $this->renderResponse($blockContext->getTemplate(), [
$template = $blockContext->getTemplate();
\assert(null !== $template);

return $this->renderResponse($template, [
'page' => $page,
'block' => $blockContext->getBlock(),
'settings' => $settings,
Expand All @@ -108,6 +111,8 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void
return;
}

$site = null !== $block->getPage() ? $block->getPage()->getSite() : null;

$form->add('settings', ImmutableArrayType::class, [
'keys' => [
['title', TextType::class, [
Expand All @@ -129,7 +134,7 @@ public function configureEditForm(FormMapper $form, BlockInterface $block): void
['pageId', PageSelectorType::class, [
'model_manager' => $this->pageAdmin->getModelManager(),
'class' => $this->pageAdmin->getClass(),
'site' => $block->getPage()->getSite(),
'site' => $site,
'required' => false,
'label' => 'form.label_page',
]],
Expand Down Expand Up @@ -186,9 +191,13 @@ public function load(BlockInterface $block): void

if (is_numeric($block->getSetting('pageId', null))) {
$cmsManager = $this->cmsManagerSelector->retrieve();
$site = $block->getPage()->getSite();

$block->setSetting('pageId', $cmsManager->getPage($site, $block->getSetting('pageId')));
if (null !== $block->getPage() && null !== $block->getPage()->getSite()) {
$block->setSetting('pageId', $cmsManager->getPage(
$block->getPage()->getSite(),
$block->getSetting('pageId')
));
}
}
}
}
5 changes: 4 additions & 1 deletion src/Block/PageListBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
'parent' => null,
]);

return $this->renderResponse($blockContext->getTemplate(), [
$template = $blockContext->getTemplate();
\assert(null !== $template);

return $this->renderResponse($template, [
'context' => $blockContext,
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
Expand Down
5 changes: 4 additions & 1 deletion src/Block/SharedBlockBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
/** @var Block $sharedBlock */
$sharedBlock = $block->getSetting('blockId');

return $this->renderResponse($blockContext->getTemplate(), [
$template = $blockContext->getTemplate();
\assert(null !== $template);

return $this->renderResponse($template, [
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
'sharedBlock' => $sharedBlock,
Expand Down
2 changes: 2 additions & 0 deletions src/Command/CleanupSnapshotsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function __construct(

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down
22 changes: 15 additions & 7 deletions src/Command/CloneSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function __construct(

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down Expand Up @@ -147,15 +149,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln('Fixing page parents');
foreach ($pageClones as $page) {
if ($page->getParent()) {
if (\array_key_exists($page->getParent()->getId(), $pageClones)) {
$id = $page->getParent()->getId();
\assert(null !== $id);

if (\array_key_exists($id, $pageClones)) {
$output->writeln(sprintf(
'new parent: % 4s - % -70s - % 4s -> % 4s',
$page->getId(),
$page->getTitle(),
$page->getParent()->getId(),
$pageClones[$page->getParent()->getId()]->getId()
$id,
$pageClones[$id]->getId()
));
$page->setParent($pageClones[$page->getParent()->getId()]);
$page->setParent($pageClones[$id]);
} else {
$page->setParent(null);
}
Expand All @@ -171,14 +176,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
]);
foreach ($blocks as $block) {
if ($block->getParent()) {
$id = $block->getParent()->getId();
\assert(null !== $id);

$output->writeln(sprintf(
'new block parent: % 4s - % 4s',
$block->getId(),
$blockClones[$block->getParent()->getId()]->getId()
$blockClones[$id]->getId()
));

if (\array_key_exists($block->getParent()->getId(), $blockClones)) {
$block->setParent($blockClones[$block->getParent()->getId()]);
if (\array_key_exists($id, $blockClones)) {
$block->setParent($blockClones[$id]);
} else {
$block->setParent(null);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Command/CreateBlockContainerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct(PageManagerInterface $pageManager, BlockInteractorIn

protected function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down
2 changes: 2 additions & 0 deletions src/Command/CreateSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function __construct(SiteManagerInterface $siteManager)

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down
2 changes: 2 additions & 0 deletions src/Command/CreateSnapshotsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function __construct(CreateSnapshotBySiteInterface $createSnapshot, SiteM

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down
2 changes: 2 additions & 0 deletions src/Command/UpdateCoreRoutesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function __construct(

public function configure(): void
{
\assert(null !== static::$defaultDescription);

$this
// TODO: Remove setDescription when support for Symfony < 5.4 is dropped.
->setDescription(static::$defaultDescription)
Expand Down
5 changes: 4 additions & 1 deletion src/Entity/BlockInteractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ public function loadPageBlocks(PageInterface $page)
continue;
}

$blocks[$block->getParent()->getId()]->addChild($block);
$id = $parent->getId();
\assert(null !== $id);

$blocks[$id]->addChild($block);
}

$this->pageBlocksLoaded[$page->getId()] = true;
Expand Down
22 changes: 15 additions & 7 deletions src/Entity/BlockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,40 @@ final class BlockManager extends BaseEntityManager implements BlockManagerInterf
{
public function updatePosition($id, $position, $parentId = null, $pageId = null, $partial = true)
{
$entityManager = $this->getEntityManager();

if ($partial) {
$meta = $this->getEntityManager()->getClassMetadata($this->getClass());
$meta = $entityManager->getClassMetadata($this->getClass());
$block = $entityManager->getReference($this->getClass(), $id);

if (null === $block) {
throw new \RuntimeException(sprintf('Unable to update position to block with id %s', $id));
}

// retrieve object references
$block = $this->getEntityManager()->getReference($this->getClass(), $id);
$pageRelation = $meta->getAssociationMapping('page');
/** @var class-string<PageInterface> */
$pageClassName = $pageRelation['targetEntity'];

$page = $this->getEntityManager()->getPartialReference($pageClassName, $pageId);
$page = $entityManager->getPartialReference($pageClassName, $pageId);

$parentRelation = $meta->getAssociationMapping('parent');
/** @var class-string<PageBlockInterface> */
$parentClassName = $pageRelation['targetEntity'];

$parent = $this->getEntityManager()->getPartialReference($parentClassName, $parentId);
$parent = $entityManager->getPartialReference($parentClassName, $parentId);

$block->setPage($page);
$block->setParent($parent);
} else {
$block = $this->find($id);

if (null === $block) {
throw new \RuntimeException(sprintf('Unable to update position to block with id %s', $id));
}
}

// set new values
$block->setPosition($position);
$this->getEntityManager()->persist($block);
$entityManager->persist($block);

return $block;
}
Expand Down
17 changes: 10 additions & 7 deletions src/Entity/PageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,21 @@ public function fixUrl(PageInterface $page): void

// hybrid page cannot be altered
if (!$page->isHybrid()) {
// make sure Page has a valid url
if ($page->getParent()) {
$parent = $page->getParent();

if (null !== $parent) {
if (!$page->getSlug()) {
$page->setSlug($this->slugify->slugify($page->getName()));
$page->setSlug($this->slugify->slugify($page->getName() ?? ''));
}

if ('/' === $page->getParent()->getUrl()) {
$parentUrl = $parent->getUrl();

if ('/' === $parentUrl) {
$base = '/';
} elseif ('/' !== substr($page->getParent()->getUrl(), -1)) {
$base = $page->getParent()->getUrl().'/';
} elseif ('/' !== substr($parentUrl ?? '', -1)) {
$base = $parentUrl.'/';
} else {
$base = $page->getParent()->getUrl();
$base = $parentUrl;
}

$page->setUrl($base.$page->getSlug());
Expand Down
7 changes: 6 additions & 1 deletion src/Entity/SnapshotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public function enableSnapshots(array $snapshots, ?\DateTimeInterface $date = nu
$pageIds = $snapshotIds = [];

foreach ($snapshots as $snapshot) {
$pageIds[] = $snapshot->getPage()->getId();
$page = $snapshot->getPage();

if (null !== $page) {
$pageIds[] = $page->getId();
}

$snapshotIds[] = $snapshot->getId();

$snapshot->setPublicationDateStart($date);
Expand Down
Loading

0 comments on commit 66fd634

Please sign in to comment.