Complex classes like WebTestCase 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 WebTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | abstract class WebTestCase extends BaseWebTestCase |
||
| 44 | { |
||
| 45 | protected $environment = 'test'; |
||
| 46 | |||
| 47 | protected $containers; |
||
| 48 | |||
| 49 | // 5 * 1024 * 1024 KB |
||
| 50 | protected $maxMemory = 5242880; |
||
| 51 | |||
| 52 | // RUN COMMAND |
||
| 53 | protected $verbosityLevel; |
||
| 54 | |||
| 55 | protected $decorated; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array|null |
||
| 59 | */ |
||
| 60 | private $inputs = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $firewallLogins = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var KernelInterface |
||
| 69 | */ |
||
| 70 | protected static $testCaseKernel; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Creates a mock object of a service identified by its id. |
||
| 74 | */ |
||
| 75 | protected function getServiceMockBuilder(string $id): MockBuilder |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Builds up the environment to run the given command. |
||
| 85 | */ |
||
| 86 | 13 | protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Retrieves the output verbosity level. |
||
| 126 | * |
||
| 127 | * @see \Symfony\Component\Console\Output\OutputInterface for available levels |
||
| 128 | * |
||
| 129 | * @throws \OutOfBoundsException If the set value isn't accepted |
||
| 130 | */ |
||
| 131 | 13 | protected function getVerbosityLevel(): int |
|
| 156 | |||
| 157 | 6 | public function setVerbosityLevel($level): void |
|
| 161 | |||
| 162 | 1 | protected function setInputs(array $inputs): void |
|
| 166 | |||
| 167 | 12 | protected function getInputs(): ?array |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Set verbosity for Symfony 3.4+. |
||
| 174 | * |
||
| 175 | * @see https://github.com/symfony/symfony/pull/24425 |
||
| 176 | * |
||
| 177 | * @param $level |
||
| 178 | */ |
||
| 179 | private function setVerbosityLevelEnv($level): void |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Retrieves the flag indicating if the output should be decorated or not. |
||
| 186 | */ |
||
| 187 | 13 | protected function getDecorated(): bool |
|
| 201 | |||
| 202 | 6 | public function isDecorated(bool $decorated): void |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Get an instance of the dependency injection container. |
||
| 209 | * (this creates a kernel *without* parameters). |
||
| 210 | */ |
||
| 211 | 15 | protected function getContainer(): ContainerInterface |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Creates an instance of a lightweight Http client. |
||
| 234 | * |
||
| 235 | * $params can be used to pass headers to the client, note that they have |
||
| 236 | * to follow the naming format used in $_SERVER. |
||
| 237 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 238 | */ |
||
| 239 | 30 | protected function makeClient(array $params = []): Client |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Creates an instance of a lightweight Http client. |
||
| 246 | * |
||
| 247 | * $params can be used to pass headers to the client, note that they have |
||
| 248 | * to follow the naming format used in $_SERVER. |
||
| 249 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 250 | */ |
||
| 251 | 1 | protected function makeAuthenticatedClient(array $params = []): Client |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Creates an instance of a lightweight Http client and log in user with |
||
| 263 | * username and password params. |
||
| 264 | * |
||
| 265 | * $params can be used to pass headers to the client, note that they have |
||
| 266 | * to follow the naming format used in $_SERVER. |
||
| 267 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 268 | */ |
||
| 269 | 1 | protected function makeClientWithCredentials(string $username, string $password, array $params = []): Client |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Create User Token. |
||
| 276 | * |
||
| 277 | * Factory method for creating a User Token object for the firewall based on |
||
| 278 | * the user object provided. By default it will be a Username/Password |
||
| 279 | * Token based on the user's credentials, but may be overridden for custom |
||
| 280 | * tokens in your applications. |
||
| 281 | * |
||
| 282 | * @param UserInterface $user The user object to base the token off of |
||
| 283 | * @param string $firewallName name of the firewall provider to use |
||
| 284 | * |
||
| 285 | * @return TokenInterface The token to be used in the security context |
||
| 286 | */ |
||
| 287 | 3 | protected function createUserToken(UserInterface $user, string $firewallName): TokenInterface |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Extracts the location from the given route. |
||
| 299 | * |
||
| 300 | * @param string $route The name of the route |
||
| 301 | * @param array $params Set of parameters |
||
| 302 | */ |
||
| 303 | 1 | protected function getUrl(string $route, array $params = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Checks the success state of a response. |
||
| 310 | * |
||
| 311 | * @param Response $response Response object |
||
| 312 | * @param bool $success to define whether the response is expected to be successful |
||
| 313 | * @param string $type |
||
| 314 | */ |
||
| 315 | 6 | public function isSuccessful(Response $response, $success = true, $type = 'text/html'): void |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Executes a request on the given url and returns the response contents. |
||
| 322 | * |
||
| 323 | * This method also asserts the request was successful. |
||
| 324 | * |
||
| 325 | * @param string $path path of the requested page |
||
| 326 | * @param string $method The HTTP method to use, defaults to GET |
||
| 327 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 328 | * @param bool $success to define whether the response is expected to be successful |
||
| 329 | */ |
||
| 330 | 1 | public function fetchContent(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): string |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Executes a request on the given url and returns a Crawler object. |
||
| 344 | * |
||
| 345 | * This method also asserts the request was successful. |
||
| 346 | * |
||
| 347 | * @param string $path path of the requested page |
||
| 348 | * @param string $method The HTTP method to use, defaults to GET |
||
| 349 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 350 | * @param bool $success Whether the response is expected to be successful |
||
| 351 | */ |
||
| 352 | 1 | public function fetchCrawler(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): Crawler |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return WebTestCase |
||
| 365 | */ |
||
| 366 | 2 | public function loginAs(UserInterface $user, string $firewallName): self |
|
| 374 | |||
| 375 | 1 | public function loginClient(KernelBrowser $client, UserInterface $user, string $firewallName): void |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Asserts that the HTTP response code of the last request performed by |
||
| 401 | * $client matches the expected code. If not, raises an error with more |
||
| 402 | * information. |
||
| 403 | */ |
||
| 404 | 13 | public static function assertStatusCode(int $expectedStatusCode, Client $client): void |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Assert that the last validation errors within $container match the |
||
| 411 | * expected keys. |
||
| 412 | * |
||
| 413 | * @param array $expected A flat array of field names |
||
| 414 | */ |
||
| 415 | 4 | public static function assertValidationErrors(array $expected, ContainerInterface $container): void |
|
| 419 | |||
| 420 | 48 | protected function tearDown(): void |
|
| 438 | |||
| 439 | 32 | protected function createClientWithParams(array $params, ?string $username = null, ?string $password = null): Client |
|
| 478 | } |
||
| 479 |
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: