Completed
Push — instant-search-master ( 95535d...2c1eb1 )
by
unknown
22:37 queued 16:04
created

functions.cookies.php ➔ jetpack_shim_setcookie()   C

Complexity

Conditions 16
Paths 90

Size

Total Lines 50

Duplication

Lines 12
Ratio 24 %

Importance

Changes 0
Metric Value
cc 16
nc 90
nop 3
dl 12
loc 50
rs 5.5666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is meant to be the home for any function handling cookies that can
4
 * be accessed anywhere within Jetpack.
5
 *
6
 * This file is loaded whether or not Jetpack is connected to WP.com.
7
 *
8
 * @package Jetpack
9
 */
10
11
/**
12
 * A PHP 5.X compatible version of the array argument version of PHP 7.3's setcookie().
13
 *
14
 * Useful for setting SameSite cookies in PHP 7.2 or earlier.
15
 *
16
 * @param string $name    Name of the cookie.
17
 * @param string $value   Value of the cookie.
18
 * @param array  $options Options to include with the cookie.
19
 * @return bool False when error happens, other wise true.
20
 */
21
function jetpack_shim_setcookie( $name, $value, $options ) {
22
	$not_allowed_chars = ",; \t\r\n\013\014";
23
24
	if ( strpbrk( $name, $not_allowed_chars ) !== false ) {
25
		return false;
26
	}
27
28
	if ( headers_sent() ) {
29
		return false;
30
	}
31
32
	$cookie = 'Set-Cookie: ' . $name . '=' . rawurlencode( $value ) . '; ';
33
34
	if ( ! empty( $options['expires'] ) ) {
35
		$cookie_date = gmdate( 'D, d M Y H:i:s \G\M\T', $options['expires'] );
36
		$cookie     .= sprintf( 'expires=%s', $cookie_date ) . ';';
37
	}
38
39
	if ( ! empty( $options['secure'] ) && true === $options['secure'] ) {
40
		$cookie .= 'secure; ';
41
	}
42
43
	if ( ! empty( $options['httponly'] ) && true === $options['httponly'] ) {
44
		$cookie .= 'HttpOnly; ';
45
	}
46
47 View Code Duplication
	if ( ! empty( $options['domain'] ) && is_string( $options['domain'] ) ) {
48
		if ( strpbrk( $options['domain'], false !== $not_allowed_chars ) ) {
49
			return false;
50
		}
51
		$cookie .= sprintf( 'domain=%s', $options['domain'] . '; ' );
52
	}
53
54 View Code Duplication
	if ( ! empty( $options['path'] ) && is_string( $options['path'] ) ) {
55
		if ( strpbrk( $options['path'], false !== $not_allowed_chars ) ) {
56
			return false;
57
		}
58
		$cookie .= sprintf( 'path=%s', $options['path'] . '; ' );
59
	}
60
61
	if ( ! empty( $options['samesite'] ) && is_string( $options['samesite'] ) ) {
62
		$cookie .= sprintf( 'SameSite=%s', $options['samesite'] . '; ' );
63
	}
64
65
	$cookie = trim( $cookie );
66
	$cookie = trim( $cookie, ';' );
67
	header( $cookie, false );
68
69
	return true;
70
}
71