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 |
||
18 | class Jetpack_Publicize_Gutenberg { |
||
19 | |||
20 | /** |
||
21 | * Instance of Publicize used to access data gathering utility methods. |
||
22 | * |
||
23 | * @since 5.9.1 |
||
24 | * @var Publicize $publicize Instance of Jetpack Publicize class. |
||
25 | */ |
||
26 | private $publicize; |
||
27 | |||
28 | /** |
||
29 | * Constructor for Jetpack_Publicize_Gutenberg |
||
30 | * |
||
31 | * Set up hooks to extend legacy Publicize behavior. |
||
32 | * |
||
33 | * @since 5.9.1 |
||
34 | */ |
||
35 | public function __construct( $publicize ) { |
||
46 | |||
47 | /** |
||
48 | * Retrieve current list of connected social accounts. |
||
49 | * |
||
50 | * Gets current list of connected accounts and send them as |
||
51 | * JSON encoded data. |
||
52 | * |
||
53 | * @see Publicize::get_filtered_connection_data() |
||
54 | * |
||
55 | * @since 5.9.1 |
||
56 | * |
||
57 | * @param WP_REST_Request $request Request instance from REST call. |
||
58 | * |
||
59 | * @return string JSON encoded connection list data. |
||
60 | */ |
||
61 | public function rest_get_publicize_connections( $request ) { |
||
65 | |||
66 | /** |
||
67 | * Add rest field to 'post' for Publicize support |
||
68 | * |
||
69 | * Sets up 'publicize' schema to submit publicize sharing title |
||
70 | * and individual connection sharing enables/disables. This schema |
||
71 | * is registered with the 'post' endpoint REST endpoint so publicize |
||
72 | * options can be saved when a post is published. |
||
73 | * |
||
74 | * @since 5.9.1 |
||
75 | */ |
||
76 | public function add_publicize_rest_fields() { |
||
135 | |||
136 | /** |
||
137 | * Check user capability for getting Publicize connection list from endpoint. |
||
138 | * |
||
139 | * @since 5.9.1 |
||
140 | * |
||
141 | * @return boolean True if current user has 'publish_post' capability. |
||
142 | */ |
||
143 | public function rest_connections_permission_callback() { |
||
146 | |||
147 | /** |
||
148 | * Check post id validity for Publicize connection list REST endpoint. |
||
149 | * |
||
150 | * @since 5.9.1 |
||
151 | * |
||
152 | * @param mixed $param post_id parameter from REST call. |
||
153 | * |
||
154 | * @return boolean True if post_id is valid integer |
||
155 | */ |
||
156 | public function rest_connections_validate_post_id( $param ) { |
||
159 | |||
160 | /** |
||
161 | * Set up Publicize meta fields for publishing post. |
||
162 | * |
||
163 | * Process 'publicize' REST field to setup Publicize for publishing |
||
164 | * post. Sets post meta keys to enable/disable each connection for |
||
165 | * the post and sets publicize title meta key if a title message |
||
166 | * is provided. |
||
167 | * |
||
168 | * @since 5.9.1 |
||
169 | * |
||
170 | * @param stdClass $new_post_obj Updated post object about to be inserted view REST endpoint. |
||
171 | * @param WP_REST_Request $request Request object, possibly containing 'publicize' field {@see add_publicize_rest_fields()}. |
||
172 | * |
||
173 | * @return WP_Post Returns the original $new_post value unchanged. |
||
174 | */ |
||
175 | public function process_publicize_from_rest( $new_post_obj, $request ) { |
||
218 | |||
219 | /** |
||
220 | * Checks if a connection should be shared to. |
||
221 | * |
||
222 | * Checks $connection_id against $connections_array to see if the connection associated |
||
223 | * with $connection_id should be shared to. Will return true if $connection_id is in the |
||
224 | * array and 'should_share' property is set to true, and will default to false otherwise. |
||
225 | * |
||
226 | * @since 5.9.1 |
||
227 | * |
||
228 | * @param array $connections_array 'connections' from 'publicize' REST field {@see add_publicize_rest_fields()}. |
||
229 | * @param string $connection_id Connection identifier string that is unique for each connection. |
||
230 | * @return boolean True if connection should be shared to, false otherwise. |
||
231 | */ |
||
232 | private function connection_should_share( $connections_array, $connection_id ) { |
||
242 | |||
243 | /** |
||
244 | * Enqueue scripts when they are needed for the edit page |
||
245 | * |
||
246 | * Enqueues necessary scripts for edit page for Gutenberg |
||
247 | * editor only. |
||
248 | * |
||
249 | * @since 5.9.1 |
||
250 | * |
||
251 | * @param string $hook Current page url. |
||
252 | */ |
||
253 | public function post_page_enqueue( $hook ) { |
||
267 | } |
||
268 |
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: