| Conditions | 13 |
| Paths | 77 |
| Total Lines | 50 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 15 | public static function setUpBeforeClass(): void |
||
| 16 | { |
||
| 17 | $monorepo = realpath(__DIR__.'/../../../../'); |
||
| 18 | |||
| 19 | if ($monorepo && realpath(getcwd()) === $monorepo && file_exists($monorepo.'/hyde')) { |
||
| 20 | throw new InvalidArgumentException('This test suite is not intended to be run from the monorepo.'); |
||
| 21 | } |
||
| 22 | |||
| 23 | if (! self::hasTestRunnerSetUp()) { |
||
| 24 | self::setUpTestRunner(); |
||
| 25 | } |
||
| 26 | |||
| 27 | // Check that post 8080 is available |
||
| 28 | $process = @fsockopen('localhost', 8080, $errno, $errstr, 1); |
||
| 29 | |||
| 30 | if ($process) { |
||
|
|
|||
| 31 | // Try to find the PID of the process using the port |
||
| 32 | if (PHP_OS_FAMILY === 'Windows') { |
||
| 33 | $raw = shell_exec('netstat -aon | findstr :8080'); |
||
| 34 | // Get the PID of the process (last column of the first line) |
||
| 35 | preg_match('/\s+(\d+)$/', explode("\n", $raw)[0], $matches); |
||
| 36 | $pid = trim($matches[1]); |
||
| 37 | } else { |
||
| 38 | $pid = shell_exec('lsof -t -i:8080'); |
||
| 39 | } |
||
| 40 | |||
| 41 | fclose($process); |
||
| 42 | $throwInsteadOfKill = false; |
||
| 43 | if ($throwInsteadOfKill) { |
||
| 44 | throw new RuntimeException(sprintf('Port 8080 is already in use. (PID %s)', $pid)); |
||
| 45 | } else { |
||
| 46 | // Kill the process using the port |
||
| 47 | shell_exec(PHP_OS_FAMILY === 'Windows' ? "taskkill /F /PID $pid" : "kill -9 $pid"); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | // Start the server in a background process, keeping the task ID for later |
||
| 52 | $null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null'; |
||
| 53 | self::$server = proc_open("php hyde serve > $null", [], $pipes, realpath(__DIR__.'/../runner')); |
||
| 54 | |||
| 55 | // Wait for the server to start |
||
| 56 | while (@fsockopen('localhost', 8080, $errno, $errstr, 1) === false) { |
||
| 57 | if (proc_get_status(self::$server)['running'] === false) { |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | // Assert that the server was started successfully |
||
| 63 | if (! self::$server) { |
||
| 64 | throw new RuntimeException('Failed to start the test server.'); |
||
| 65 | } |
||
| 143 |