Complex classes like WPCOM_REST_API_V2_Field_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WPCOM_REST_API_V2_Field_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class WPCOM_REST_API_V2_Field_Controller { |
||
| 10 | /** |
||
| 11 | * @var string|string[] $object_type The REST Object Type(s) to which the field should be added. |
||
| 12 | */ |
||
| 13 | protected $object_type; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var string $field_name The name of the REST API field to add. |
||
| 17 | */ |
||
| 18 | protected $field_name; |
||
| 19 | |||
| 20 | public function __construct() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Registers the field with the appropriate schema and callbacks. |
||
| 38 | */ |
||
| 39 | public function register_fields() { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Ensures the response matches the schema and request context. |
||
| 51 | * |
||
| 52 | * You shouldn't have to extend this method. |
||
| 53 | * |
||
| 54 | * @param mixed $value |
||
| 55 | * @param WP_REST_Request $request |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | function prepare_for_response( $value, $request ) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the schema's default value |
||
| 72 | * |
||
| 73 | * If there is no default, returns the type's falsey value. |
||
| 74 | * |
||
| 75 | * @param array $schema |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | final public function get_default_value( $schema ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * The field's wrapped getter. Does permission checks and output preparation. |
||
| 104 | * |
||
| 105 | * This cannot be extended: implement `->get()` instead. |
||
| 106 | * |
||
| 107 | * @param mixed $object_data Probably an array. Whatever the endpoint returns. |
||
| 108 | * @param string $field_name Should always match `->field_name` |
||
| 109 | * @param WP_REST_Request $request |
||
| 110 | * @param $object_type Should always match `->object_type` |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | final public function get_for_response( $object_data, $field_name, $request, $object_type ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The field's wrapped setter. Does permission checks. |
||
| 127 | * |
||
| 128 | * This cannot be extended: implement `->update()` instead. |
||
| 129 | * |
||
| 130 | * @param mixed $value The new value for the field. |
||
| 131 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
| 132 | * @param string $field_name Should always match `->field_name` |
||
| 133 | * @param WP_REST_Request $request |
||
| 134 | * @param $object_type Should always match `->object_type` |
||
| 135 | * @return void|WP_Error |
||
| 136 | */ |
||
| 137 | final public function update_from_request( $value, $object_data, $field_name, $request, $object_type ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Permission Check for the field's getter. Must be implemented in the inheriting class. |
||
| 159 | * |
||
| 160 | * @param mixed $object_data Whatever the endpoint would returnn for its response. |
||
| 161 | * @param WP_REST_Request |
||
| 162 | * @return true|WP_Error |
||
| 163 | */ |
||
| 164 | public function get_permission_check( $object_data, $request ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The field's "raw" getter. Must be implemented in the inheriting class. |
||
| 171 | * |
||
| 172 | * @param mixed $object_data Whatever the endpoint would returnn for its response. |
||
| 173 | * @param WP_REST_Request |
||
| 174 | * @return mixed |
||
| 175 | */ |
||
| 176 | public function get( $object_data, $request ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Permission Check for the field's setter. Must be implemented in the inheriting class. |
||
| 183 | * |
||
| 184 | * @param mixed $value The new value for the field. |
||
| 185 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
| 186 | * @param WP_REST_Request |
||
| 187 | * @return true|WP_Error |
||
| 188 | */ |
||
| 189 | public function update_permission_check( $value, $object_data, $request ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The field's "raw" setter. Must be implemented in the inheriting class. |
||
| 196 | * |
||
| 197 | * @param mixed $value The new value for the field. |
||
| 198 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
| 199 | * @param WP_REST_Request |
||
| 200 | * @return mixed |
||
| 201 | */ |
||
| 202 | public function update( $value, $object_data, $request ) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * The JSON Schema for the field |
||
| 209 | * @link https://json-schema.org/understanding-json-schema/ |
||
| 210 | * As of WordPress 5.0, Core currently understands: |
||
| 211 | * * type |
||
| 212 | * * string - not minLength, not maxLength, not pattern |
||
| 213 | * * integer - minimum, maximum, exclusiveMinimum, exclusiveMaximum, not multipleOf |
||
| 214 | * * number - minimum, maximum, exclusiveMinimum, exclusiveMaximum, not multipleOf |
||
| 215 | * * boolean |
||
| 216 | * * null |
||
| 217 | * * object - properties, additionalProperties, not propertyNames, not dependencies, not patternProperties, not required |
||
| 218 | * * array: only lists, not tuples - items, not minItems, not maxItems, not uniqueItems, not contains |
||
| 219 | * * enum |
||
| 220 | * * format |
||
| 221 | * * date-time |
||
| 222 | |||
| 223 | * * ip |
||
| 224 | * * uri |
||
| 225 | * As of WordPress 5.0, Core does not support: |
||
| 226 | * * Multiple type: `type: [ 'string', 'integer' ]` |
||
| 227 | * * $ref, allOf, anyOf, oneOf, not, const |
||
| 228 | * |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function get_schema() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param array $schema |
||
| 238 | * @param string $context REST API Request context |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | private function is_valid_for_context( $schema, $context ) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Removes properties that should not appear in the current |
||
| 247 | * request's context |
||
| 248 | * |
||
| 249 | * $context is a Core REST API Framework request attribute that is |
||
| 250 | * always one of: |
||
| 251 | * * view (what you see on the blog) |
||
| 252 | * * edit (what you see in an editor) |
||
| 253 | * * embed (what you see in, e.g., an oembed) |
||
| 254 | * |
||
| 255 | * Fields (and sub-fields, and sub-sub-...) can be flagged for a |
||
| 256 | * set of specific contexts via the field's schema. |
||
| 257 | * |
||
| 258 | * The Core API will filter out top-level fields with the wrong |
||
| 259 | * context, but will not recurse deeply enough into arrays/objects |
||
| 260 | * to remove all levels of sub-fields with the wrong context. |
||
| 261 | * |
||
| 262 | * This function handles that recursion. |
||
| 263 | * |
||
| 264 | * @param mixed $value |
||
| 265 | * @param array $schema |
||
| 266 | * @param string $context REST API Request context |
||
| 267 | * @return mixed Filtered $value |
||
| 268 | */ |
||
| 269 | final function filter_response_by_context( $value, $schema, $context ) { |
||
| 326 | } |
||
| 327 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.