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 | * Creates a mock object of a service identified by its id. |
||
| 69 | */ |
||
| 70 | protected function getServiceMockBuilder(string $id): MockBuilder |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Builds up the environment to run the given command. |
||
| 80 | */ |
||
| 81 | 13 | protected function runCommand(string $name, array $params = [], bool $reuseKernel = false): CommandTester |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Retrieves the output verbosity level. |
||
| 121 | * |
||
| 122 | * @see \Symfony\Component\Console\Output\OutputInterface for available levels |
||
| 123 | * |
||
| 124 | * @throws \OutOfBoundsException If the set value isn't accepted |
||
| 125 | */ |
||
| 126 | 13 | protected function getVerbosityLevel(): int |
|
| 151 | |||
| 152 | 6 | public function setVerbosityLevel($level): void |
|
| 156 | |||
| 157 | 1 | protected function setInputs(array $inputs): void |
|
| 161 | |||
| 162 | 12 | protected function getInputs(): ?array |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Set verbosity for Symfony 3.4+. |
||
| 169 | * |
||
| 170 | * @see https://github.com/symfony/symfony/pull/24425 |
||
| 171 | * |
||
| 172 | * @param $level |
||
| 173 | */ |
||
| 174 | private function setVerbosityLevelEnv($level): void |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieves the flag indicating if the output should be decorated or not. |
||
| 181 | */ |
||
| 182 | 13 | protected function getDecorated(): bool |
|
| 196 | |||
| 197 | 6 | public function isDecorated(bool $decorated): void |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Get an instance of the dependency injection container. |
||
| 204 | * (this creates a kernel *without* parameters). |
||
| 205 | */ |
||
| 206 | 15 | private function getDependencyInjectionContainer(): ContainerInterface |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Keep support of Symfony < 5.3 |
||
| 228 | */ |
||
| 229 | 15 | public function __call(string $name, $arguments) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Creates an instance of a lightweight Http client. |
||
| 244 | * |
||
| 245 | * $params can be used to pass headers to the client, note that they have |
||
| 246 | * to follow the naming format used in $_SERVER. |
||
| 247 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 248 | */ |
||
| 249 | 30 | protected function makeClient(array $params = []): Client |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Creates an instance of a lightweight Http client. |
||
| 256 | * |
||
| 257 | * $params can be used to pass headers to the client, note that they have |
||
| 258 | * to follow the naming format used in $_SERVER. |
||
| 259 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 260 | */ |
||
| 261 | 1 | protected function makeAuthenticatedClient(array $params = []): Client |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Creates an instance of a lightweight Http client and log in user with |
||
| 273 | * username and password params. |
||
| 274 | * |
||
| 275 | * $params can be used to pass headers to the client, note that they have |
||
| 276 | * to follow the naming format used in $_SERVER. |
||
| 277 | * Example: 'HTTP_X_REQUESTED_WITH' instead of 'X-Requested-With' |
||
| 278 | */ |
||
| 279 | 1 | protected function makeClientWithCredentials(string $username, string $password, array $params = []): Client |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Create User Token. |
||
| 286 | * |
||
| 287 | * Factory method for creating a User Token object for the firewall based on |
||
| 288 | * the user object provided. By default it will be a Username/Password |
||
| 289 | * Token based on the user's credentials, but may be overridden for custom |
||
| 290 | * tokens in your applications. |
||
| 291 | * |
||
| 292 | * @param UserInterface $user The user object to base the token off of |
||
| 293 | * @param string $firewallName name of the firewall provider to use |
||
| 294 | * |
||
| 295 | * @return TokenInterface The token to be used in the security context |
||
| 296 | */ |
||
| 297 | 3 | protected function createUserToken(UserInterface $user, string $firewallName): TokenInterface |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Extracts the location from the given route. |
||
| 309 | * |
||
| 310 | * @param string $route The name of the route |
||
| 311 | * @param array $params Set of parameters |
||
| 312 | */ |
||
| 313 | 1 | protected function getUrl(string $route, array $params = [], int $absolute = UrlGeneratorInterface::ABSOLUTE_PATH): string |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Checks the success state of a response. |
||
| 320 | * |
||
| 321 | * @param Response $response Response object |
||
| 322 | * @param bool $success to define whether the response is expected to be successful |
||
| 323 | * @param string $type |
||
| 324 | */ |
||
| 325 | 6 | public function isSuccessful(Response $response, $success = true, $type = 'text/html'): void |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Executes a request on the given url and returns the response contents. |
||
| 332 | * |
||
| 333 | * This method also asserts the request was successful. |
||
| 334 | * |
||
| 335 | * @param string $path path of the requested page |
||
| 336 | * @param string $method The HTTP method to use, defaults to GET |
||
| 337 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 338 | * @param bool $success to define whether the response is expected to be successful |
||
| 339 | */ |
||
| 340 | 1 | public function fetchContent(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): string |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Executes a request on the given url and returns a Crawler object. |
||
| 354 | * |
||
| 355 | * This method also asserts the request was successful. |
||
| 356 | * |
||
| 357 | * @param string $path path of the requested page |
||
| 358 | * @param string $method The HTTP method to use, defaults to GET |
||
| 359 | * @param bool $authentication Whether to use authentication, defaults to false |
||
| 360 | * @param bool $success Whether the response is expected to be successful |
||
| 361 | */ |
||
| 362 | 1 | public function fetchCrawler(string $path, string $method = 'GET', bool $authentication = false, bool $success = true): Crawler |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @return WebTestCase |
||
| 375 | */ |
||
| 376 | 2 | public function loginAs(UserInterface $user, string $firewallName): self |
|
| 384 | |||
| 385 | 1 | public function loginClient(KernelBrowser $client, UserInterface $user, string $firewallName): void |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Asserts that the HTTP response code of the last request performed by |
||
| 411 | * $client matches the expected code. If not, raises an error with more |
||
| 412 | * information. |
||
| 413 | */ |
||
| 414 | 13 | public static function assertStatusCode(int $expectedStatusCode, Client $client): void |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Assert that the last validation errors within $container match the |
||
| 421 | * expected keys. |
||
| 422 | * |
||
| 423 | * @param array $expected A flat array of field names |
||
| 424 | */ |
||
| 425 | 4 | public static function assertValidationErrors(array $expected, ContainerInterface $container): void |
|
| 429 | |||
| 430 | 48 | protected function tearDown(): void |
|
| 444 | |||
| 445 | 32 | protected function createClientWithParams(array $params, ?string $username = null, ?string $password = null): Client |
|
| 484 | } |
||
| 485 |
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: