Passed
Push — master ( cbb9d7...90f9be )
by Alexander
02:17
created

Serve   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 80.43%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 50
c 2
b 0
f 0
dl 0
loc 87
ccs 37
cts 46
cp 0.8043
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A isAddressTaken() 0 9 2
B execute() 0 47 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console\Command;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Yiisoft\Yii\Console\ExitCode;
14
15
class Serve extends Command
16
{
17
    public const EXIT_CODE_NO_DOCUMENT_ROOT = 2;
18
    public const EXIT_CODE_NO_ROUTING_FILE = 3;
19
    public const EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS = 5;
20
21
    private const DEFAULT_PORT = 8080;
22
    private const DEFAULT_DOCROOT = 'public';
23
    private const DEFAULT_ROUTER = 'public/index.php';
24
25
    protected static $defaultName = 'serve';
26
27 2
    public function configure(): void
28
    {
29
        $this
30 2
            ->setDescription('Runs PHP built-in web server')
31 2
            ->setHelp('In order to access server from remote machines use 0.0.0.0:8000. That is especially useful when running server in a virtual machine.')
32 2
            ->addArgument('address', InputArgument::OPTIONAL, 'Host to serve at', 'localhost')
33 2
            ->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to serve at', self::DEFAULT_PORT)
34 2
            ->addOption('docroot', 't', InputOption::VALUE_OPTIONAL, 'Document root to serve from', self::DEFAULT_DOCROOT)
35 2
            ->addOption('router', 'r', InputOption::VALUE_OPTIONAL, 'Path to router script', self::DEFAULT_ROUTER)
36 2
            ->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.');
37 2
    }
38
39 2
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41 2
        $io = new SymfonyStyle($input, $output);
42 2
        $address = $input->getArgument('address');
43
44 2
        $port = $input->getOption('port');
45 2
        $docroot = $input->getOption('docroot');
46 2
        $router = $input->getOption('router');
47 2
        if (!file_exists(self::DEFAULT_ROUTER)) {
48 2
            $router = null;
49
        }
50
51 2
        $env = $input->getOption('env');
52
53 2
        $documentRoot = getcwd() . '/' . $docroot; // TODO: can we do it better?
54
55 2
        if (strpos($address, ':') === false) {
0 ignored issues
show
Bug introduced by
It seems like $address can also be of type string[]; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        if (strpos(/** @scrutinizer ignore-type */ $address, ':') === false) {
Loading history...
56 2
            $address .= ':' . $port;
57
        }
58
59 2
        if (!is_dir($documentRoot)) {
60 1
            $io->error("Document root \"$documentRoot\" does not exist.");
61 1
            return self::EXIT_CODE_NO_DOCUMENT_ROOT;
62
        }
63
64 1
        if ($this->isAddressTaken($address)) {
0 ignored issues
show
Bug introduced by
It seems like $address can also be of type null and string[]; however, parameter $address of Yiisoft\Yii\Console\Comm...Serve::isAddressTaken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        if ($this->isAddressTaken(/** @scrutinizer ignore-type */ $address)) {
Loading history...
65
            $io->error("http://$address is taken by another process.");
66
            return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS;
67
        }
68
69 1
        if ($router !== null && !file_exists($router)) {
0 ignored issues
show
Bug introduced by
It seems like $router can also be of type string[]; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        if ($router !== null && !file_exists(/** @scrutinizer ignore-type */ $router)) {
Loading history...
70
            $io->error("Routing file \"$router\" does not exist.");
71
            return self::EXIT_CODE_NO_ROUTING_FILE;
72
        }
73
74 1
        $output->writeLn("Server started on <href=http://{$address}/>http://{$address}/</>");
75 1
        $output->writeLn("Document root is \"{$documentRoot}\"");
76 1
        if ($router) {
77
            $output->writeLn("Routing file is \"$router\"");
78
        }
79 1
        $output->writeLn('Quit the server with CTRL-C or COMMAND-C.');
80
81 1
        if ($env === 'test') {
82 1
            return ExitCode::OK;
83
        }
84
85
        passthru('"' . PHP_BINARY . '"' . " -S {$address} -t \"{$documentRoot}\" $router");
86
    }
87
88
    /**
89
     * @param string $address server address
90
     *
91
     * @return bool if address is already in use
92
     */
93 1
    private function isAddressTaken(string $address): bool
94
    {
95 1
        [$hostname, $port] = explode(':', $address);
96 1
        $fp = @fsockopen($hostname, (int)$port, $errno, $errstr, 3);
97 1
        if ($fp === false) {
98 1
            return false;
99
        }
100
        fclose($fp);
101
        return true;
102
    }
103
}
104