Completed
Push — master ( 4493f0...1ecc26 )
by Sergi Tur
04:12
created

Serve::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Acacha\Llum\Traits;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class Serve.
11
 *
12
 * @property OutputInterface $output
13
 */
14
trait Serve
15
{
16
    /**
17
     * Serve command.
18
     *
19
     * @param int $port
0 ignored issues
show
Bug introduced by
There is no parameter named $port. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
     */
21
    protected function serve(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
    {
23
        $port = $this->port($input);
24
        $continue = true;
25
        do {
26
            if ($this->check_port($port)) {
27
                $this->output->writeln('<info>Running php artisan serve --port='.$port.'</info>');
28
                exec('php artisan serve --port='.$port.' > /dev/null 2>&1 &');
29
                sleep(1);
30
                if (file_exists('/usr/bin/sensible-browser')) {
31
                    $this->output->writeln('<info>Opening http://localhost:'.$port.' with default browser</info>');
32
                    passthru('/usr/bin/sensible-browser http://localhost:'.$port);
33
                }
34
                $continue = false;
35
            }
36
            ++$port;
37
        } while ($continue);
38
    }
39
40
    /**
41
     * Check if port is in use.
42
     *
43
     * @param int    $port
44
     * @param string $host
45
     * @param int    $timeout
46
     *
47
     * @return bool
48
     */
49
    protected function check_port($port = 8000, $host = '127.0.0.1', $timeout = 3)
50
    {
51
        $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
52
        if (!$fp) {
53
            return true;
54
        } else {
55
            fclose($fp);
56
57
            return false;
58
        }
59
    }
60
61
    /**
62
     * Configure command.
63
     */
64
    public function configure()
65
    {
66
        parent::configure();
67
68
        $this
0 ignored issues
show
Bug introduced by
It seems like addArgument() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
            // configure an argument
70
            ->addArgument('port', InputArgument::OPTIONAL, 'Port number')
71
        ;
72
    }
73
74
    /**
75
     * Obtain port.
76
     *
77
     * @param InputInterface $input
78
     * @return mixed|string
79
     */
80
    protected function port(InputInterface $input) {
81
        $name = $input->getArgument('port');
82
        return isset($name) ? $name : 8080;
83
    }
84
85
}
86