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
|
|||
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 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.