Completed
Push — add/connect-user-endpoint ( 4e5b9e...eae093 )
by
unknown
28:36 queued 18:20
created

Jetpack_JSON_API_User_Create_Endpoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 17.31 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 9
loc 52
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A result() 0 3 1
A validate_input() 0 13 4
A create_or_get_user() 0 16 3
A get_user() 9 9 4

How to fix   Duplicated Code   

Duplicated Code

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:

1
<?php
2
3
class Jetpack_JSON_API_User_Create_Endpoint extends Jetpack_JSON_API_Endpoint {
4
5
	protected $needed_capabilities = 'create_users';
6
7
	private $user_data;
8
9
	function result() {
10
		return $this->create_or_get_user();
11
	}
12
13
	function validate_input( $object ) {
14
		$this->user_data = $this->input();
15
		if ( empty( $this->user_data ) ) {
16
			return new WP_Error( 'input_error', __( 'user_data is required', 'jetpack' ) );
17
		}
18
		if ( ! isset( $this->user_data[ 'email' ] ) ) {
19
			return new WP_Error( 'input_error', __( 'user email is required', 'jetpack' ) );
20
		}
21
		if ( ! isset( $this->user_data[ 'login' ] ) ) {
22
			return new WP_Error( 'input_error', __( 'user login is required', 'jetpack' ) );
23
		}
24
		return parent::validate_input( $object );
25
	}
26
27
	function create_or_get_user() {
28
		require_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php';
29
30
		// Check for an existing user
31
		$user = get_user_by( 'email', $this->user_date['email'] );
0 ignored issues
show
Bug introduced by
The property user_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
		if ( ! $user ) {
34
			$user = Jetpack_SSO_Helpers::generate_user( (object) $this->user_data );
35
		}
36
37
		if ( ! $user ) {
38
			return false;
39
		}
40
41
		return $this->get_user( $user->ID );
42
	}
43
44 View Code Duplication
	public function get_user( $user_id ) {
45
		$the_user = $this->get_author( $user_id, true );
46
		if ( $the_user && ! is_wp_error( $the_user ) ) {
47
			$userdata = get_userdata( $user_id );
48
			$the_user->roles = ! is_wp_error( $userdata ) ? $userdata->roles : array();
49
		}
50
51
		return $the_user;
52
	}
53
54
}
55