Repository: symfony/symfony
File: src/Symfony/Component/HttpKernel/EventListener/CacheAttributeListener.php (PHP)

Code before:

        $attributes = $request->attributes->get('_cache') ?? $event->getAttributes(Cache::class);
        $attributes = array_filter($attributes, function (Cache $cache) use (&$variables, $request, $event) {
            if (null === $cache->if) {
                return true;
            }

            if (!\is_bool($if = $this->evaluate($cache->if, $variables ??= $this->getVariables($request, $event)))) {
                throw new \TypeError(\sprintf('The value of the "$if" option of the "%s" attribute must evaluate to a boolean, "%s" given.', Cache::class, get_debug_type($if)));
            }

            return $if;
        });

        if (!$attributes) {
            $request->attributes->remove('_cache');

            return;
        }

        $request->attributes->set('_cache', $attributes);
        $response = null;
        $lastModified = null;
        $etag = null;

        /** @var Cache[] $attributes */
        foreach ($attributes as $cache) {
            if (null !== $cache->lastModified) {
                $lastModified = $this->evaluate($cache->lastModified, $variables ??= $this->getVariables($request, $event));
                ($response ??= new Response())->setLastModified($lastModified);
            }

            if (null !== $cache->etag) {
                $etag = hash('sha256', $this->evaluate($cache->etag, $variables ??= $this->getVariables($request, $event)));
                ($response ??= new Response())->setEtag($etag);
            }
        }

        if ($response?->isNotModified($request)) {
            $event->setController(static fn () => $response);
            $event->stopPropagation();

            return;
        }

        if (null !== $etag) {
            $this->etags[$request] = $etag;
        }
        if (null !== $lastModified) {
            $this->lastModified[$request] = $lastModified;
        }

Review comment (suggestion):
this would be better placed on the definition of the `$attributes` variable actually.

Code after:
        /** @var Cache[] $attributes */
        if (!$attributes = $request->attributes->get('_cache') ?? $event->getAttributes(Cache::class)) {
            return;
        }

        $variables = null;
        $attributes = array_filter($attributes, function (Cache $cache) use (&$variables, $request, $event) {
            if (null === $cache->if) {
                return true;
            }

            if (!\is_bool($if = $this->evaluate($cache->if, $variables ??= $this->getVariables($request, $event)))) {
                throw new \TypeError(\sprintf('The value of the "$if" option of the "%s" attribute must evaluate to a boolean, "%s" given.', Cache::class, get_debug_type($if)));
            }

            return $if;
        });

        if (!$attributes) {
            $request->attributes->remove('_cache');

            return;
        }

        $request->attributes->set('_cache', $attributes);
        $response = null;
        $lastModified = null;
        $etag = null;

        foreach ($attributes as $cache) {
            if (null !== $cache->lastModified) {
                $lastModified = $this->evaluate($cache->lastModified, $variables ??= $this->getVariables($request, $event));
                ($response ??= new Response())->setLastModified($lastModified);
            }

            if (null !== $cache->etag) {
                $etag = hash('sha256', $this->evaluate($cache->etag, $variables ??= $this->getVariables($request, $event)));
                ($response ??= new Response())->setEtag($etag);
            }
        }

        if ($response?->isNotModified($request)) {
            $event->setController(static fn () => $response);
            $event->stopPropagation();

            return;
        }

        if (null !== $etag) {
            $this->etags[$request] = $etag;
        }