Server::run()   B
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 61
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 41
c 5
b 0
f 0
dl 0
loc 61
rs 7.6666
cc 10
nc 15
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Chuck\Server;
6
7
use Conia\Cli\Command;
8
use Conia\Cli\Opts;
9
use Throwable;
10
11
/** @psalm-api */
12
class Server extends Command
13
{
14
    protected string $name = 'server';
15
    protected string $description = 'Serve the application on the builtin PHP server';
16
17
    public function __construct(
18
        protected readonly string $docroot,
19
        protected readonly int $port = 1983,
20
    ) {
21
    }
22
23
    public function run(): string|int
24
    {
25
        $docroot = $this->docroot;
26
        $port = (string)$this->port;
27
28
        try {
29
            $sizeStr = trim(exec('stty size'));
30
            if ($sizeStr) {
31
                $size = explode(' ', $sizeStr);
32
                $columns = $size[1];
33
            } else {
34
                $columns = '80';
35
            }
36
        } catch (Throwable) {
37
            $columns = '80';
38
        }
39
40
        $opts = new Opts();
41
        $host = $opts->get('-h', $opts->get('--host', 'localhost'));
42
        $port = $opts->get('-p', $opts->get('--port', $port));
43
        $filter = $opts->get('-f', $opts->get('--filter', ''));
44
        $quiet = $opts->has('-q');
45
46
        $descriptors = [
47
            0 => ['pipe', 'r'],
48
            1 => ['pipe', 'w'],
49
            2 => ['pipe', 'w'],
50
        ];
51
        $process = proc_open(
52
            'CONIA_CLI_SERVER=1 ' .
53
            "CONIA_DOCUMENT_ROOT={$docroot} " .
54
                "CONIA_TERMINAL_COLUMNS={$columns} " .
55
                "php -S {$host}:{$port} " .
56
                ($quiet ? '-q ' : '') .
57
                "    -t {$docroot}" . DIRECTORY_SEPARATOR . ' ' . __DIR__ . DIRECTORY_SEPARATOR . 'CliRouter.php ',
58
            $descriptors,
59
            $pipes
60
        );
61
62
        if (is_resource($process)) {
63
            while (!feof($pipes[1])) {
64
                $output = fgets($pipes[2], 1024);
65
66
                if (strlen($output) === 0) {
67
                    break;
68
                }
69
70
                if (!preg_match('/^\[.*?\] \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}/', $output)) {
71
                    $pos = (int)strpos($output, ']');
72
73
                    if (!$filter || !preg_match($filter, substr($output, (int)strpos($output, '/')))) {
74
                        echo substr($output, $pos + 2);
75
                    }
76
                }
77
            }
78
79
            fclose($pipes[1]);
80
            proc_close($process);
81
        }
82
83
        return 0;
84
    }
85
}
86