Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some method of the CRUDController final #7252

Merged
merged 3 commits into from
Jun 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 34 additions & 40 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,10 +950,8 @@ final public function configureAdmin(Request $request): void
*
* @param string $view The view name
* @param array<string, mixed> $parameters An array of parameters to pass to the view
*
* @return Response A Response instance
VincentLanglet marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function renderWithExtraParams(string $view, array $parameters = [], ?Response $response = null): Response
final protected function renderWithExtraParams(string $view, array $parameters = [], ?Response $response = null): Response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we add @final in 3.x to these methods to warn users?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do. I prefer to first decide what should be final.

{
return $this->render($view, $this->addRenderExtraParams($parameters), $response);
}
Expand All @@ -972,14 +970,10 @@ protected function addRenderExtraParams(array $parameters = []): array
}

/**
* Render JSON.
*
* @param mixed $data
* @param mixed[] $headers
*
* @return JsonResponse with json encoded data
*/
protected function renderJson($data, int $status = Response::HTTP_OK, array $headers = []): JsonResponse
final protected function renderJson($data, int $status = Response::HTTP_OK, array $headers = []): JsonResponse
{
return new JsonResponse($data, $status, $headers);
}
Expand All @@ -989,7 +983,7 @@ protected function renderJson($data, int $status = Response::HTTP_OK, array $hea
*
* @return bool True if the request is an XMLHttpRequest, false otherwise
*/
protected function isXmlHttpRequest(Request $request): bool
final protected function isXmlHttpRequest(Request $request): bool
{
return $request->isXmlHttpRequest() || $request->get('_xml_http_request');
}
Expand Down Expand Up @@ -1032,17 +1026,17 @@ protected function getBaseTemplate(): string
/**
* @throws \Exception
*/
protected function handleModelManagerException(\Exception $e): void
protected function handleModelManagerException(\Exception $exception): void
{
if ($this->getParameter('kernel.debug')) {
throw $e;
throw $exception;
}

$context = ['exception' => $e];
if (null !== $e->getPrevious()) {
$context['previous_exception_message'] = $e->getPrevious()->getMessage();
$context = ['exception' => $exception];
if (null !== $exception->getPrevious()) {
$context['previous_exception_message'] = $exception->getPrevious()->getMessage();
}
$this->getLogger()->error($e->getMessage(), $context);
$this->getLogger()->error($exception->getMessage(), $context);
}

/**
Expand Down Expand Up @@ -1111,15 +1105,15 @@ final protected function redirectToList(): RedirectResponse
/**
* Returns true if the preview is requested to be shown.
*/
protected function isPreviewRequested(Request $request): bool
final protected function isPreviewRequested(Request $request): bool
{
return null !== $request->get('btn_preview');
}

/**
* Returns true if the preview has been approved.
*/
protected function isPreviewApproved(Request $request): bool
final protected function isPreviewApproved(Request $request): bool
{
return null !== $request->get('btn_preview_approve');
}
Expand All @@ -1130,7 +1124,7 @@ protected function isPreviewApproved(Request $request): bool
* That means either a preview is requested or the preview has already been shown
* and it got approved/declined.
*/
protected function isInPreviewMode(Request $request): bool
final protected function isInPreviewMode(Request $request): bool
{
return $this->admin->supportsPreviewMode()
&& ($this->isPreviewRequested($request)
Expand All @@ -1141,7 +1135,7 @@ protected function isInPreviewMode(Request $request): bool
/**
* Returns true if the preview has been declined.
*/
protected function isPreviewDeclined(Request $request): bool
final protected function isPreviewDeclined(Request $request): bool
{
return null !== $request->get('btn_preview_decline');
}
Expand Down Expand Up @@ -1202,7 +1196,7 @@ protected function getAclRoles(): \Traversable
*
* @throws HttpException
*/
protected function validateCsrfToken(Request $request, string $intention): void
final protected function validateCsrfToken(Request $request, string $intention): void
{
if (!$this->has('security.csrf.token_manager')) {
return;
Expand All @@ -1220,15 +1214,15 @@ protected function validateCsrfToken(Request $request, string $intention): void
/**
* Escape string for html output.
*/
protected function escapeHtml(string $s): string
final protected function escapeHtml(string $s): string
{
return htmlspecialchars($s, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
return htmlspecialchars($s, \ENT_QUOTES | \ENT_SUBSTITUTE);
}

/**
* Get CSRF token.
*/
protected function getCsrfToken(string $intention): ?string
final protected function getCsrfToken(string $intention): ?string
{
if (!$this->has('security.csrf.token_manager')) {
return null;
Expand Down Expand Up @@ -1371,15 +1365,30 @@ final protected function assertObjectExists(Request $request, bool $strict = fal
/**
* @phpstan-return array{_tab?: string}
*/
private function getSelectedTab(Request $request): array
final protected function getSelectedTab(Request $request): array
{
return array_filter(['_tab' => (string) $request->request->get('_tab')]);
}

/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*
* @param string[]|null $theme
*/
final protected function setFormTheme(FormView $formView, ?array $theme = null): void
{
$twig = $this->get('twig');
\assert($twig instanceof Environment);
$formRenderer = $twig->getRuntime(FormRenderer::class);
\assert($formRenderer instanceof FormRenderer);

$formRenderer->setTheme($formView, $theme);
}

/**
* @phpstan-param T $object
*/
private function checkParentChildAssociation(Request $request, object $object): void
final protected function checkParentChildAssociation(Request $request, object $object): void
{
if (!$this->admin->isChild()) {
return;
Expand Down Expand Up @@ -1441,19 +1450,4 @@ private function equalsOrContains($haystack, object $needle): bool

return false;
}

/**
* Sets the admin form theme to form view. Used for compatibility between Symfony versions.
*
* @param string[]|null $theme
*/
private function setFormTheme(FormView $formView, ?array $theme = null): void
{
$twig = $this->get('twig');
\assert($twig instanceof Environment);
$formRenderer = $twig->getRuntime(FormRenderer::class);
\assert($formRenderer instanceof FormRenderer);

$formRenderer->setTheme($formView, $theme);
}
}