1 | <?php |
||
21 | class ProcessBase extends Process |
||
22 | { |
||
23 | /** |
||
24 | * @var OutputStyle |
||
25 | */ |
||
26 | protected $output; |
||
27 | |||
28 | /** |
||
29 | * @var OutputInterface |
||
30 | */ |
||
31 | protected $stderr; |
||
32 | |||
33 | private $simulated = false; |
||
34 | |||
35 | private $verbose = false; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * Symfony 4 style constructor for creating Process instances from strings. |
||
44 | * @param string $command The commandline string to run |
||
45 | * @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
||
46 | * @param array|null $env The environment variables or null to use the same environment as the current PHP process |
||
47 | * @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input |
||
48 | * @param int|float|null $timeout The timeout in seconds or null to disable |
||
49 | * @return Process |
||
50 | */ |
||
51 | public static function fromShellCommandline($command, $cwd = null, array $env = null, $input = null, $timeout = 60) |
||
58 | |||
59 | /** |
||
60 | * realtimeStdout returns the output stream that realtime output |
||
61 | * should be sent to (if applicable) |
||
62 | * |
||
63 | * @return OutputStyle $output |
||
64 | */ |
||
65 | public function realtimeStdout() |
||
69 | |||
70 | protected function realtimeStderr() |
||
81 | |||
82 | /** |
||
83 | * setRealtimeOutput allows the caller to inject an OutputStyle object |
||
84 | * that will be used to stream realtime output if applicable. |
||
85 | * |
||
86 | * @param OutputStyle $output |
||
87 | */ |
||
88 | public function setRealtimeOutput(OutputInterface $output, $stderr = null) |
||
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function isVerbose() |
||
101 | |||
102 | /** |
||
103 | * @param bool $verbose |
||
104 | */ |
||
105 | public function setVerbose($verbose) |
||
109 | |||
110 | /** |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isSimulated() |
||
117 | |||
118 | /** |
||
119 | * @param bool $simulated |
||
120 | */ |
||
121 | public function setSimulated($simulated) |
||
125 | |||
126 | /** |
||
127 | * @return LoggerInterface |
||
128 | */ |
||
129 | public function getLogger() |
||
133 | |||
134 | /** |
||
135 | * @param LoggerInterface $logger |
||
136 | */ |
||
137 | public function setLogger($logger) |
||
141 | |||
142 | /** |
||
143 | * @inheritDoc |
||
144 | */ |
||
145 | public function start(callable $callback = null, $env = array()) |
||
161 | |||
162 | /** |
||
163 | * Get Process output and decode its JSON. |
||
164 | * |
||
165 | * @return array |
||
166 | * An associative array. |
||
167 | */ |
||
168 | public function getOutputAsJson() |
||
188 | |||
189 | /** |
||
190 | * Allow for a certain amount of resiliancy in the output received when |
||
191 | * json is expected. |
||
192 | * |
||
193 | * @param string $data |
||
194 | * @return string |
||
195 | */ |
||
196 | protected function removeNonJsonJunk($data) |
||
216 | |||
217 | /** |
||
218 | * Return a realTime output object. |
||
219 | * |
||
220 | * @return callable |
||
221 | */ |
||
222 | public function showRealtime() |
||
228 | } |
||
229 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: