Skip to content

[HTTP Foundation] Setting false as Request::isMethodSafe() first parameter should have been deprecated on 4.1 #31323

@blitux

Description

@blitux

Symfony version(s) affected: symfony/http-foundation v4.1+

Description

On https://github.com/symfony/http-foundation/blob/master/Request.php#L1435-L1452

/**
 * Checks whether or not the method is safe.
 *
 * @see https://tools.ietf.org/html/rfc7231#section-4.2.1
 *
 * @param bool $andCacheable Adds the additional condition that the method should be cacheable. True by default.
 *
 * @return bool
 */
public function isMethodSafe(/* $andCacheable = true */)
{
    if (!\func_num_args() || func_get_arg(0)) {
        // setting $andCacheable to false should be deprecated in 4.1
        throw new \BadMethodCallException('Checking only for cacheable HTTP methods with Symfony\Component\HttpFoundation\Request::isMethodSafe() is not supported.');
    }

    return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
}

It actually throws an exception when no argument is passed. This should have been deprecated on 4.1. Source: symfony/http-foundation@ba431a0

How to reproduce
Create a Symfony\Component\HttpFoundation\Request instance and then call the method isMethodSafe on it with no arguments.

What is expected
It should not throw an exception when calling the method without arguments, and raise an error with E_USER_DEPRECATED code when it's called with arguments.

Possible Solution

    public function isMethodSafe()
    {
        if (\func_num_args() > 0) {
            @trigger_error('Passing arguments to Symfony\Component\HttpFoundation\Request::isMethodSafe() is not supported. To check if method is cacheable, use Symfony\Component\HttpFoundation\Request::isMethodCacheable() instead.', E_USER_DEPRECATED);
        }

        return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions