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

Code before:
    /**
     * @var \SplObjectStorage<Request, string>
     */
    private \SplObjectStorage $etags;

    public function __construct(
        private ?ExpressionLanguage $expressionLanguage = null,
    ) {
        $this->lastModified = new \SplObjectStorage();
        $this->etags = new \SplObjectStorage();
    }

    /**
     * Handles HTTP validation headers.
     */
    public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
    {
        $request = $event->getRequest();

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

            return;
        }

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

        foreach ($attributes as $cache) {
            if (!\is_bool($cache->if)) {
                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)));
                }

                $cache->if = $if;
            }
            if (!$cache->if) {
                continue;
            }

            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)));

Review comment (performance):
Removing this filtering before setting the `_cache` attribute means that the behavior in `onKernelResponse` will not be the same between not having any `Cache` attribute and having only disabled `Cache` attributes, which might be unexpected.

What is this PR actually optimizing ?

Code after:
    /**
     * @var \SplObjectStorage<Request, string>
     */
    private \SplObjectStorage $etags;

    public function __construct(
        private ?ExpressionLanguage $expressionLanguage = null,
    ) {
        $this->lastModified = new \SplObjectStorage();
        $this->etags = new \SplObjectStorage();
    }

    /**
     * Handles HTTP validation headers.
     */
    public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
    {
        $request = $event->getRequest();

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

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

        foreach ($attributes as $cache) {
            if (!\is_bool($cache->if)) {
                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)));
                }

                $cache->if = $if;
            }
            if (!$cache->if) {
                continue;
            }

            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);
            }