| Conditions | 10 |
| Paths | 244 |
| Total Lines | 84 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 65 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 66 | { |
||
| 67 | $drafts = $input->getOption('drafts'); |
||
| 68 | $open = $input->getOption('open'); |
||
| 69 | $host = $input->getOption('host') ?? 'localhost'; |
||
| 70 | $port = $input->getOption('port') ?? '8000'; |
||
| 71 | $postprocess = $input->getOption('postprocess'); |
||
| 72 | |||
| 73 | $this->setUpServer($host, $port); |
||
| 74 | $command = sprintf( |
||
| 75 | 'php -S %s:%d -t %s %s', |
||
| 76 | $host, |
||
| 77 | $port, |
||
| 78 | $this->getPath().'/'.(string) $this->getBuilder()->getConfig()->get('output.dir'), |
||
| 79 | Util::joinFile($this->getPath(), self::TMP_DIR, 'router.php') |
||
| 80 | ); |
||
| 81 | $process = Process::fromShellCommandline($command); |
||
| 82 | |||
| 83 | // (re)builds before serve |
||
| 84 | $buildCommand = $this->getApplication()->find('build'); |
||
| 85 | $buildImputArray = [ |
||
| 86 | 'command' => 'build', |
||
| 87 | 'path' => $this->getPath(), |
||
| 88 | '--drafts' => $drafts, |
||
| 89 | '--postprocess' => $postprocess, |
||
| 90 | ]; |
||
| 91 | if ($this->getConfigFile() !== null) { |
||
|
1 ignored issue
–
show
|
|||
| 92 | $buildImputArray = array_merge($buildImputArray, [ |
||
| 93 | '--config' => $this->getConfigFile(), |
||
| 94 | ]); |
||
| 95 | } |
||
| 96 | $buildInput = new ArrayInput($buildImputArray); |
||
| 97 | if ($buildCommand->run($buildInput, $this->output) != 0) { |
||
| 98 | return 1; |
||
| 99 | } |
||
| 100 | |||
| 101 | // handles process |
||
| 102 | if (!$process->isStarted()) { |
||
| 103 | // set resource watcher |
||
| 104 | $finder = new Finder(); |
||
| 105 | $finder->files() |
||
| 106 | ->in($this->getPath()) |
||
| 107 | ->exclude($this->getBuilder()->getConfig()->getOutputPath()); |
||
| 108 | if (file_exists(Util::joinFile($this->getPath(), '.gitignore'))) { |
||
| 109 | $finder->ignoreVCSIgnored(true); |
||
| 110 | } |
||
| 111 | $hashContent = new Crc32ContentHash(); |
||
| 112 | $resourceCache = new ResourceCacheMemory(); |
||
| 113 | $resourceWatcher = new ResourceWatcher($resourceCache, $finder, $hashContent); |
||
| 114 | $resourceWatcher->initialize(); |
||
| 115 | // starts server |
||
| 116 | try { |
||
| 117 | if (function_exists('\pcntl_signal')) { |
||
| 118 | \pcntl_async_signals(true); |
||
| 119 | \pcntl_signal(SIGINT, [$this, 'tearDownServer']); |
||
| 120 | \pcntl_signal(SIGTERM, [$this, 'tearDownServer']); |
||
| 121 | } |
||
| 122 | $output->writeln( |
||
| 123 | sprintf('Starting server (<href=http://%s:%d>%s:%d</>)...', $host, $port, $host, $port) |
||
| 124 | ); |
||
| 125 | $process->start(); |
||
| 126 | if ($open) { |
||
| 127 | $output->writeln('Opening web browser...'); |
||
| 128 | Util\Plateform::openBrowser(sprintf('http://%s:%s', $host, $port)); |
||
| 129 | } |
||
| 130 | while ($process->isRunning()) { |
||
| 131 | $result = $resourceWatcher->findChanges(); |
||
| 132 | if ($result->hasChanges()) { |
||
| 133 | // re-builds |
||
| 134 | $output->writeln('<comment>Changes detected.</comment>'); |
||
| 135 | $output->writeln(''); |
||
| 136 | $buildCommand->run($buildInput, $output); |
||
| 137 | $output->writeln('<info>Server is runnning...</info>'); |
||
| 138 | } |
||
| 139 | usleep(1000000); // waits 1s |
||
| 140 | } |
||
| 141 | } catch (ProcessFailedException $e) { |
||
| 142 | $this->tearDownServer(); |
||
| 143 | |||
| 144 | throw new \Exception(sprintf($e->getMessage())); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | return 0; |
||
| 149 | } |
||
| 216 |