Completed
Push — master ( d3a073...5737c8 )
by Greg
02:21
created

src/Task/Development/PhpServer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Task\Development;
3
4
use Robo\Task\Base\Exec;
5
6
/**
7
 * Runs PHP server and stops it when task finishes.
8
 *
9
 * ``` php
10
 * <?php
11
 * // run server in /public directory
12
 * $this->taskServer(8000)
13
 *  ->dir('public')
14
 *  ->run();
15
 *
16
 * // run with IP 0.0.0.0
17
 * $this->taskServer(8000)
18
 *  ->host('0.0.0.0')
19
 *  ->run();
20
 *
21
 * // execute server in background
22
 * $this->taskServer(8000)
23
 *  ->background()
24
 *  ->run();
25
 * ?>
26
 * ```
27
 */
28
class PhpServer extends Exec
29
{
30
    /**
31
     * @var int
32
     */
33
    protected $port;
34
35
    /**
36
     * @var string
37
     */
38
    protected $host = '127.0.0.1';
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    protected $command = 'php -S %s:%d ';
44
45
    /**
46
     * @param int $port
47
     */
48
    public function __construct($port)
49
    {
50
        $this->port = $port;
51
52
        if (strtolower(PHP_OS) === 'linux') {
53
            $this->command = 'exec php -S %s:%d ';
54
        }
55
    }
56
57
    /**
58
     * @param string $host
59
     *
60
     * @return $this
61
     */
62
    public function host($host)
63
    {
64
        $this->host = $host;
65
        return $this;
66
    }
67
68
    /**
69
     * @param string $path
70
     *
71
     * @return $this
72
     */
73
    public function dir($path)
74
    {
75
        $this->command .= "-t $path";
76
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Robo\Task\Development\PhpServer) is incompatible with the return type of the parent method Robo\Task\Base\Exec::dir of type Robo\Common\ExecTrait.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function getCommand()
83
    {
84
        return sprintf($this->command . $this->arguments, $this->host, $this->port);
85
    }
86
}
87