Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Jetpack_Signature often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Signature, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Jetpack_Signature { |
||
| 8 | public $token; |
||
| 9 | public $secret; |
||
| 10 | |||
| 11 | function __construct( $access_token, $time_diff = 0 ) { |
||
| 20 | |||
| 21 | function sign_current_request( $override = array() ) { |
||
| 63 | |||
| 64 | // body_hash v. body-hash is annoying. Refactor to accept an array? |
||
| 65 | function sign_request( $token = '', $timestamp = 0, $nonce = '', $body_hash = '', $method = '', $url = '', $body = null, $verify_body_hash = true ) { |
||
| 66 | if ( !$this->secret ) { |
||
| 67 | return new Jetpack_Error( 'invalid_secret', 'Invalid secret' ); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ( !$this->token ) { |
||
| 71 | return new Jetpack_Error( 'invalid_token', 'Invalid token' ); |
||
| 72 | } |
||
| 73 | |||
| 74 | list( $token ) = explode( '.', $token ); |
||
| 75 | |||
| 76 | if ( 0 !== strpos( $token, "$this->token:" ) ) { |
||
| 77 | return new Jetpack_Error( 'token_mismatch', 'Incorrect token' ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $required_parameters = array( 'token', 'timestamp', 'nonce', 'method', 'url' ); |
||
| 81 | View Code Duplication | if ( !is_null( $body ) ) { |
|
| 82 | $required_parameters[] = 'body_hash'; |
||
| 83 | if ( !is_string( $body ) ) { |
||
| 84 | return new Jetpack_Error( 'invalid_body', 'Body is malformed.' ); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | foreach ( $required_parameters as $required ) { |
||
| 89 | View Code Duplication | if ( !is_scalar( $$required ) ) { |
|
| 90 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', str_replace( '_', '-', $required ) ) ); |
||
| 91 | } |
||
| 92 | |||
| 93 | View Code Duplication | if ( !strlen( $$required ) ) { |
|
| 94 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is missing.', str_replace( '_', '-', $required ) ) ); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if ( is_null( $body ) ) { |
||
| 99 | if ( $body_hash ) { |
||
| 100 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | if ( $verify_body_hash && jetpack_sha1_base64( $body ) !== $body_hash ) { |
||
| 104 | return new Jetpack_Error( 'invalid_body_hash', 'The body hash does not match.' ); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | $parsed = parse_url( $url ); |
||
| 109 | if ( !isset( $parsed['host'] ) ) { |
||
| 110 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'url' ) ); |
||
| 111 | } |
||
| 112 | |||
| 113 | if ( $parsed['host'] === JETPACK__WPCOM_JSON_API_HOST ) { |
||
| 114 | $parsed['host'] = 'public-api.wordpress.com'; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ( !empty( $parsed['port'] ) ) { |
||
| 118 | $port = $parsed['port']; |
||
| 119 | } else { |
||
| 120 | if ( 'http' == $parsed['scheme'] ) { |
||
| 121 | $port = 80; |
||
| 122 | } else if ( 'https' == $parsed['scheme'] ) { |
||
| 123 | $port = 443; |
||
| 124 | } else { |
||
| 125 | return new Jetpack_Error( 'unknown_scheme_port', "The scheme's port is unknown" ); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | if ( !ctype_digit( "$timestamp" ) || 10 < strlen( $timestamp ) ) { // If Jetpack is around in 275 years, you can blame mdawaffe for the bug. |
||
| 130 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'timestamp' ) ); |
||
| 131 | } |
||
| 132 | |||
| 133 | $local_time = $timestamp - $this->time_diff; |
||
| 134 | if ( $local_time < time() - 600 || $local_time > time() + 300 ) { |
||
| 135 | return new Jetpack_Error( 'invalid_signature', 'The timestamp is too old.' ); |
||
| 136 | } |
||
| 137 | |||
| 138 | if ( 12 < strlen( $nonce ) || preg_match( '/[^a-zA-Z0-9]/', $nonce ) ) { |
||
| 139 | return new Jetpack_Error( 'invalid_signature', sprintf( 'The required "%s" parameter is malformed.', 'nonce' ) ); |
||
| 140 | } |
||
| 141 | |||
| 142 | $normalized_request_pieces = array( |
||
| 143 | $token, |
||
| 144 | $timestamp, |
||
| 145 | $nonce, |
||
| 146 | $body_hash, |
||
| 147 | strtoupper( $method ), |
||
| 148 | strtolower( $parsed['host'] ), |
||
| 149 | $port, |
||
| 150 | $parsed['path'], |
||
| 151 | // Normalized Query String |
||
| 152 | ); |
||
| 153 | |||
| 154 | $normalized_request_pieces = array_merge( $normalized_request_pieces, $this->normalized_query_parameters( isset( $parsed['query'] ) ? $parsed['query'] : '' ) ); |
||
| 155 | |||
| 156 | $normalized_request_string = join( "\n", $normalized_request_pieces ) . "\n"; |
||
| 157 | |||
| 158 | return base64_encode( hash_hmac( 'sha1', $normalized_request_string, $this->secret, true ) ); |
||
| 159 | } |
||
| 160 | |||
| 161 | function normalized_query_parameters( $query_string ) { |
||
| 180 | |||
| 181 | function encode_3986( $string ) { |
||
| 185 | |||
| 186 | function join_with_equal_sign( $name, $value ) { |
||
| 189 | } |
||
| 190 | |||
| 194 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.