Completed
Push — add/beta-plugin ( d1204d )
by
unknown
189:46 queued 179:34
created

JetpackBetaCliCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 7.06 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 6
loc 85
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A branch() 0 22 4
B validation_checks() 6 17 8
A install_jetpack() 0 9 2
A branches_list() 0 14 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
if ( defined( 'WP_CLI' ) && WP_CLI ) {
4
	/**
5
	 * Control your local Jetpack Beta Tester plugin.
6
	 */
7
	class JetpackBetaCliCommand extends WP_CLI_Command {
8
		/**
9
		 * Activate a branch version
10
		 *
11
		 * ## OPTIONS
12
		 *
13
		 * activate master: Get a version of the master branch built every 15 minutes
14
		 * activate stable: Get the latest stable version of Jetpack
15
		 * activate branch_name: Get a version of PR. PR must be built and unit-tested before it become availabe
16
		 * list: Get list of available jetpack branches to install 
17
		 * 
18
		 * ## EXAMPLES
19
		 *
20
		 * wp jetpack-beta branch activate master
21
		 * wp jetpack-beta branch activate stable
22
		 * wp jetpack-beta branch activate branch_name
23
		 * wp jetpack-beta branch list
24
		 *
25
		 */
26
		public function branch( $args ) {
27
28
			$this->validation_checks( $args );
29
30
			if ( 'list' === $args[0] ) {
31
				return $this->branches_list();
32
			}
33
			
34
			$branches = array( 'master', 'stable', 'rc' );
35
36
			if ( in_array( $args[1], $branches)) {
37
				return $this->install_jetpack( $args[1], $args[1] );
38
			} else {
39
				$branch_name = str_replace( '/', '_', $args[1] ); 
40
				$url = Jetpack_Beta::get_install_url( $branch_name, 'pr' );
41
				if ( $url === null ) {
42
					return WP_CLI::error( __( 'Invalid branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack' ) );
43
				}
44
				return $this->install_jetpack( $branch_name, 'pr' );
45
			}
46
			return WP_CLI::error( __( 'Unrecognized branch version. ', 'jetpack' ) );
0 ignored issues
show
Unused Code introduced by
return \WP_CLI::error(__...ersion. ', 'jetpack')); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
47
		}
48
49
		private function validation_checks( $args ) {
50
			if ( is_multisite() && ! is_main_site() ) {
51
				return WP_CLI::error( __( 'Secondary sites in multisite instalations are not supported', 'jetpack' ) );				
52
			}
53
54
			if ( empty( $args ) ) {
55
				return WP_CLI::error( __( 'Specify subcommand. "activate" and "list" subcommands are supported', 'jetpack' ) );
56
			}
57
58 View Code Duplication
			if ( 'activate' !== $args[0] && 'list' !== $args[0] ) {
59
				return WP_CLI::error( __( 'Only "activate" and "list" subcommands are supported', 'jetpack' ) );				
60
			}
61
62 View Code Duplication
			if ( 'activate' === $args[0] && empty( $args[1] ) ) {
63
				return WP_CLI::error( __( 'Specify branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack' ) );				
64
			}
65
		}
66
67
		private function install_jetpack( $branch, $section ) {
68
			WP_CLI::line( 'Activating ' . $branch . ' branch...' );
69
70
			$result = Jetpack_Beta::install_and_activate( $branch, $section  );
71
			if ( is_wp_error( $result ) ) {
72
				return WP_CLI::error( __( 'Error', 'jetpack' ) . $result->get_error_message() );
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
			}
74
			return WP_CLI::success( __( 'Jetpack is currently on ' . $branch . ' branch', 'jetpack-beta' ) );
75
		}
76
77
		private function branches_list() {
78
			$manifest = Jetpack_Beta::get_beta_manifest();
79
			$jetpack_beta_active = get_option( 'jetpack_beta_active' );
80
			$current_branch = str_replace( '_', '/', $jetpack_beta_active[0] );
81
			$branches = array( 'stable', 'master', 'rc' );
82
			foreach( get_object_vars( $manifest->pr ) as $key ) {
83
				$branches[] = $key->branch;
84
			}
85
			sort($branches);
86
			WP_CLI::line( 'Available branches: ');
87
			foreach( $branches as $branch ) {
88
				WP_CLI::line( $current_branch == $branch ? '* ' . $branch : '  ' . $branch );
89
			}
90
		}
91
	}
92
93
	WP_CLI::add_command( 'jetpack-beta', 'JetpackBetaCliCommand' );
94
}
95