Skip to content

Commit f6296d2

Browse files
committed
Refactor code adding return types and updated php-doc
1 parent 3e3efe3 commit f6296d2

3 files changed

Lines changed: 89 additions & 155 deletions

File tree

src/Admin/PageAdmin.php

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
1717
use Gedmo\Loggable\Entity\LogEntry;
18+
use Gedmo\Loggable\Entity\Repository\LogEntryRepository;
19+
use Networking\InitCmsBundle\Doctrine\Extensions\Versionable\VersionableInterface;
1820
use Networking\InitCmsBundle\Entity\BasePage;
1921
use Networking\InitCmsBundle\Entity\LayoutBlock;
2022
use Networking\InitCmsBundle\Entity\MenuItem;
@@ -28,6 +30,7 @@
2830
use Sonata\AdminBundle\Datagrid\DatagridMapper;
2931
use Sonata\AdminBundle\Datagrid\ListMapper;
3032
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
33+
use Sonata\AdminBundle\Exception\ModelManagerThrowable;
3134
use Sonata\AdminBundle\Filter\Model\FilterData;
3235
use Sonata\AdminBundle\Form\FormMapper;
3336
use Sonata\AdminBundle\Form\Type\Operator\ContainsOperatorType;
@@ -54,27 +57,19 @@
5457
*/
5558
class PageAdmin extends BaseAdmin
5659
{
57-
/**
58-
* @var string
59-
*/
60-
protected $pageLocale;
60+
protected string $pageLocale;
6161

62-
/**
63-
* @var bool
64-
*/
65-
protected $canCreateHomepage = false;
62+
protected bool $canCreateHomepage = false;
6663

67-
/**
68-
* @var string
69-
*/
70-
protected $repository = '';
64+
protected string $repository = '';
7165

7266
private ?LogEntry $lastEditedBy = null;
7367

7468
public function __construct(
7569
protected PageManagerInterface $pageManager,
7670
protected array $pageTemplates,
7771
) {
72+
parent::__construct();
7873
}
7974

8075
protected function generateBaseRoutePattern(bool $isChildAdmin = false
@@ -133,7 +128,7 @@ protected function configureRoutes(RouteCollectionInterface $collection
133128
['method' => 'GET']
134129
);
135130
$collection->add('getPath', 'get_path/', [], ['method' => 'GET']);
136-
$collection->add('batchTranslate', 'batch_translate', []);
131+
$collection->add('batchTranslate', 'batch_translate');
137132
$collection->add(
138133
'editPageSettings',
139134
'edit_page_settings/{id}/{no_layout}',
@@ -277,8 +272,6 @@ protected function configureFormFields(FormMapper $form): void
277272
$this->pageLocale,
278273
$this->hasSubject() ? $this->getSubject()
279274
->getId() : null,
280-
false,
281-
false
282275
),
283276
]
284277
);
@@ -308,7 +301,7 @@ protected function configureFormFields(FormMapper $form): void
308301
}
309302
}
310303

311-
$requireUrl = $this->canCreateHomepage ? false : true;
304+
$requireUrl = !$this->canCreateHomepage;
312305

313306
if ($this->hasObject()) {
314307
if (!$this->getSubject()->isHome()) {
@@ -513,9 +506,9 @@ protected function configureDatagridFilters(DatagridMapper $filter): void
513506
'row_attr' => ['class' => 'form-floating'],
514507
'placeholder' => 'empty_option',
515508
'choices' => [
516-
PageInterface::STATUS_DRAFT => PageInterface::STATUS_DRAFT,
517-
PageInterface::STATUS_REVIEW => PageInterface::STATUS_REVIEW,
518-
PageInterface::STATUS_PUBLISHED => PageInterface::STATUS_PUBLISHED,
509+
VersionableInterface::STATUS_DRAFT => VersionableInterface::STATUS_DRAFT,
510+
VersionableInterface::STATUS_REVIEW => VersionableInterface::STATUS_REVIEW,
511+
VersionableInterface::STATUS_PUBLISHED => VersionableInterface::STATUS_PUBLISHED,
519512
],
520513
'translation_domain' => $this->getTranslationDomain(),
521514
],
@@ -535,12 +528,9 @@ public function configureDefaultFilterValues(array &$filterValues): void
535528
];
536529
}
537530

538-
/**
539-
* @return bool
540-
*/
541-
public function matchPath(ProxyQuery $ProxyQuery, $alias, $field, $data)
531+
public function matchPath(ProxyQuery $ProxyQuery, $alias, $field, $data): bool
542532
{
543-
if (!$data || !$data instanceof FilterData || !$data->hasValue()) {
533+
if (!$data instanceof FilterData || !$data->hasValue()) {
544534
return false;
545535
}
546536
$value = trim((string) $data->getValue());
@@ -582,12 +572,7 @@ public function configureQuery(ProxyQueryInterface $query
582572
return $query;
583573
}
584574

585-
/**
586-
* @param FilterData $data
587-
*
588-
* @return bool
589-
*/
590-
public function getByLocale(ProxyQuery $ProxyQuery, $alias, $field, $data)
575+
public function getByLocale(ProxyQueryInterface $ProxyQuery, $alias, $field, $data): bool
591576
{
592577
if (is_array($data)) {
593578
$data = FilterData::fromArray($data);
@@ -596,8 +581,6 @@ public function getByLocale(ProxyQuery $ProxyQuery, $alias, $field, $data)
596581
if (!$locale = $data->getValue()) {
597582
$locale = $this->getDefaultLocale();
598583
}
599-
600-
$alias = $ProxyQuery->getRootAlias();
601584
$ProxyQuery->andWhere(sprintf('%s.%s = :locale', $alias, $field));
602585
$ProxyQuery->orderBy(sprintf('%s.path', $alias), 'asc');
603586
$ProxyQuery->setParameter(':locale', $locale);
@@ -674,10 +657,7 @@ protected function configureListFields(ListMapper $list): void
674657
}
675658
}
676659

677-
/**
678-
* @return \Doctrine\Common\Collections\ArrayCollection
679-
*/
680-
public function getTranslationLanguages()
660+
public function getTranslationLanguages(): ArrayCollection
681661
{
682662
$translationLanguages = new ArrayCollection();
683663

@@ -730,10 +710,8 @@ public function getTranslationLanguages()
730710
/**
731711
* Get the page templates from the configuration and create an array to use in the
732712
* choice field in the admin form.
733-
*
734-
* @return array
735713
*/
736-
protected function getPageTemplates()
714+
protected function getPageTemplates(): array
737715
{
738716
$choices = [];
739717

@@ -746,25 +724,22 @@ protected function getPageTemplates()
746724

747725
/**
748726
* If there is an object get the object template variable, else get first in template array.
749-
*
750-
* @return string
751727
*/
752-
protected function getDefaultTemplate()
728+
protected function getDefaultTemplate(): string
753729
{
754730
if ($this->hasObject()) {
755731
return $this->getSubject()->getTemplateName();
756732
}
757-
$defaultTemplate = array_key_first($this->pageTemplates);
758733

759-
return $defaultTemplate;
734+
return array_key_first($this->pageTemplates);
760735
}
761736

762737
/**
763738
* Get the icons which represent the templates.
764739
*
765740
* @return array
766741
*/
767-
protected function getPageTemplateIcons()
742+
protected function getPageTemplateIcons(): array
768743
{
769744
$icons = [];
770745

@@ -775,6 +750,11 @@ protected function getPageTemplateIcons()
775750
return $icons;
776751
}
777752

753+
/**
754+
* @return array|array[]
755+
*
756+
* @throws \JsonException
757+
*/
778758
public function configureBatchActions(array $actions): array
779759
{
780760
unset($actions['delete']);
@@ -817,19 +797,17 @@ public function getExportFormats(): array
817797
* @param PageInterface $object
818798
*
819799
* @return mixed|void
800+
*
801+
* @throws ModelManagerThrowable
820802
*/
821803
public function postRemove(object $object): void
822804
{
823805
$contentRoute = $object->getContentRoute();
824806

825-
try {
826-
$this->getModelManager()->delete($contentRoute);
827-
} catch (\Exception) {
828-
exit;
829-
}
807+
$this->getModelManager()->delete($contentRoute);
830808
}
831809

832-
public function hasObject()
810+
public function hasObject(): bool
833811
{
834812
return $this->hasSubject() && $this->getSubject()->getId();
835813
}
@@ -846,7 +824,7 @@ public function getLastEditedBy(): ?LogEntry
846824
}
847825

848826
$loggableClass = LogEntry::class;
849-
/** @var \Gedmo\Loggable\Entity\Repository\LogEntryRepository $repo */
827+
/** @var LogEntryRepository $repo */
850828
$repo = $this->getModelManager()->getEntityManager($loggableClass)
851829
->getRepository($loggableClass);
852830

src/Component/Routing/Route.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
* For the full copyright and license information, please view the LICENSE
1111
* file that was distributed with this source code.
1212
*/
13+
1314
namespace Networking\InitCmsBundle\Component\Routing;
1415

1516
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
1617
use Symfony\Component\Routing\Route as SymfonyRoute;
1718
use Symfony\Component\Routing\RouteCompiler;
19+
1820
/**
1921
* Dummy Route Class for creating routes on the run. Used when building navigation items.
2022
*
@@ -26,47 +28,35 @@ class Route extends SymfonyRoute implements RouteObjectInterface
2628
{
2729
final public const LOCALE = '_locale';
2830

29-
/**
30-
* @param $pattern
31-
* @param array $defaults
32-
*/
3331
public function __construct($pattern, array $defaults = [])
3432
{
3533
$this->setPath($pattern);
3634
$this->setDefaults($defaults);
3735
$this->setOptions([
38-
'compiler_class' => RouteCompiler::class
36+
'compiler_class' => RouteCompiler::class,
3937
]);
38+
39+
parent::__construct($pattern, $defaults);
4040
}
4141

42-
/**
43-
* @return mixed
44-
*/
4542
public function getRouteKey(): ?string
4643
{
4744
return $this->getPath();
4845
}
4946

50-
51-
5247
/**
5348
* Get the content document this route entry stands for. If non-null,
5449
* the ControllerClassMapper uses it to identify a controller and
5550
* the content is passed to the controller.
5651
*
57-
* If there is no specific content for this url (i.e. its an "application"
52+
* If there is no specific content for this url (i.e. it's an "application"
5853
* page), may return null.
59-
*
60-
* @return object the document or entity this route entry points to
6154
*/
6255
public function getContent(): ?object
6356
{
6457
return $this->getDefault(self::CONTENT_OBJECT);
6558
}
6659

67-
/**
68-
* @return mixed
69-
*/
7060
public function getLocale()
7161
{
7262
return $this->getDefault(self::LOCALE);

0 commit comments

Comments
 (0)