Conditions | 10 |
Paths | 97 |
Total Lines | 66 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
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 ) ) ) |
||
|
|||
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 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: