Complex classes like AuthenticationRequest 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 AuthenticationRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | abstract class AuthenticationRequest { |
||
38 | |||
39 | /** Indicates that the request is not required for authentication to proceed. */ |
||
40 | const OPTIONAL = 0; |
||
41 | |||
42 | /** Indicates that the request is required for authentication to proceed. */ |
||
43 | const REQUIRED = 1; |
||
44 | |||
45 | /** Indicates that the request is required by a primary authentication |
||
46 | * provdier, but other primary authentication providers do not require it. */ |
||
47 | const PRIMARY_REQUIRED = 2; |
||
48 | |||
49 | /** @var string|null The AuthManager::ACTION_* constant this request was |
||
50 | * created to be used for. The *_CONTINUE constants are not used here, the |
||
51 | * corresponding "begin" constant is used instead. |
||
52 | */ |
||
53 | public $action = null; |
||
54 | |||
55 | /** @var int For login, continue, and link actions, one of self::OPTIONAL, |
||
56 | * self::REQUIRED, or self::PRIMARY_REQUIRED */ |
||
57 | public $required = self::REQUIRED; |
||
58 | |||
59 | /** @var string|null Return-to URL, in case of redirect */ |
||
60 | public $returnToUrl = null; |
||
61 | |||
62 | /** @var string|null Username. May not be used by all subclasses. */ |
||
63 | public $username = null; |
||
64 | |||
65 | /** |
||
66 | * Supply a unique key for deduplication |
||
67 | * |
||
68 | * When the AuthenticationRequests instances returned by the providers are |
||
69 | * merged, the value returned here is used for keeping only one copy of |
||
70 | * duplicate requests. |
||
71 | * |
||
72 | * Subclasses should override this if multiple distinct instances would |
||
73 | * make sense, i.e. the request class has internal state of some sort. |
||
74 | * |
||
75 | * This value might be exposed to the user in web forms so it should not |
||
76 | * contain private information. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | public function getUniqueId() { |
||
83 | |||
84 | /** |
||
85 | * Fetch input field info |
||
86 | * |
||
87 | * The field info is an associative array mapping field names to info |
||
88 | * arrays. The info arrays have the following keys: |
||
89 | * - type: (string) Type of input. Types and equivalent HTML widgets are: |
||
90 | * - string: <input type="text"> |
||
91 | * - password: <input type="password"> |
||
92 | * - select: <select> |
||
93 | * - checkbox: <input type="checkbox"> |
||
94 | * - multiselect: More a grid of checkboxes than <select multi> |
||
95 | * - button: <input type="submit"> (uses 'label' as button text) |
||
96 | * - hidden: Not visible to the user, but needs to be preserved for the next request |
||
97 | * - null: No widget, just display the 'label' message. |
||
98 | * - options: (array) Maps option values to Messages for the |
||
99 | * 'select' and 'multiselect' types. |
||
100 | * - value: (string) Value (for 'null' and 'hidden') or default value (for other types). |
||
101 | * - label: (Message) Text suitable for a label in an HTML form |
||
102 | * - help: (Message) Text suitable as a description of what the field is |
||
103 | * - optional: (bool) If set and truthy, the field may be left empty |
||
104 | * |
||
105 | * @return array As above |
||
106 | */ |
||
107 | abstract public function getFieldInfo(); |
||
108 | |||
109 | /** |
||
110 | * Returns metadata about this request. |
||
111 | * |
||
112 | * This is mainly for the benefit of API clients which need more detailed render hints |
||
113 | * than what's available through getFieldInfo(). Semantics are unspecified and left to the |
||
114 | * individual subclasses, but the contents of the array should be primitive types so that they |
||
115 | * can be transformed into JSON or similar formats. |
||
116 | * |
||
117 | * @return array A (possibly nested) array with primitive types |
||
118 | */ |
||
119 | public function getMetadata() { |
||
122 | |||
123 | /** |
||
124 | * Initialize form submitted form data. |
||
125 | * |
||
126 | * Should always return false if self::getFieldInfo() returns an empty |
||
127 | * array. |
||
128 | * |
||
129 | * @param array $data Submitted data as an associative array |
||
130 | * @return bool Whether the request data was successfully loaded |
||
131 | */ |
||
132 | public function loadFromSubmission( array $data ) { |
||
189 | |||
190 | /** |
||
191 | * Describe the credentials represented by this request |
||
192 | * |
||
193 | * This is used on requests returned by |
||
194 | * AuthenticationProvider::getAuthenticationRequests() for ACTION_LINK |
||
195 | * and ACTION_REMOVE and for requests returned in |
||
196 | * AuthenticationResponse::$linkRequest to create useful user interfaces. |
||
197 | * |
||
198 | * @return Message[] with the following keys: |
||
199 | * - provider: A Message identifying the service that provides |
||
200 | * the credentials, e.g. the name of the third party authentication |
||
201 | * service. |
||
202 | * - account: A Message identifying the credentials themselves, |
||
203 | * e.g. the email address used with the third party authentication |
||
204 | * service. |
||
205 | */ |
||
206 | public function describeCredentials() { |
||
212 | |||
213 | /** |
||
214 | * Update a set of requests with form submit data, discarding ones that fail |
||
215 | * @param AuthenticationRequest[] $reqs |
||
216 | * @param array $data |
||
217 | * @return AuthenticationRequest[] |
||
218 | */ |
||
219 | public static function loadRequestsFromSubmission( array $reqs, array $data ) { |
||
224 | |||
225 | /** |
||
226 | * Select a request by class name. |
||
227 | * @param AuthenticationRequest[] $reqs |
||
228 | * @param string $class Class name |
||
229 | * @param bool $allowSubclasses If true, also returns any request that's a subclass of the given |
||
230 | * class. |
||
231 | * @return AuthenticationRequest|null Returns null if there is not exactly |
||
232 | * one matching request. |
||
233 | */ |
||
234 | public static function getRequestByClass( array $reqs, $class, $allowSubclasses = false ) { |
||
244 | |||
245 | /** |
||
246 | * Get the username from the set of requests |
||
247 | * |
||
248 | * Only considers requests that have a "username" field. |
||
249 | * |
||
250 | * @param AuthenticationRequest[] $requests |
||
|
|||
251 | * @return string|null |
||
252 | * @throws \UnexpectedValueException If multiple different usernames are present. |
||
253 | */ |
||
254 | public static function getUsernameFromRequests( array $reqs ) { |
||
272 | |||
273 | /** |
||
274 | * Merge the output of multiple AuthenticationRequest::getFieldInfo() calls. |
||
275 | * @param AuthenticationRequest[] $reqs |
||
276 | * @return array |
||
277 | * @throws \UnexpectedValueException If fields cannot be merged |
||
278 | */ |
||
279 | public static function mergeFieldInfo( array $reqs ) { |
||
344 | |||
345 | /** |
||
346 | * Implementing this mainly for use from the unit tests. |
||
347 | * @param array $data |
||
348 | * @return AuthenticationRequest |
||
349 | */ |
||
350 | public static function __set_state( $data ) { |
||
357 | } |
||
358 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.