Conditions | 15 |
Paths | 289 |
Total Lines | 64 |
Code Lines | 32 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
33 | function xoops_setcookie( |
||
34 | string $name, |
||
35 | ?string $value = '', |
||
36 | int $expire = 0, |
||
37 | string $path = '/', |
||
38 | string $domain = '', |
||
39 | ?bool $secure = null, |
||
40 | bool $httponly = true, |
||
41 | string $samesite = 'Lax' |
||
42 | ): bool { |
||
43 | if (headers_sent()) { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | // Convert null values to empty string for compatibility with setcookie. |
||
48 | $value = $value ?? ''; |
||
49 | $host = parse_url(XOOPS_URL, PHP_URL_HOST); |
||
50 | if (!is_string($host)) { |
||
|
|||
51 | $host = ''; // Fallback for invalid XOOPS_URL |
||
52 | } |
||
53 | |||
54 | // Validate the domain BEFORE using it. |
||
55 | if (class_exists('\Xoops\RegDom\RegisteredDomain')) { |
||
56 | if (!\Xoops\RegDom\RegisteredDomain::domainMatches($host, $domain)) { |
||
57 | $originalDomain = $domain; |
||
58 | $domain = ''; // Auto-correct to a safe, host-only cookie |
||
59 | |||
60 | if (defined('XOOPS_DEBUG_MODE') && XOOPS_DEBUG_MODE) { |
||
61 | error_log( |
||
62 | sprintf( |
||
63 | '[XOOPS Cookie] Invalid domain "%s" for host "%s" (cookie: %s) - using host-only.', |
||
64 | $originalDomain, |
||
65 | $host, |
||
66 | $name |
||
67 | ) |
||
68 | ); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | // Auto-detect 'secure' flag if not explicitly set |
||
74 | if ($secure === null) { |
||
75 | $secure = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') |
||
76 | || (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) |
||
77 | || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https'); |
||
78 | } |
||
79 | |||
80 | // Use modern array syntax for PHP 7.3+ |
||
81 | if (PHP_VERSION_ID >= 70300) { |
||
82 | $options = [ |
||
83 | 'expires' => $expire, |
||
84 | 'path' => $path, |
||
85 | 'secure' => $secure, |
||
86 | 'httponly' => $httponly, |
||
87 | 'samesite' => $samesite, |
||
88 | ]; |
||
89 | if ($domain !== '') { |
||
90 | $options['domain'] = $domain; |
||
91 | } |
||
92 | return setcookie($name, $value, $options); |
||
93 | } |
||
94 | |||
95 | // Fallback for older PHP versions |
||
96 | return setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
||
97 | } |
||
144 |