| Conditions | 15 |
| Paths | 113 |
| Total Lines | 63 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 17 | public static function setUpBeforeClass(): void |
||
| 18 | { |
||
| 19 | $monorepo = realpath(__DIR__.'/../../../../'); |
||
| 20 | |||
| 21 | if ($monorepo && realpath(getcwd()) === $monorepo && file_exists($monorepo.'/hyde')) { |
||
| 22 | throw new InvalidArgumentException('This test suite is not intended to be run from the monorepo.'); |
||
| 23 | } |
||
| 24 | |||
| 25 | if (! self::hasTestRunnerSetUp()) { |
||
| 26 | self::setUpTestRunner(); |
||
| 27 | } |
||
| 28 | |||
| 29 | // Check that post 8080 is available |
||
| 30 | $process = @fsockopen('localhost', 8080, $errno, $errstr, 1); |
||
| 31 | |||
| 32 | if ($process) { |
||
| 33 | // Try to find the PID of the process using the port |
||
| 34 | if (PHP_OS_FAMILY === 'Windows') { |
||
| 35 | $raw = shell_exec('netstat -aon | findstr :8080'); |
||
| 36 | // Get the PID of the process (last column of the first line) |
||
| 37 | preg_match('/\s+(\d+)$/', explode("\n", $raw)[0], $matches); |
||
| 38 | $pid = trim($matches[1]); |
||
| 39 | } else { |
||
| 40 | $pid = shell_exec('lsof -t -i:8080'); |
||
| 41 | } |
||
| 42 | |||
| 43 | fclose($process); |
||
| 44 | |||
| 45 | $throwOnBusyPort = false; |
||
| 46 | if ($throwOnBusyPort) { |
||
| 47 | throw new RuntimeException(sprintf('Port 8080 is already in use. (PID %s)', $pid)); |
||
| 48 | } else { |
||
| 49 | // Kill the process using the port |
||
| 50 | shell_exec(PHP_OS_FAMILY === 'Windows' ? "taskkill /F /PID $pid" : "kill -9 $pid"); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | // Start the server in a background process, keeping the task ID for later |
||
| 55 | $null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null'; |
||
| 56 | self::$server = proc_open("php hyde serve > $null", [], $pipes, realpath(self::getRunnerPath())); |
||
| 57 | |||
| 58 | // Wait for the server to start |
||
| 59 | $waitTime = time(); |
||
| 60 | while (@fsockopen('localhost', 8080, $errno, $errstr, 1) === false) { |
||
| 61 | // Timeout after 5 seconds |
||
| 62 | if (time() - $waitTime > 5) { |
||
| 63 | throw new RuntimeException('Failed to start the test server.'); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (proc_get_status(self::$server)['running'] === false) { |
||
| 67 | // Make a head request to the server to see if it's running |
||
| 68 | if (shell_exec('curl -I http://localhost:8080') === false) { |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // Sleep 20ms |
||
| 74 | usleep(20000); |
||
| 75 | } |
||
| 76 | |||
| 77 | // Assert that the server was started successfully |
||
| 78 | if (! self::$server) { |
||
| 79 | throw new RuntimeException('Failed to start the test server.'); |
||
| 80 | } |
||
| 200 |