Repository: symfony/symfony
File: src/Symfony/Component/HttpKernel/DataCollector/RequestDataCollector.php (PHP)

Code before:

        if (\is_object($controller)) {
            $r = new \ReflectionClass($controller);

            return [
                'class' => $r->getName(),
                'method' => null,
                'file' => $r->getFileName(),
                'line' => $r->getStartLine(),
            ];
        }

        return \is_string($controller) ? $controller : 'n/a';
    }

    public function getCurlCommand(): ?string
    {
        if (!isset($this->data['method']) || !isset($this->data['path_info'])) {
            return null;
        }

        $command = ['curl', '--compressed'];

        // Build the full URL
        $scheme = $this->data['request_server']['HTTPS'] ?? false ? 'https' : 'http';
        $host = $this->data['request_server']['HTTP_HOST'] ?? $this->data['request_server']['SERVER_NAME'] ?? 'localhost';
        $url = $scheme.'://'.$host.$this->data['path_info'];

        if (!empty($this->data['request_query'])) {
            $url .= '?'.http_build_query($this->data['request_query']->getValue());
        }

        // Add HTTP method
        $method = $this->data['method'];
        if (Request::METHOD_GET !== $method) {
            $command[] = \sprintf('--request %s', $method);
        }

        $command[] = \sprintf('--url %s', escapeshellarg($url));

        // Add headers
        foreach ($this->data['request_headers']->getValue() as $name => $value) {
            if (\is_array($value)) {
                $value = implode(', ', $value);
            }

            // Skip certain headers
            if (\in_array(strtolower($name), ['host', 'cookie'])) {
                continue;
            }


Review comment (bug):
This is wrong. For `HEAD`, you **must** use `-I` (or its longhand). See the blog post I linked where the lead developer of curl explains that using `-X` (or its `--request` longhand) can cause curl to hang if used with `HEAD`.
And for `POST`, that should also be unnecessary due to the data options

Code after:
        }

        if (\is_object($controller)) {
            $r = new \ReflectionClass($controller);

            return [
                'class' => $r->getName(),
                'method' => null,
                'file' => $r->getFileName(),
                'line' => $r->getStartLine(),
            ];
        }

        return \is_string($controller) ? $controller : 'n/a';
    }

    private function computeCurlCommand(Request $request, ?string $content): string
    {
        $command = ['curl', '--compressed'];

        $method = $request->getMethod();

        if (Request::METHOD_HEAD === $method) {
            $command[] = '--head';
        } elseif (Request::METHOD_GET !== $method) {
            $command[] = \sprintf('--request %s', $method);
        }

        $command[] = \sprintf('--url %s', escapeshellarg($request->getUri()));

        foreach ($request->headers->all() as $name => $values) {
            if (\in_array(strtolower($name), ['host', 'cookie'], true)) {
                continue;
            }

            $command[] = '--header '.escapeshellarg(ucwords($name, '-').': '.implode(', ', $values));
        }

        if ($request->cookies->all()) {
            $cookies = [];
            foreach ($request->cookies->all() as $name => $value) {
                $cookies[] = urlencode($name).'='.urlencode($value);
            }
            $command[] = '--cookie '.escapeshellarg(implode('; ', $cookies));
        }

        if ($content && \in_array($method, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_DELETE], true)) {
            $command[] = '--data-raw '.$this->escapePayload($content);
        }

        return implode(" \\\n  ", $command);