Completed
Push — fix/display-connection-errors ( 8173c0...d95e4c )
by
unknown
07:21
created

Connection_Error_Handler   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 8.64
c 0
b 0
f 0
wmc 47
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F get_error_message_from_code() 0 108 47

How to fix   Complexity   

Complex Class

Complex classes like Connection_Error_Handler 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 Connection_Error_Handler, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * The Jetpack Connection Error Handler class file.
4
 *
5
 * @package automattic/jetpack-connection
6
 */
7
8
namespace Automattic\Jetpack\Connection;
9
10
/**
11
 * The Jetpack Connection Error Handler class is used to handle the errors
12
 * generated during the connection process.
13
 */
14
class Connection_Error_Handler {
15
16
	/**
17
	 * Returns the detailed error message for an error code.
18
	 *
19
	 * @param string $error_code The error code.
20
	 */
21
	public function get_error_message_from_code( $error_code ) {
22
		$message = '';
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
23
24
		switch ( $error_code ) {
25
			case 'cheatin':
26
				$message = __( "Cheatin' uh?", 'jetpack' );
27
				break;
28
			case 'wrong_state':
29
				$message = __(
30
					'You need to stay logged in to your WordPress blog while you authorize Jetpack.',
31
					'jetpack'
32
				);
33
				break;
34
			case 'invalid_client':
35
				$message = __(
36
					'We had an issue connecting Jetpack. Please deactivate then reactivate the Jetpack plugin and then connect again.',
37
					'jetpack'
38
				);
39
				break;
40
			case 'invalid_grant':
41
				$message = __(
42
					'There was an issue connecting your Jetpack. Please click "Connect to WordPress.com" again.',
43
					'jetpack'
44
				);
45
				break;
46
			case 'access_denied':
47
			case 'site_inaccessible':
48
			case 'site_requires_authorization':
49
				$message = __(
50
					'Your website needs to be publicly accessible to use Jetpack. Please update your website\'s settings and try again',
51
					'jetpack'
52
				);
53
				break;
54
			case 'site_blacklisted':
55
				$message = sprintf(
56
					__(
57
						'This site can\'t be connected to WordPress.com because it violates our  <a %s>Terms of Service</a>.',
58
						'jetpack'
59
					),
60
					'href="https://wordpress.com/tos" rel="noopener noreferrer" target="_blank"'
61
				);
62
				break;
63
			case 'not_public':
64
				$message = __(
65
					'<strong>Your Jetpack has a glitch.</strong> Connecting this site with WordPress.com is not possible. This usually means your site is not publicly accessible (localhost).',
66
					'jetpack'
67
				);
68
				break;
69
			case 'wpcom_408':
70
			case 'wpcom_5??':
71
			case 'wpcom_bad_response':
72
			case 'wpcom_outage':
73
				$message = __(
74
					'WordPress.com is currently having problems and is unable to fuel up your Jetpack.  Please try again later.',
75
					'jetpack'
76
				);
77
				break;
78
			case 'register_http_request_failed':
79
			case 'token_http_request_failed':
80
				$message = __(
81
					'Jetpack could not contact WordPress.com.  This usually means something is incorrectly configured on your web host.',
82
					'jetpack'
83
				);
84
				break;
85
			case 'no_role':
86
			case 'no_cap':
87
			case 'no_code':
88
			case 'no_state':
89
			case 'invalid_state':
90
			case 'invalid_request':
91
			case 'invalid_scope':
92
			case 'unsupported_response_type':
93
			case 'invalid_token':
94
			case 'no_token':
95
			case 'missing_secrets':
96
			case 'home_missing':
97
			case 'siteurl_missing':
98
			case 'gmt_offset_missing':
99
			case 'site_name_missing':
100
			case 'secret_1_missing':
101
			case 'secret_2_missing':
102
			case 'site_lang_missing':
103
			case 'home_malformed':
104
			case 'siteurl_malformed':
105
			case 'gmt_offset_malformed':
106
			case 'timezone_string_malformed':
107
			case 'site_name_malformed':
108
			case 'secret_1_malformed':
109
			case 'secret_2_malformed':
110
			case 'site_lang_malformed':
111
			case 'secrets_mismatch':
112
			case 'verify_secret_1_missing':
113
			case 'verify_secret_1_malformed':
114
			case 'verify_secrets_missing':
115
			case 'verify_secrets_mismatch':
116
			default:
117
				$message = sprintf(
118
					__(
119
						"<strong>Your Jetpack has a glitch.</strong>  We're sorry for the inconvenience. Please try again later, if the issue continues please <a %1\$s>contact support</a> with this message: %2\$s",
120
						'jetpack'
121
					),
122
					'href="https://jetpack.com/contact-support" rel="noopener noreferrer" target="_blank"',
123
					esc_html( $error_code )
124
				);
125
		}
126
127
		return $message;
128
	}
129
}
130