Marco Borgeaud 6550e65756
Diff blocks: fix some incorrect use for php (#2804)
Improvement identified in #2790.

Add a prefix to the diff-id when it is used multiple times in different
"how to fix it in XYZ" sections to avoid ambiguity and pedantically
follow the spec:

> A single and unique diff-id should be used only once for each type of
code example as shown in the description of a rule.

Obvious typos around `diff-type` were fixed.
2023-08-10 15:57:24 +02:00

49 lines
1.1 KiB
Plaintext

== How to fix it in Symfony
=== Code examples
include::../../common/fix/code-rationale.adoc[]
==== Noncompliant code example
[source,php,diff-id=21,diff-type=noncompliant]
----
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
public function checkCookie(Request $request): Response
{
$response = $this->render('/welcome.html');
if (!$request->cookies->has('PHPSESSID')) {
$value = $request->query->get('cookie');
$cookie = Cookie::create('PHPSESSID', $value);
$response->headers->setCookie($cookie); // Noncompliant
}
return $response;
}
----
==== Compliant solution
[source,php,diff-id=21,diff-type=compliant]
----
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
public function checkCookie(Request $request): Response
{
$response = $this->render('/welcome.html');
if (!$request->cookies->has('PHPSESSID')) {
return $this->redirectToRoute('getcookie');
}
return $response;
}
----
include::../../common/fix/how-does-this-work.adoc[]