Complex classes like XtoolsController 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 XtoolsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class XtoolsController extends Controller |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Given the request object, parse out common parameters. These include the |
||
| 27 | * 'project', 'username', 'namespace' and 'article', along with their legacy |
||
| 28 | * counterparts (e.g. 'lang' and 'wiki'). |
||
| 29 | * @param Request $request |
||
| 30 | * @return string[] Normalized parameters (no legacy params). |
||
| 31 | */ |
||
| 32 | public function parseQueryParams(Request $request) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Get a Project instance from the project string, using defaults if the |
||
| 49 | * given project string is invalid. |
||
| 50 | * @param string[] $params Query params. |
||
| 51 | * @return Project |
||
| 52 | */ |
||
| 53 | public function getProjectFromQuery($params) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * If the project and username in the given params hash are valid, Project and User instances |
||
| 78 | * are returned. User validation only occurs if 'username' is in the params. |
||
| 79 | * Otherwise a redirect is returned that goes back to the index page. |
||
| 80 | * @param Request $request The HTTP request. |
||
| 81 | * @param string $tooHighEditCountAction If the requested user has more than the configured |
||
| 82 | * max edit count, they will be redirect to this route, passing in available params. |
||
| 83 | * @return RedirectResponse|array Array contains [Project|null, User|null] |
||
| 84 | */ |
||
| 85 | public function validateProjectAndUser(Request $request, $tooHighEditCountAction = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Validate the given project, returning a Project if it is valid or false otherwise. |
||
| 108 | * @param string|string[] $params Project domain or database name, or params hash as |
||
| 109 | * retrieved by self::getParams(). |
||
| 110 | * @return Project|false |
||
| 111 | */ |
||
| 112 | public function validateProject($params) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Validate the given user, returning a User or Redirect if they don't exist. |
||
| 131 | * @param string|string[] $params Username or params hash as retrieved by self::getParams(). |
||
| 132 | * @param Project $project Project to get check against. |
||
| 133 | * @param string $tooHighEditCountAction If the requested user has more than the configured |
||
| 134 | * max edit count, they will be redirect to this route, passing in available params. |
||
| 135 | * @return RedirectResponse|User |
||
| 136 | */ |
||
| 137 | public function validateUser($params, Project $project, $tooHighEditCountAction = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get a Page instance from the given page title, and validate that it exists. |
||
| 174 | * @param Project $project |
||
| 175 | * @param string $pageTitle |
||
| 176 | * @return Page|RedirectResponse Page or redirect back to index if page doesn't exist. |
||
| 177 | */ |
||
| 178 | public function getAndValidatePage($project, $pageTitle) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Get all standardized parameters from the Request, either via URL query string or routing. |
||
| 196 | * @param Request $request |
||
| 197 | * @return string[] |
||
| 198 | */ |
||
| 199 | public function getParams(Request $request) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get UTC timestamps from given start and end string parameters. |
||
| 239 | * This also makes $start on month before $end if not present, |
||
| 240 | * and makes $end the current time if not present. |
||
| 241 | * @param string $start |
||
| 242 | * @param string $end |
||
| 243 | * @param bool $useDefaults Whether to use defaults if the values |
||
| 244 | * are blank. The start date is set to one month before the end date, |
||
| 245 | * and the end date is set to the present. |
||
| 246 | * @return mixed[] Start and end date as UTC timestamps or 'false' if empty. |
||
| 247 | */ |
||
| 248 | public function getUTCFromDateParams($start, $end, $useDefaults = true) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Given the params hash, normalize any legacy parameters to thier modern equivalent. |
||
| 277 | * @param string[] $params |
||
| 278 | * @return string[] |
||
| 279 | */ |
||
| 280 | private function convertLegacyParams($params) |
||
| 321 | } |
||
| 322 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: