Completed
Push — add/jetpack-assistant-ui ( a6f776...33ce41 )
by Jeremy
202:03 queued 191:10
created

JetpackBetaCliCommand::validation_checks()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 18

Duplication

Lines 6
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 8
nc 5
nop 1
dl 6
loc 18
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * Jetpack Beta Tester CLI controls
4
 *
5
 * @package Jetpack Beta
6
 */
7
8
if ( defined( 'WP_CLI' ) && WP_CLI ) {
9
	/**
10
	 * Control your local Jetpack Beta Tester plugin.
11
	 */
12
	class JetpackBetaCliCommand extends WP_CLI_Command {
13
		/**
14
		 * Activate a branch version
15
		 *
16
		 * ## OPTIONS
17
		 *
18
		 * activate master: Get a version of the master branch built every 15 minutes
19
		 * activate stable: Get the latest stable version of Jetpack
20
		 * activate branch_name: Get a version of PR. PR must be built and unit-tested before it become availabe
21
		 * list: Get list of available jetpack branches to install
22
		 *
23
		 * ## EXAMPLES
24
		 *
25
		 * wp jetpack-beta branch activate master
26
		 * wp jetpack-beta branch activate stable
27
		 * wp jetpack-beta branch activate branch_name
28
		 * wp jetpack-beta branch list
29
		 *
30
		 * @param array $args arguments passed to CLI, per the examples above.
31
		 */
32
		public function branch( $args ) {
33
34
			$this->validation_checks( $args );
35
36
			if ( 'list' === $args[0] ) {
37
				return $this->branches_list();
38
			}
39
40
			$branches = array( 'master', 'stable', 'rc' );
41
42
			if ( in_array( $args[1], $branches, true ) ) {
43
				return $this->install_jetpack( $args[1], $args[1] );
44
			} else {
45
				$branch_name = str_replace( '/', '_', $args[1] );
46
				$url         = Jetpack_Beta::get_install_url( $branch_name, 'pr' );
47
				if ( null === $url ) {
48
					return WP_CLI::error( __( 'Invalid branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack-beta' ) );
49
				}
50
				return $this->install_jetpack( $branch_name, 'pr' );
51
			}
52
			return WP_CLI::error( __( 'Unrecognized branch version. ', 'jetpack-beta' ) );
0 ignored issues
show
Unused Code introduced by
return \WP_CLI::error(__...n. ', 'jetpack-beta')); 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...
53
		}
54
55
		/**
56
		 * Validate that we can switch branches.
57
		 *
58
		 * @param array $args arguments passed to CLI.
59
		 */
60
		private function validation_checks( $args ) {
61
62
			if ( is_multisite() && ! is_main_site() ) {
63
				return WP_CLI::error( __( 'Secondary sites in multisite instalations are not supported', 'jetpack-beta' ) );
64
			}
65
66
			if ( empty( $args ) ) {
67
				return WP_CLI::error( __( 'Specify subcommand. "activate" and "list" subcommands are supported', 'jetpack-beta' ) );
68
			}
69
70 View Code Duplication
			if ( 'activate' !== $args[0] && 'list' !== $args[0] ) {
71
				return WP_CLI::error( __( 'Only "activate" and "list" subcommands are supported', 'jetpack-beta' ) );
72
			}
73
74 View Code Duplication
			if ( 'activate' === $args[0] && empty( $args[1] ) ) {
75
				return WP_CLI::error( __( 'Specify branch name. Try `wp jetpack-beta branch list` for list of available branches', 'jetpack-beta' ) );
76
			}
77
		}
78
79
		/**
80
		 * Install Jetpack using selected branch.
81
		 *
82
		 * @param array $branch is the selected branch.
83
		 * @param array $section what we're specifically installing (PR, master, stable, etc).
84
		 */
85
		private function install_jetpack( $branch, $section ) {
86
87
			WP_CLI::line( 'Activating ' . $branch . ' branch...' );
88
89
			$result = Jetpack_Beta::install_and_activate( $branch, $section );
0 ignored issues
show
Documentation introduced by
$branch is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$section is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
			if ( is_wp_error( $result ) ) {
91
				return WP_CLI::error( __( 'Error', 'jetpack-beta' ) . $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...
92
			}
93
			// translators: $branch is what branch we've switched to.
94
			return WP_CLI::success( printf( esc_html__( 'Jetpack is currently on %s branch', 'jetpack-beta' ), esc_html( $branch ) ) );
95
		}
96
97
		/**
98
		 * Display list of branches.
99
		 */
100
		private function branches_list() {
101
			$manifest            = Jetpack_Beta::get_beta_manifest();
102
			$jetpack_beta_active = get_option( 'jetpack_beta_active' );
103
			$current_branch      = str_replace( '_', '/', $jetpack_beta_active[0] );
104
			$branches            = array( 'stable', 'master', 'rc' );
105
			foreach ( get_object_vars( $manifest->pr ) as $key ) {
106
				$branches[] = $key->branch;
107
			}
108
			sort( $branches );
109
			WP_CLI::line( 'Available branches: ' );
110
			foreach ( $branches as $branch ) {
111
				WP_CLI::line( $current_branch === $branch ? '* ' . $branch : '  ' . $branch );
112
			}
113
		}
114
	}
115
	WP_CLI::add_command( 'jetpack-beta', 'JetpackBetaCliCommand' );
116
}
117