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() { |
||
52 | |||
53 | /** |
||
54 | * Ensures the response matches the schema and request context. |
||
55 | * |
||
56 | * @param mixed $value |
||
57 | * @param WP_REST_Request $request |
||
58 | * @return mixed |
||
59 | */ |
||
60 | private function prepare_for_response( $value, $request ) { |
||
71 | |||
72 | /** |
||
73 | * Returns the schema's default value |
||
74 | * |
||
75 | * If there is no default, returns the type's falsey value. |
||
76 | * |
||
77 | * @param array $schema |
||
78 | * @return mixed |
||
79 | */ |
||
80 | final public function get_default_value( $schema ) { |
||
103 | |||
104 | /** |
||
105 | * The field's wrapped getter. Does permission checks and output preparation. |
||
106 | * |
||
107 | * This cannot be extended: implement `->get()` instead. |
||
108 | * |
||
109 | * @param mixed $object_data Probably an array. Whatever the endpoint returns. |
||
110 | * @param string $field_name Should always match `->field_name` |
||
111 | * @param WP_REST_Request $request |
||
112 | * @param string $object_type Should always match `->object_type` |
||
113 | * @return mixed |
||
114 | */ |
||
115 | final public function get_for_response( $object_data, $field_name, $request, $object_type ) { |
||
132 | |||
133 | /** |
||
134 | * The field's wrapped setter. Does permission checks. |
||
135 | * |
||
136 | * This cannot be extended: implement `->update()` instead. |
||
137 | * |
||
138 | * @param mixed $value The new value for the field. |
||
139 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
140 | * @param string $field_name Should always match `->field_name` |
||
141 | * @param WP_REST_Request $request |
||
142 | * @param string $object_type Should always match `->object_type` |
||
143 | * @return void|WP_Error |
||
144 | */ |
||
145 | final public function update_from_request( $value, $object_data, $field_name, $request, $object_type ) { |
||
165 | |||
166 | /** |
||
167 | * Permission Check for the field's getter. Must be implemented in the inheriting class. |
||
168 | * |
||
169 | * @param mixed $object_data Whatever the endpoint would return for its response. |
||
170 | * @param WP_REST_Request $request |
||
171 | * @return true|WP_Error |
||
172 | */ |
||
173 | public function get_permission_check( $object_data, $request ) { |
||
177 | |||
178 | /** |
||
179 | * The field's "raw" getter. Must be implemented in the inheriting class. |
||
180 | * |
||
181 | * @param mixed $object_data Whatever the endpoint would return for its response. |
||
182 | * @param WP_REST_Request $request |
||
183 | * @return mixed |
||
184 | */ |
||
185 | public function get( $object_data, $request ) { |
||
189 | |||
190 | /** |
||
191 | * Permission Check for the field's setter. Must be implemented in the inheriting class. |
||
192 | * |
||
193 | * @param mixed $value The new value for the field. |
||
194 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
195 | * @param WP_REST_Request $request |
||
196 | * @return true|WP_Error |
||
197 | */ |
||
198 | public function update_permission_check( $value, $object_data, $request ) { |
||
202 | |||
203 | /** |
||
204 | * The field's "raw" setter. Must be implemented in the inheriting class. |
||
205 | * |
||
206 | * @param mixed $value The new value for the field. |
||
207 | * @param mixed $object_data Probably a WordPress object (e.g., WP_Post) |
||
208 | * @param WP_REST_Request $request |
||
209 | * @return mixed |
||
210 | */ |
||
211 | public function update( $value, $object_data, $request ) { |
||
215 | |||
216 | /** |
||
217 | * The JSON Schema for the field |
||
218 | * |
||
219 | * @link https://json-schema.org/understanding-json-schema/ |
||
220 | * As of WordPress 5.0, Core currently understands: |
||
221 | * * type |
||
222 | * * string - not minLength, not maxLength, not pattern |
||
223 | * * integer - minimum, maximum, exclusiveMinimum, exclusiveMaximum, not multipleOf |
||
224 | * * number - minimum, maximum, exclusiveMinimum, exclusiveMaximum, not multipleOf |
||
225 | * * boolean |
||
226 | * * null |
||
227 | * * object - properties, additionalProperties, not propertyNames, not dependencies, not patternProperties, not required |
||
228 | * * array: only lists, not tuples - items, not minItems, not maxItems, not uniqueItems, not contains |
||
229 | * * enum |
||
230 | * * format |
||
231 | * * date-time |
||
232 | |||
233 | * * ip |
||
234 | * * uri |
||
235 | * As of WordPress 5.0, Core does not support: |
||
236 | * * Multiple type: `type: [ 'string', 'integer' ]` |
||
237 | * * $ref, allOf, anyOf, oneOf, not, const |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | public function get_schema() { |
||
245 | |||
246 | /** |
||
247 | * @param array $schema |
||
248 | * @param string $context REST API Request context |
||
249 | * @return bool |
||
250 | */ |
||
251 | private function is_valid_for_context( $schema, $context ) { |
||
254 | |||
255 | /** |
||
256 | * Removes properties that should not appear in the current |
||
257 | * request's context |
||
258 | * |
||
259 | * $context is a Core REST API Framework request attribute that is |
||
260 | * always one of: |
||
261 | * * view (what you see on the blog) |
||
262 | * * edit (what you see in an editor) |
||
263 | * * embed (what you see in, e.g., an oembed) |
||
264 | * |
||
265 | * Fields (and sub-fields, and sub-sub-...) can be flagged for a |
||
266 | * set of specific contexts via the field's schema. |
||
267 | * |
||
268 | * The Core API will filter out top-level fields with the wrong |
||
269 | * context, but will not recurse deeply enough into arrays/objects |
||
270 | * to remove all levels of sub-fields with the wrong context. |
||
271 | * |
||
272 | * This function handles that recursion. |
||
273 | * |
||
274 | * @param mixed $value |
||
275 | * @param array $schema |
||
276 | * @param string $context REST API Request context |
||
277 | * @return mixed Filtered $value |
||
278 | */ |
||
279 | final public function filter_response_by_context( $value, $schema, $context ) { |
||
335 | } |
||
336 |
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.