symfony/http-kernel · error · TypeError

The value of the "$if" option of the

Error message

The value of the "$if" option of the "%s" attribute must evaluate to a boolean, "%s" given.

What it means

The Cache attribute listener resolves the "$if" option before the controller runs. If "if" is not already a boolean, it is evaluated as an expression; when the evaluation result is not bool, a TypeError is thrown naming the actual type found.

Solutions

  1. Fix the expression so it returns a boolean, e.g. #[Cache(if: "request.headers.has('Authorization')")].
  2. Pass a literal bool to the if option: #[Cache(if: true)].
  3. Wrap the expression in an explicit comparison (a == b) rather than returning a raw value.

Example fix

// before
#[Cache(if: 'request.getMethod()')]
// after
#[Cache(if: "request.getMethod() == 'POST'")]
Defensive patterns

Strategy: type-guard

Validate before calling

$if = $cache->if;
if (!is_bool($if) && !is_bool(@eval || null)) { /* validate expression returns bool before applying attribute */ }

Type guard

function isBoolExpr(mixed $v): bool { return is_bool($v); }

Try / catch

try { $listener->onKernelControllerAttribute($event, 'kernel.controller', ...); } catch (\TypeError $e) { $logger->error('Bad Cache if-expression: '.$e->getMessage()); }

Prevention

When it happens

Trigger: A controller or action annotated with #[Cache(if: ...)] whose expression evaluates to a non-boolean (e.g. a string, int, or null returned by the expression), dispatched via onKernelControllerAttribute/onKernelControllerArguments.

Common situations: Typo in expression referencing an undefined variable that evaluates to null; writing if: '1' instead of a real boolean expression; expression returning the request method string instead of a comparison.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of symfony/http-kernel@aa3a39d728 (2026-09-13). Data as JSON: /api/errors/8ed41d9fea02023e. Report an issue: GitHub.

Appendix: source

Thrown at EventListener/CacheAttributeListener.php:139

                KernelEvents::RESPONSE => ['onKernelResponse', -10],
            ];
        }

        return [
            KernelEvents::CONTROLLER_ARGUMENTS.'.'.Cache::class => 'onKernelControllerAttribute',
            KernelEvents::RESPONSE.'.'.Cache::class => 'onKernelControllerAttribute',
        ];
    }

    public function reset(): void
    {
    }

    private function processAttributeBeforeController(Cache $cache, Request $request, ControllerArgumentsEvent $event): void
    {
        if (!\is_bool($cache->if)) {
            if (!\is_bool($if = $this->evaluate($cache->if, $cache->variables))) {
                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) {
            return;
        }

        $response = null;

        if (null !== $cache->lastModified && !$cache->lastModified instanceof \DateTimeInterface) {
            $lastModified = $this->evaluate($cache->lastModified, $cache->variables);
            ($response ??= new Response())->setLastModified($lastModified);
            $cache->lastModified = $lastModified;
        }

        if (null !== $cache->etag) {

View on GitHub (pinned to aa3a39d728)