Welcome Guest, Not a member yet? Register   Sign In
Improve setCookie function
#1
Star 

Hello folks,

I was reviewing the setCookie function on ResponseTrait.php file. I see an opportunity to use extract function when $name argument is an array.

Here is the small improvement:

Code:
    /**
    * Set a cookie
    *
    * Accepts an arbitrary number of binds (up to 7) or an associative
    * array in the first parameter containing all the values.
    *
    * @param array|Cookie|string $name    Cookie name / array containing binds / Cookie object
    * @param string              $value    Cookie value
    * @param string              $expire  Cookie expiration time in seconds
    * @param string              $domain  Cookie domain (e.g.: '.yourdomain.com')
    * @param string              $path    Cookie path (default: '/')
    * @param string              $prefix  Cookie name prefix ('': the default prefix)
    * @param bool|null          $secure  Whether to only transfer cookies via SSL
    * @param bool|null          $httponly Whether only make the cookie accessible via HTTP (no javascript)
    * @param string|null        $samesite
    *
    * @return $this
    */
    public function setCookie(
        $name,
        $value = '',
        $expire = '',
        $domain = '',
        $path = '/',
        $prefix = '',
        $secure = null,
        $httponly = null,
        $samesite = null
    ) {
        if ($name instanceof Cookie) {
            $this->cookieStore = $this->cookieStore->put($name);

            return $this;
        }

        /** @var CookieConfig|null $cookieConfig */
        $cookieConfig = config(CookieConfig::class);

        if ($cookieConfig instanceof CookieConfig) {
            $secure ??= $cookieConfig->secure;
            $httponly ??= $cookieConfig->httponly;
            $samesite ??= $cookieConfig->samesite;
        }

        if (is_array($name)) {
            $cleaned_args  = array_intersect_key($name, array_flip([
                'name',
                'value',
                'expire',
                'domain',
                'path',
                'prefix',
                'secure',
                'httponly',
                'samesite'
            ]));
            extract($cleaned_args);
        }

        if (is_numeric($expire)) {
            $expire = $expire > 0 ? Time::now()->getTimestamp() + $expire : 0;
        }

        $cookie = new Cookie($name, $value, [
            'expires'  => $expire ?: 0,
            'domain'  => $domain,
            'path'    => $path,
            'prefix'  => $prefix,
            'secure'  => $secure,
            'httponly' => $httponly,
            'samesite' => $samesite ?? '',
        ]);

        $this->cookieStore = $this->cookieStore->put($cookie);

        return $this;
    }

Please let me know if I can open an issue and PR on github to send this little change.
Reply
#2

I don't know that is improved or not.
If it is benchmarked and clearly faster, then I would suggest to send PR.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB