Conditions | 16 |
Paths | 60 |
Total Lines | 96 |
Code Lines | 52 |
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 |
||
43 | function authorize( $data = array() ) { |
||
44 | $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; |
||
|
|||
45 | |||
46 | $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); |
||
47 | // Checking if site has been active/connected previously before recording unique connection |
||
48 | if ( ! $jetpack_unique_connection ) { |
||
49 | // jetpack_unique_connection option has never been set |
||
50 | $jetpack_unique_connection = array( |
||
51 | 'connected' => 0, |
||
52 | 'disconnected' => 0, |
||
53 | 'version' => '3.6.1', |
||
54 | ); |
||
55 | |||
56 | update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); |
||
57 | |||
58 | //track unique connection |
||
59 | $jetpack = $this->get_jetpack();; |
||
60 | |||
61 | $jetpack->stat( 'connections', 'unique-connection' ); |
||
62 | $jetpack->do_stats( 'server_side' ); |
||
63 | } |
||
64 | |||
65 | // increment number of times connected |
||
66 | $jetpack_unique_connection['connected'] += 1; |
||
67 | Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); |
||
68 | |||
69 | $role = Jetpack::translate_current_user_to_role(); |
||
70 | |||
71 | if ( ! $role ) { |
||
72 | return new Jetpack_Error( 'no_role', 'Invalid request.', 400 ); |
||
73 | } |
||
74 | |||
75 | $cap = Jetpack::translate_role_to_cap( $role ); |
||
76 | if ( ! $cap ) { |
||
77 | return new Jetpack_Error( 'no_cap', 'Invalid request.', 400 ); |
||
78 | } |
||
79 | |||
80 | if ( ! empty( $data['error'] ) ) { |
||
81 | return new Jetpack_Error( $data['error'], 'Error included in the request.', 400 ); |
||
82 | } |
||
83 | |||
84 | if ( ! isset( $data['state'] ) ) { |
||
85 | return new Jetpack_Error( 'no_state', 'Request must include state.', 400 ); |
||
86 | } |
||
87 | |||
88 | if ( ! ctype_digit( $data['state'] ) ) { |
||
89 | return new Jetpack_Error( $data['error'], 'State must be an integer.', 400 ); |
||
90 | } |
||
91 | |||
92 | $current_user_id = get_current_user_id(); |
||
93 | if ( $current_user_id != $data['state'] ) { |
||
94 | return new Jetpack_Error( 'wrong_state', 'State does not match current user.', 400 ); |
||
95 | } |
||
96 | |||
97 | if ( empty( $data['code'] ) ) { |
||
98 | return new Jetpack_Error( 'no_code', 'Request must include an authorization code.', 400 ); |
||
99 | } |
||
100 | |||
101 | $token = $this->get_token( $data ); |
||
102 | |||
103 | if ( is_wp_error( $token ) ) { |
||
104 | $code = $token->get_error_code(); |
||
105 | if ( empty( $code ) ) { |
||
106 | $code = 'invalid_token'; |
||
107 | } |
||
108 | return new Jetpack_Error( $code, $token->get_error_message(), 400 ); |
||
109 | } |
||
110 | |||
111 | if ( ! $token ) { |
||
112 | return new Jetpack_Error( 'no_token', 'Error generating token.', 400 ); |
||
113 | } |
||
114 | |||
115 | $is_master_user = ! Jetpack::is_active(); |
||
116 | |||
117 | Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); |
||
118 | |||
119 | if ( ! $is_master_user ) { |
||
120 | // Don't activate anything since we are just connecting a user. |
||
121 | return 'linked'; |
||
122 | } |
||
123 | |||
124 | $redirect_on_activation_error = ( 'client' === $data['auth_type'] ) ? true : false; |
||
125 | if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { |
||
126 | Jetpack::delete_active_modules(); |
||
127 | |||
128 | Jetpack::activate_default_modules( 999, 1, $active_modules, $redirect_on_activation_error ); |
||
129 | } else { |
||
130 | Jetpack::activate_default_modules( false, false, array(), $redirect_on_activation_error ); |
||
131 | } |
||
132 | |||
133 | // Start nonce cleaner |
||
134 | wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); |
||
135 | wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); |
||
136 | |||
137 | return 'authorized'; |
||
138 | } |
||
139 | |||
279 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.