Skip to content

Commit 7c931cf

Browse files
authored
IBX-9727: Added type-hints and adapted codebase to PHP8+ within src/bundle (#1672)
* IBX-9727: Added type-hints and adapted codebase to PHP8+ within `src/bundle` * rebranded translation key
1 parent 32730bb commit 7c931cf

File tree

98 files changed

+1486
-3179
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1486
-3179
lines changed

phpstan-baseline.neon

Lines changed: 87 additions & 531 deletions
Large diffs are not rendered by default.

src/bundle/Command/CompileAssetsCommand.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
name: self::COMMAND_NAME,
2222
description: 'Compiles all assets using Webpack Encore'
2323
)]
24-
class CompileAssetsCommand extends Command
24+
final class CompileAssetsCommand extends Command
2525
{
26-
public const COMMAND_NAME = 'ibexa:encore:compile';
27-
public const COMMAND_DEFAULT_TIMEOUT = 300;
26+
public const string COMMAND_NAME = 'ibexa:encore:compile';
27+
public const int COMMAND_DEFAULT_TIMEOUT = 300;
2828

29-
private int $timeout;
30-
31-
public function __construct(int $timeout = self::COMMAND_DEFAULT_TIMEOUT)
32-
{
33-
$this->timeout = $timeout;
29+
public function __construct(
30+
private readonly int $timeout = self::COMMAND_DEFAULT_TIMEOUT
31+
) {
3432
parent::__construct();
3533
}
3634

@@ -96,11 +94,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9694
if (!empty($frontendConfigsName)) {
9795
$frontendConfigsNameArr = explode(',', $frontendConfigsName);
9896
$yarnEncoreCommand = implode(' && ', array_map(
99-
fn (string $configName) => "{$yarnBaseEncoreCommand} --config {$this->getFrontendConfigPath($configName)}",
97+
fn (string $configName): string => "{$yarnBaseEncoreCommand} --config {$this->getFrontendConfigPath($configName)}",
10098
$frontendConfigsNameArr
10199
));
102100
}
103101

102+
/** @var \Symfony\Component\Console\Helper\DebugFormatterHelper $debugFormatter */
104103
$debugFormatter = $this->getHelper('debug_formatter');
105104

106105
$process = Process::fromShellCommandline(
@@ -138,6 +137,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
138137
)
139138
);
140139

141-
return $process->getExitCode();
140+
return $process->getExitCode() ?? Command::FAILURE;
142141
}
143142
}

src/bundle/Controller/ApplicationConfigController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414

1515
final class ApplicationConfigController extends Controller
1616
{
17-
private Aggregator $aggregator;
18-
19-
public function __construct(Aggregator $aggregator)
17+
public function __construct(private readonly Aggregator $aggregator)
2018
{
21-
$this->aggregator = $aggregator;
2219
}
2320

2421
public function loadConfigAction(): ApplicationConfig

src/bundle/Controller/AssetController.php

Lines changed: 49 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Ibexa\AdminUi\Form\Data\Asset\ImageAssetUploadData;
1313
use Ibexa\Contracts\AdminUi\Controller\Controller;
1414
use Ibexa\Core\FieldType\Image\Value as ImageValue;
15-
use Ibexa\Core\FieldType\ImageAsset\AssetMapper;
1615
use Ibexa\Core\FieldType\ImageAsset\AssetMapper as ImageAssetMapper;
1716
use JMS\TranslationBundle\Annotation\Desc;
1817
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -24,92 +23,69 @@
2423
use Symfony\Component\Validator\Validator\ValidatorInterface;
2524
use Symfony\Contracts\Translation\TranslatorInterface;
2625

27-
class AssetController extends Controller
26+
final class AssetController extends Controller
2827
{
29-
public const CSRF_TOKEN_HEADER = 'X-CSRF-Token';
28+
public const string CSRF_TOKEN_HEADER = 'X-CSRF-Token';
3029

31-
public const LANGUAGE_CODE_KEY = 'languageCode';
32-
public const FILE_KEY = 'file';
30+
public const string LANGUAGE_CODE_KEY = 'languageCode';
31+
public const string FILE_KEY = 'file';
3332

34-
private ValidatorInterface $validator;
35-
36-
private CsrfTokenManagerInterface $csrfTokenManager;
37-
38-
private AssetMapper $imageAssetMapper;
39-
40-
private TranslatorInterface $translator;
41-
42-
/**
43-
* @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
44-
* @param \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface $csrfTokenManager
45-
* @param \Ibexa\Core\FieldType\ImageAsset\AssetMapper $imageAssetMapper
46-
* @param \Symfony\Contracts\Translation\TranslatorInterface $translator
47-
*/
4833
public function __construct(
49-
ValidatorInterface $validator,
50-
CsrfTokenManagerInterface $csrfTokenManager,
51-
ImageAssetMapper $imageAssetMapper,
52-
TranslatorInterface $translator
34+
private readonly ValidatorInterface $validator,
35+
private readonly CsrfTokenManagerInterface $csrfTokenManager,
36+
private readonly ImageAssetMapper $imageAssetMapper,
37+
private readonly TranslatorInterface $translator
5338
) {
54-
$this->validator = $validator;
55-
$this->csrfTokenManager = $csrfTokenManager;
56-
$this->imageAssetMapper = $imageAssetMapper;
57-
$this->translator = $translator;
5839
}
5940

60-
/**
61-
* @param \Symfony\Component\HttpFoundation\Request $request
62-
*
63-
* @return \Symfony\Component\HttpFoundation\Response
64-
*
65-
* @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
66-
*/
6741
public function uploadImageAction(Request $request): Response
6842
{
69-
if ($this->isValidCsrfToken($request)) {
70-
$data = new ImageAssetUploadData(
71-
$request->files->get(self::FILE_KEY),
72-
$request->request->get(self::LANGUAGE_CODE_KEY)
73-
);
74-
75-
$errors = $this->validator->validate($data);
76-
if ($errors->count() === 0) {
77-
try {
78-
$file = $data->getFile();
79-
80-
$content = $this->imageAssetMapper->createAsset(
81-
$file->getClientOriginalName(),
82-
new ImageValue([
83-
'inputUri' => $file->getRealPath(),
84-
'fileSize' => $file->getSize(),
85-
'fileName' => $file->getClientOriginalName(),
86-
'alternativeText' => $file->getClientOriginalName(),
87-
]),
88-
$data->getLanguageCode()
89-
);
90-
91-
return new JsonResponse([
92-
'destinationContent' => [
93-
'id' => $content->contentInfo->id,
94-
'name' => $content->getName(),
95-
'locationId' => $content->contentInfo->mainLocationId,
96-
],
97-
'value' => $this->imageAssetMapper->getAssetValue($content),
98-
]);
99-
} catch (Exception $e) {
100-
return $this->createGenericErrorResponse($e->getMessage());
43+
if (!$this->isValidCsrfToken($request)) {
44+
return $this->createInvalidCsrfResponse();
45+
}
46+
47+
$data = new ImageAssetUploadData(
48+
$request->files->get(self::FILE_KEY),
49+
$request->request->get(self::LANGUAGE_CODE_KEY)
50+
);
51+
52+
$errors = $this->validator->validate($data);
53+
if ($errors->count() === 0) {
54+
try {
55+
$file = $data->getFile();
56+
if ($file === null) {
57+
throw new Exception('File is missing in the request.');
10158
}
102-
} else {
103-
return $this->createInvalidInputResponse($errors);
59+
60+
$content = $this->imageAssetMapper->createAsset(
61+
$file->getClientOriginalName(),
62+
new ImageValue([
63+
'inputUri' => $file->getRealPath(),
64+
'fileSize' => $file->getSize(),
65+
'fileName' => $file->getClientOriginalName(),
66+
'alternativeText' => $file->getClientOriginalName(),
67+
]),
68+
$data->getLanguageCode() ?? ''
69+
);
70+
71+
$contentInfo = $content->getContentInfo();
72+
73+
return new JsonResponse([
74+
'destinationContent' => [
75+
'id' => $contentInfo->getId(),
76+
'name' => $content->getName(),
77+
'locationId' => $contentInfo->getMainLocationId(),
78+
],
79+
'value' => $this->imageAssetMapper->getAssetValue($content),
80+
]);
81+
} catch (Exception $e) {
82+
return $this->createGenericErrorResponse($e->getMessage());
10483
}
84+
} else {
85+
return $this->createInvalidInputResponse($errors);
10586
}
106-
107-
return $this->createInvalidCsrfResponse();
10887
}
10988

110-
/**
111-
* @return \Symfony\Component\HttpFoundation\JsonResponse
112-
*/
11389
private function createInvalidCsrfResponse(): JsonResponse
11490
{
11591
$errorMessage = $this->translator->trans(
@@ -122,11 +98,6 @@ private function createInvalidCsrfResponse(): JsonResponse
12298
return $this->createGenericErrorResponse($errorMessage);
12399
}
124100

125-
/**
126-
* @param \Symfony\Component\Validator\ConstraintViolationListInterface $errors
127-
*
128-
* @return \Symfony\Component\HttpFoundation\JsonResponse
129-
*/
130101
private function createInvalidInputResponse(ConstraintViolationListInterface $errors): JsonResponse
131102
{
132103
$errorMessages = [];
@@ -137,11 +108,6 @@ private function createInvalidInputResponse(ConstraintViolationListInterface $er
137108
return $this->createGenericErrorResponse(implode(', ', $errorMessages));
138109
}
139110

140-
/**
141-
* @param string $errorMessage
142-
*
143-
* @return \Symfony\Component\HttpFoundation\JsonResponse
144-
*/
145111
private function createGenericErrorResponse(string $errorMessage): JsonResponse
146112
{
147113
return new JsonResponse(
@@ -154,11 +120,6 @@ private function createGenericErrorResponse(string $errorMessage): JsonResponse
154120
);
155121
}
156122

157-
/**
158-
* @param \Symfony\Component\HttpFoundation\Request $request
159-
*
160-
* @return bool
161-
*/
162123
private function isValidCsrfToken(Request $request): bool
163124
{
164125
$csrfTokenValue = $request->headers->get(self::CSRF_TOKEN_HEADER);

src/bundle/Controller/BookmarkController.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,18 @@
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpFoundation\Response;
2525

26-
class BookmarkController extends Controller
26+
final class BookmarkController extends Controller
2727
{
28-
private BookmarkService $bookmarkService;
29-
30-
private DatasetFactory $datasetFactory;
31-
32-
private FormFactory $formFactory;
33-
34-
private LocationService $locationService;
35-
36-
private SubmitHandler $submitHandler;
37-
38-
private ConfigResolverInterface $configResolver;
39-
4028
public function __construct(
41-
BookmarkService $bookmarkService,
42-
DatasetFactory $datasetFactory,
43-
FormFactory $formFactory,
44-
LocationService $locationService,
45-
SubmitHandler $submitHandler,
46-
ConfigResolverInterface $configResolver
29+
private readonly BookmarkService $bookmarkService,
30+
private readonly DatasetFactory $datasetFactory,
31+
private readonly FormFactory $formFactory,
32+
private readonly LocationService $locationService,
33+
private readonly SubmitHandler $submitHandler,
34+
private readonly ConfigResolverInterface $configResolver
4735
) {
48-
$this->bookmarkService = $bookmarkService;
49-
$this->datasetFactory = $datasetFactory;
50-
$this->formFactory = $formFactory;
51-
$this->locationService = $locationService;
52-
$this->submitHandler = $submitHandler;
53-
$this->configResolver = $configResolver;
5436
}
5537

56-
/**
57-
* @param \Symfony\Component\HttpFoundation\Request $request
58-
*
59-
* @return \Symfony\Component\HttpFoundation\Response
60-
*/
6138
public function listAction(Request $request): Response
6239
{
6340
/** @phpstan-var int<0, max> $page */
@@ -67,15 +44,19 @@ public function listAction(Request $request): Response
6744
new BookmarkAdapter($this->bookmarkService, $this->datasetFactory)
6845
);
6946

70-
$pagerfanta->setMaxPerPage($this->configResolver->getParameter('pagination.bookmark_limit'));
47+
$pagerfanta->setMaxPerPage(
48+
$this->configResolver->getParameter('pagination.bookmark_limit')
49+
);
7150
$pagerfanta->setCurrentPage(min($page, $pagerfanta->getNbPages()));
7251

7352
$editForm = $this->formFactory->contentEdit(
7453
new ContentEditData()
7554
);
7655

7756
$removeBookmarkForm = $this->formFactory->removeBookmark(
78-
new BookmarkRemoveData($this->getChoices(iterator_to_array($pagerfanta->getCurrentPageResults())))
57+
new BookmarkRemoveData(
58+
$this->getChoices(iterator_to_array($pagerfanta->getCurrentPageResults()))
59+
)
7960
);
8061

8162
return $this->render(
@@ -88,11 +69,6 @@ public function listAction(Request $request): Response
8869
);
8970
}
9071

91-
/**
92-
* @param \Symfony\Component\HttpFoundation\Request $request
93-
*
94-
* @return \Symfony\Component\HttpFoundation\Response
95-
*/
9672
public function removeAction(Request $request): Response
9773
{
9874
$form = $this->formFactory->removeBookmark(
@@ -101,15 +77,18 @@ public function removeAction(Request $request): Response
10177
$form->handleRequest($request);
10278

10379
if ($form->isSubmitted()) {
104-
$result = $this->submitHandler->handle($form, function (BookmarkRemoveData $data): RedirectResponse {
105-
foreach ($data->getBookmarks() as $locationId => $selected) {
106-
$this->bookmarkService->deleteBookmark(
107-
$this->locationService->loadLocation($locationId)
108-
);
80+
$result = $this->submitHandler->handle(
81+
$form,
82+
function (BookmarkRemoveData $data): RedirectResponse {
83+
foreach ($data->getBookmarks() as $locationId => $selected) {
84+
$this->bookmarkService->deleteBookmark(
85+
$this->locationService->loadLocation($locationId)
86+
);
87+
}
88+
89+
return $this->redirectToRoute('ibexa.bookmark.list');
10990
}
110-
111-
return $this->redirectToRoute('ibexa.bookmark.list');
112-
});
91+
);
11392

11493
if ($result instanceof Response) {
11594
return $result;

0 commit comments

Comments
 (0)