| Conditions | 19 |
| Paths | 2040 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 54 | function spip_setcookie($name = '', $value = '', $options = []) { |
||
| 55 | static $to_secure_list = ['spip_session']; |
||
| 56 | |||
| 57 | if (!is_array($options)) { |
||
| 58 | // anciens paramètres : |
||
| 59 | # spip_setcookie($name = '', $value = '', $expire = 0, $path = 'AUTO', $domain = '', $secure = '') |
||
| 60 | $opt = func_get_args(); |
||
| 61 | $opt = array_slice($opt, 2); |
||
| 62 | $options = []; # /!\ après le func_get_args (sinon $opt[0] référence la nouvelle valeur de $options !); |
||
| 63 | if (isset($opt[0])) { |
||
| 64 | $options['expires'] = $opt[0]; |
||
| 65 | } |
||
| 66 | if (isset($opt[1])) { |
||
| 67 | $options['path'] = $opt[1]; |
||
| 68 | } |
||
| 69 | if (isset($opt[2])) { |
||
| 70 | $options['domain'] = $opt[2]; |
||
| 71 | } |
||
| 72 | if (isset($opt[3])) { |
||
| 73 | $options['secure'] = $opt[3]; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $name = preg_replace('/^spip_/', $GLOBALS['cookie_prefix'] . '_', $name); |
||
| 78 | |||
| 79 | // expires |
||
| 80 | if (!isset($options['expires'])) { |
||
| 81 | $options['expires'] = 0; |
||
| 82 | } |
||
| 83 | if (!isset($options['path']) or $options['path'] === 'AUTO') { |
||
| 84 | if (defined('_COOKIE_PATH')) { |
||
| 85 | $options['path'] = _COOKIE_PATH; |
||
| 86 | } else { |
||
| 87 | $options['path'] = preg_replace(',^\w+://[^/]*,', '', url_de_base()); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | if (empty($options['domain']) and defined('_COOKIE_DOMAIN') and _COOKIE_DOMAIN) { |
||
| 91 | $options['domain'] = _COOKIE_DOMAIN; |
||
| 92 | } |
||
| 93 | if (in_array($name, $to_secure_list)) { |
||
| 94 | if (empty($options['secure']) and defined('_COOKIE_SECURE') and _COOKIE_SECURE) { |
||
| 95 | $options['secure'] = true; |
||
| 96 | } |
||
| 97 | if (empty($options['httponly'])) { |
||
| 98 | $options['httponly'] = true; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | if (empty($options['samesite'])) { |
||
| 102 | $options['samesite'] = 'Lax'; |
||
| 103 | } |
||
| 104 | |||
| 105 | #spip_log("cookie('$name', '$value', " . json_encode($options, true) . ")", "cookies"); |
||
| 106 | $a = @setcookie($name, $value, $options); |
||
| 107 | |||
| 108 | spip_cookie_envoye(true); |
||
|
|
|||
| 109 | |||
| 110 | return $a; |
||
| 111 | } |
||
| 112 | |||
| 199 |