Completed
Push — fix/display-connection-errors ( 995afe...d1e7f2 )
by
unknown
27:09 queued 18:54
created

Error_Handler   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 117
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 109 47

How to fix   Complexity   

Complex Class

Complex classes like 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 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 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
					/* translators: URL to the WPCOM TOS page. */
57
					__(
58
						'This site can\'t be connected to WordPress.com because it violates our  <a %s>Terms of Service</a>.',
59
						'jetpack'
60
					),
61
					'href="https://wordpress.com/tos" rel="noopener noreferrer" target="_blank"'
62
				);
63
				break;
64
			case 'not_public':
65
				$message = __(
66
					'<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).',
67
					'jetpack'
68
				);
69
				break;
70
			case 'wpcom_408':
71
			case 'wpcom_5??':
72
			case 'wpcom_bad_response':
73
			case 'wpcom_outage':
74
				$message = __(
75
					'WordPress.com is currently having problems and is unable to fuel up your Jetpack.  Please try again later.',
76
					'jetpack'
77
				);
78
				break;
79
			case 'register_http_request_failed':
80
			case 'token_http_request_failed':
81
				$message = __(
82
					'Jetpack could not contact WordPress.com.  This usually means something is incorrectly configured on your web host.',
83
					'jetpack'
84
				);
85
				break;
86
			case 'no_role':
87
			case 'no_cap':
88
			case 'no_code':
89
			case 'no_state':
90
			case 'invalid_state':
91
			case 'invalid_request':
92
			case 'invalid_scope':
93
			case 'unsupported_response_type':
94
			case 'invalid_token':
95
			case 'no_token':
96
			case 'missing_secrets':
97
			case 'home_missing':
98
			case 'siteurl_missing':
99
			case 'gmt_offset_missing':
100
			case 'site_name_missing':
101
			case 'secret_1_missing':
102
			case 'secret_2_missing':
103
			case 'site_lang_missing':
104
			case 'home_malformed':
105
			case 'siteurl_malformed':
106
			case 'gmt_offset_malformed':
107
			case 'timezone_string_malformed':
108
			case 'site_name_malformed':
109
			case 'secret_1_malformed':
110
			case 'secret_2_malformed':
111
			case 'site_lang_malformed':
112
			case 'secrets_mismatch':
113
			case 'verify_secret_1_missing':
114
			case 'verify_secret_1_malformed':
115
			case 'verify_secrets_missing':
116
			case 'verify_secrets_mismatch':
117
			default:
118
				$message = sprintf(
119
					__(
120
						"<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",
121
						'jetpack'
122
					),
123
					'href="https://jetpack.com/contact-support" rel="noopener noreferrer" target="_blank"',
124
					esc_html( $error_code )
125
				);
126
		}
127
128
		return $message;
129
	}
130
}
131