PhpServer::host()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Robo\Task\Development;
4
5
use Robo\Task\Base\Exec;
6
7
/**
8
 * Runs PHP server and stops it when task finishes.
9
 *
10
 * ``` php
11
 * <?php
12
 * // run server in /public directory
13
 * $this->taskServer(8000)
14
 *  ->dir('public')
15
 *  ->run();
16
 *
17
 * // run with IP 0.0.0.0
18
 * $this->taskServer(8000)
19
 *  ->host('0.0.0.0')
20
 *  ->run();
21
 *
22
 * // execute server in background
23
 * $this->taskServer(8000)
24
 *  ->background()
25
 *  ->run();
26
 * ?>
27
 * ```
28
 */
29
class PhpServer extends Exec
30
{
31
    /**
32
     * @var int
33
     */
34
    protected $port;
35
36
    /**
37
     * @var string
38
     */
39
    protected $host = '127.0.0.1';
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected $command = 'php -S %s:%d ';
45
46
    /**
47
     * @param int $port
48
     */
49
    public function __construct($port)
50
    {
51
        $this->port = $port;
52
53
        if (strtolower(PHP_OS) === 'linux') {
54
            $this->command = 'exec php -S %s:%d ';
55
        }
56
    }
57
58
    /**
59
     * @param string $host
60
     *
61
     * @return $this
62
     */
63
    public function host($host)
64
    {
65
        $this->host = $host;
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $path
71
     *
72
     * @return $this
73
     */
74
    public function dir($path)
75
    {
76
        $this->command .= "-t $path";
77
        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...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getCommand()
84
    {
85
        return sprintf($this->command . $this->arguments, $this->host, $this->port);
86
    }
87
}
88