Completed
Push — add/notification-on-plan-upgra... ( 77a3fc )
by
unknown
08:20 queued 14s
created

Jetpack_Components::render_upgrade_nudge()   C

Complexity

Conditions 10
Paths 97

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 97
nop 1
dl 0
loc 66
rs 6.8751
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
/**
3
 * Components Library
4
 *
5
 * Load and display a pre-rendered component
6
 */
7
class Jetpack_Components {
8
	/**
9
	 * Load and display a pre-rendered component
10
	 *
11
	 * @since 7.7.0
12
	 *
13
	 * @param string $name  Component name.
14
	 * @param array  $props Component properties.
15
	 *
16
	 * @return string The component markup
17
	 */
18
	public static function render_component( $name, $props ) {
19
20
		$rtl = is_rtl() ? '.rtl' : '';
21
		wp_enqueue_style( 'jetpack-components', plugins_url( "_inc/blocks/components{$rtl}.css", JETPACK__PLUGIN_FILE ), array( 'wp-components' ), JETPACK__VERSION );
22
23
		ob_start();
24
		// `include` fails gracefully and throws a warning, but doesn't halt execution.
25
		include JETPACK__PLUGIN_DIR . "_inc/blocks/$name.html";
26
		$markup = ob_get_clean();
27
28
		foreach ( $props as $key => $value ) {
29
			$markup = str_replace(
30
				"#$key#",
31
				$value,
32
				$markup
33
			);
34
35
			// Workaround, required to replace strings in `sprintf`-expressions.
36
			// See extensions/i18n-to-php.js for more information.
37
			$markup = str_replace(
38
				"%($key)s",
39
				$value,
40
				$markup
41
			);
42
		}
43
44
		return $markup;
45
	}
46
47
	/**
48
	 * Load and display a pre-rendered component
49
	 *
50
	 * @since 7.7.0
51
	 *
52
	 * @param array $props Component properties.
53
	 *
54
	 * @return string The component markup
55
	 */
56
	public static function render_upgrade_nudge( $props ) {
57
		$plan_slug = $props['plan'];
58
		jetpack_require_lib( 'plans' );
59
		$plan = Jetpack_Plans::get_plan( $plan_slug );
60
61
		if ( ! $plan ) {
62
			return self::render_component(
63
				'upgrade-nudge',
64
				array(
65
					'planName'   => __( 'a paid plan', 'jetpack' ),
66
					'upgradeUrl' => '',
67
				)
68
			);
69
		}
70
71
		// WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't
72
		// For Jetpack, we thus use the plan slug with the 'jetpack_' prefix removed.
73
		$plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' )
74
			? substr( $plan_slug, strlen( 'jetpack_' ) )
75
			: $plan->path_slug;
76
77
		$post_id   = get_the_ID();
78
		$post_type = get_post_type();
79
80
		// The editor for CPTs has an `edit/` route fragment prefixed.
81
		$post_type_editor_route_prefix = in_array( $post_type, array( 'page', 'post' ), true ) ? '' : 'edit';
82
83
		if ( method_exists( 'Jetpack', 'build_raw_urls' ) ) {
84
			$site_slug = Jetpack::build_raw_urls( home_url() );
85
		} elseif ( class_exists( 'WPCOM_Masterbar' ) && method_exists( 'WPCOM_Masterbar', 'get_calypso_site_slug' ) ) {
86
			$site_slug = WPCOM_Masterbar::get_calypso_site_slug( get_current_blog_id() );
87
		}
88
89
		// Post-checkout: redirect back to the editor.
90
		$redirect_to = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
91
			? add_query_arg(
92
				array(
93
					'plan_upgraded' => 1,
94
				),
95
				'/' . implode( '/', array_filter( array( $post_type_editor_route_prefix, $post_type, $site_slug, $post_id ) ) )
0 ignored issues
show
Bug introduced by
The variable $site_slug does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
96
			)
97
			: add_query_arg(
98
				array(
99
					'action'        => 'edit',
100
					'post'          => $post_id,
101
					'plan_upgraded' => 1,
102
				),
103
				admin_url( 'post.php' )
104
			);
105
106
		$upgrade_url =
107
			$plan_path_slug
108
			? add_query_arg(
109
				'redirect_to',
110
				$redirect_to,
111
				"https://wordpress.com/checkout/${site_slug}/${plan_path_slug}"
112
			) : '';
113
114
		return self::render_component(
115
			'upgrade-nudge',
116
			array(
117
				'planName'   => $plan->product_name,
118
				'upgradeUrl' => $upgrade_url,
119
			)
120
		);
121
	}
122
}
123