Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
16 | class Messenger extends EventEmitter |
||
17 | { |
||
18 | const INTERVAL = 0.1; |
||
19 | const TERMINATE_RPC = 'wyrihaximus.react.child-process.messenger.terminate'; |
||
20 | |||
21 | /** |
||
22 | * @var Stream |
||
23 | */ |
||
24 | protected $stdin; |
||
25 | |||
26 | /** |
||
27 | * @var Stream |
||
28 | */ |
||
29 | protected $stdout; |
||
30 | |||
31 | /** |
||
32 | * @var Stream |
||
33 | */ |
||
34 | protected $stderr; |
||
35 | |||
36 | /** |
||
37 | * @var OutstandingCalls |
||
38 | */ |
||
39 | protected $outstandingRpcCalls; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $rpcs = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $options = []; |
||
50 | |||
51 | /** |
||
52 | * @var string[] |
||
53 | */ |
||
54 | protected $buffers = [ |
||
55 | 'stdin' => '', |
||
56 | 'stdout' => '', |
||
57 | 'stderr' => '', |
||
58 | ]; |
||
59 | |||
60 | protected $defaultOptions = [ |
||
61 | 'lineClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Line', |
||
62 | 'messageFactoryClass' => 'WyriHaximus\React\ChildProcess\Messenger\Messages\Factory', |
||
63 | 'lineOptions' => [], |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * @param Stream $stdin |
||
68 | * @param Stream $stdout |
||
69 | * @param Stream $stderr |
||
70 | * @param array $options |
||
71 | */ |
||
72 | 10 | public function __construct(Stream $stdin, Stream $stdout, Stream $stderr, array $options) |
|
83 | |||
84 | /** |
||
85 | * Forward any unknown calls when there is a call forward possible. |
||
86 | * |
||
87 | * @param string $name |
||
88 | * @param array $arguments |
||
89 | * |
||
90 | * @return mixed |
||
91 | */ |
||
92 | 1 | public function __call($name, array $arguments) |
|
100 | |||
101 | /** |
||
102 | * @param string $target |
||
103 | * @param callable $listener |
||
104 | */ |
||
105 | 2 | public function registerRpc($target, callable $listener) |
|
109 | |||
110 | /** |
||
111 | * @param string $target |
||
112 | */ |
||
113 | 1 | public function deregisterRpc($target) |
|
117 | |||
118 | /** |
||
119 | * @param string $target |
||
120 | * @return bool |
||
121 | */ |
||
122 | 1 | public function hasRpc($target) |
|
126 | |||
127 | /** |
||
128 | * @param $target |
||
129 | * @param $payload |
||
130 | * @return React\Promise\PromiseInterface |
||
131 | */ |
||
132 | 2 | public function callRpc($target, $payload) |
|
133 | { |
||
134 | try { |
||
135 | 2 | $promise = $this->rpcs[$target]($payload, $this); |
|
136 | 2 | if ($promise instanceof PromiseInterface) { |
|
137 | 1 | return $promise; |
|
138 | } |
||
139 | |||
140 | 1 | throw new \Exception('RPC must return promise'); |
|
141 | 1 | } catch (\Exception $exception) { |
|
142 | 1 | return new RejectedPromise($exception); |
|
143 | } catch (\Throwable $exception) { |
||
|
|||
144 | return new RejectedPromise($exception); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param Message $message |
||
150 | */ |
||
151 | 1 | public function message(Message $message) |
|
152 | { |
||
153 | 1 | $this->write($this->createLine($message)); |
|
154 | 1 | } |
|
155 | |||
156 | /** |
||
157 | * @param Error $error |
||
158 | */ |
||
159 | 1 | public function error(Error $error) |
|
160 | { |
||
161 | 1 | $this->writeErr($this->createLine($error)); |
|
162 | 1 | } |
|
163 | |||
164 | /** |
||
165 | * @param string $uniqid |
||
166 | * @return OutstandingCall |
||
167 | */ |
||
168 | 1 | public function getOutstandingCall($uniqid) |
|
169 | { |
||
170 | 1 | return $this->outstandingRpcCalls->getCall($uniqid); |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param Rpc $rpc |
||
175 | * @return \React\Promise\Promise |
||
176 | */ |
||
177 | 2 | public function rpc(Rpc $rpc) |
|
178 | { |
||
179 | $callReference = $this->outstandingRpcCalls->newCall(function () { |
||
180 | 2 | }); |
|
181 | |||
182 | 2 | $this->write($this->createLine($rpc->setUniqid($callReference->getUniqid()))); |
|
183 | |||
184 | 2 | return $callReference->getDeferred()->promise(); |
|
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param ActionableMessageInterface $line |
||
189 | * @return LineInterface |
||
190 | */ |
||
191 | 4 | public function createLine(ActionableMessageInterface $line) |
|
192 | { |
||
193 | 4 | $lineCLass = $this->options['lineClass']; |
|
194 | |||
195 | 4 | return (string) new $lineCLass($line, $this->options['lineOptions']); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return \React\Promise\Promise |
||
200 | */ |
||
201 | 1 | public function softTerminate() |
|
202 | { |
||
203 | 1 | return $this->rpc(MessageFactory::rpc(static::TERMINATE_RPC)); |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return Stream |
||
208 | */ |
||
209 | 3 | public function getStdin() |
|
210 | { |
||
211 | 3 | return $this->stdin; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @return Stream |
||
216 | */ |
||
217 | 3 | public function getStdout() |
|
218 | { |
||
219 | 3 | return $this->stdout; |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return Stream |
||
224 | */ |
||
225 | 3 | public function getStderr() |
|
229 | |||
230 | 10 | protected function attachMessenger() |
|
231 | { |
||
232 | /** |
||
233 | * @todo duplicated code much? |
||
234 | */ |
||
235 | 10 | View Code Duplication | if (isset($this->options['read_err'])) { |
236 | 2 | $streamName = $this->options['read_err']; |
|
237 | $this->$streamName->on('data', function ($data) use ($streamName) { |
||
238 | $this->onData($data, $streamName); |
||
251 | |||
252 | /** |
||
253 | * @param string $line |
||
254 | */ |
||
255 | 3 | View Code Duplication | protected function write($line) |
263 | |||
264 | /** |
||
265 | * @param string $line |
||
266 | */ |
||
267 | 1 | View Code Duplication | protected function writeErr($line) |
275 | |||
276 | /** |
||
277 | * @param string $data |
||
278 | * @param string $source |
||
279 | */ |
||
280 | 3 | protected function onData($data, $source) |
|
292 | |||
293 | /** |
||
294 | * @param array $messages |
||
295 | * @param string $source |
||
296 | */ |
||
297 | 2 | protected function iterateMessages(array $messages, $source) |
|
310 | } |
||
311 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.