Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class Request { |
||
| 15 | |||
| 16 | public function __construct() {} |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Whether this request is something that is renderable. |
||
| 20 | * |
||
| 21 | * @since 2.5.2 |
||
| 22 | * |
||
| 23 | * @return bool Yes or no. |
||
| 24 | */ |
||
| 25 | 101 | public function is_renderable() { |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Check if WordPress is_admin(), and make sure not DOING_AJAX. |
||
| 44 | * |
||
| 45 | * @return boolean |
||
| 46 | */ |
||
| 47 | 43 | public static function is_admin() { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * This is the frontend. |
||
| 56 | * |
||
| 57 | * @return boolean True or false. |
||
| 58 | */ |
||
| 59 | public static function is_frontend() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Is this the Add Media / From URL preview request? |
||
| 65 | * |
||
| 66 | * Will not work in WordPress 4.8+ |
||
| 67 | * |
||
| 68 | * @return boolean |
||
| 69 | */ |
||
| 70 | 3 | public static function is_add_oembed_preview() { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Is this an AJAX call in progress? |
||
| 77 | * |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | 3 | public static function is_ajax() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Is this a REST request? Call after parse_request. |
||
| 86 | * |
||
| 87 | * @return boolean |
||
| 88 | */ |
||
| 89 | public static function is_rest() { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The current $post is a View, no? |
||
| 95 | * |
||
| 96 | * @api |
||
| 97 | * @since 2.0 |
||
| 98 | * @todo tests |
||
| 99 | * |
||
| 100 | * @return \GV\View|false The view requested or false |
||
| 101 | */ |
||
| 102 | 90 | public function is_view() { |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Checks whether this is a single entry request |
||
| 112 | * |
||
| 113 | * @api |
||
| 114 | * @since 2.0 |
||
| 115 | * @todo tests |
||
| 116 | * |
||
| 117 | * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
||
| 118 | * |
||
| 119 | * @return \GV\GF_Entry|false The entry requested or false. |
||
| 120 | */ |
||
| 121 | 21 | public function is_entry( $form_id = 0 ) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Checks whether this an edit entry request. |
||
| 186 | * |
||
| 187 | * @api |
||
| 188 | * @since 2.0 |
||
| 189 | * @todo tests |
||
| 190 | * |
||
| 191 | * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
||
| 192 | * |
||
| 193 | * @return \GV\Entry|false The entry requested or false. |
||
| 194 | */ |
||
| 195 | 13 | public function is_edit_entry( $form_id = 0 ) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Checks whether this an entry search request. |
||
| 213 | * |
||
| 214 | * @api |
||
| 215 | * @since 2.0 |
||
| 216 | * @todo implementation |
||
| 217 | * |
||
| 218 | * @return boolean True if this is a search request. |
||
| 219 | */ |
||
| 220 | 15 | public function is_search() { |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Calculate whether the $_REQUEST has a GravityView field |
||
| 243 | * |
||
| 244 | * @internal |
||
| 245 | * @todo Roll into the future Search refactor |
||
| 246 | * |
||
| 247 | * @since 2.0.7 |
||
| 248 | * |
||
| 249 | * @param array $get $_POST or $_GET array |
||
| 250 | * |
||
| 251 | * @return bool True: GravityView-formatted field detected; False: not detected |
||
| 252 | */ |
||
| 253 | 14 | private function _has_field_key( $get ) { |
|
| 275 | } |
||
| 276 | |||
| 282 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.