| Conditions | 16 |
| Paths | 90 |
| Total Lines | 50 |
| Lines | 12 |
| Ratio | 24 % |
| 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 |
||
| 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 |