2424use Rector \Parallel \ValueObject \Schedule ;
2525use Rector \ValueObject \Error \SystemError ;
2626use Rector \ValueObject \ProcessResult ;
27- use Rector \ValueObject \Reporting \FileDiff ;
2827use Symfony \Component \Console \Command \Command ;
2928use Symfony \Component \Console \Input \InputInterface ;
3029use Throwable ;
@@ -67,16 +66,7 @@ public function process(
6766 // basic properties setup
6867 $ numberOfProcesses = $ schedule ->getNumberOfProcesses ();
6968
70- // initial counters
71-
72- /** @var FileDiff[] $fileDiffs */
73- $ fileDiffs = [];
74-
75- /** @var SystemError[] $systemErrors */
76- $ systemErrors = [];
77-
78- /** @var array<string, array<string, true>> $usedSkips */
79- $ usedSkips = [];
69+ $ parallelResultCollector = new ParallelResultCollector ();
8070
8171 $ tcpServer = new TcpServer ('127.0.0.1:0 ' , $ streamSelectLoop );
8272 $ this ->processPool = new ProcessPool ($ tcpServer );
@@ -117,18 +107,17 @@ public function process(
117107
118108 $ systemErrorsCount = 0 ;
119109 $ reachedSystemErrorsCountLimit = false ;
120- $ totalChanged = 0 ;
121110
122111 $ handleErrorCallable = function (Throwable $ throwable ) use (
123- & $ systemErrors ,
112+ $ parallelResultCollector ,
124113 &$ systemErrorsCount ,
125114 &$ reachedSystemErrorsCountLimit
126115 ): void {
127- $ systemErrors [] = new SystemError (
116+ $ parallelResultCollector -> collectSystemError ( new SystemError (
128117 $ throwable ->getMessage (),
129118 $ throwable ->getFile (),
130119 $ throwable ->getLine (),
131- );
120+ )) ;
132121
133122 ++$ systemErrorsCount ;
134123 $ reachedSystemErrorsCountLimit = true ;
@@ -144,9 +133,7 @@ public function process(
144133 $ fileChunksBudgetPerProcess = [];
145134
146135 $ processSpawner = function () use (
147- &$ systemErrors ,
148- &$ fileDiffs ,
149- &$ usedSkips ,
136+ $ parallelResultCollector ,
150137 &$ jobs ,
151138 $ postFileCallback ,
152139 &$ systemErrorsCount ,
@@ -158,8 +145,7 @@ public function process(
158145 $ timeoutInSeconds ,
159146 $ handleErrorCallable ,
160147 &$ fileChunksBudgetPerProcess ,
161- &$ processSpawner ,
162- &$ totalChanged
148+ &$ processSpawner
163149 ): void {
164150 $ processIdentifier = Random ::generate ();
165151 $ workerCommandLine = $ this ->workerCommandLineFactory ->create (
@@ -178,17 +164,14 @@ public function process(
178164 // 1. callable on data
179165 function (array $ json ) use (
180166 $ parallelProcess ,
181- &$ systemErrors ,
182- &$ fileDiffs ,
183- &$ usedSkips ,
167+ $ parallelResultCollector ,
184168 &$ jobs ,
185169 $ postFileCallback ,
186170 &$ systemErrorsCount ,
187171 &$ reachedInternalErrorsCountLimit ,
188172 $ processIdentifier ,
189173 &$ fileChunksBudgetPerProcess ,
190- &$ processSpawner ,
191- &$ totalChanged
174+ &$ processSpawner
192175 ): void {
193176 /** @var array{
194177 * total_changed: int,
@@ -198,30 +181,7 @@ function (array $json) use (
198181 * system_errors_count: int,
199182 * used_skips: array<string, string[]>
200183 * } $json */
201- $ totalChanged += $ json [Bridge::TOTAL_CHANGED ];
202-
203- foreach ($ json [Bridge::USED_SKIPS ] as $ skip => $ paths ) {
204- $ usedSkips [$ skip ] ??= [];
205- foreach ($ paths as $ path ) {
206- $ usedSkips [$ skip ][$ path ] = true ;
207- }
208- }
209-
210- // decode arrays to objects
211- foreach ($ json [Bridge::SYSTEM_ERRORS ] as $ jsonError ) {
212- if (is_string ($ jsonError )) {
213- $ systemErrors [] = new SystemError ('System error: ' . $ jsonError );
214- continue ;
215- }
216-
217- $ systemErrors [] = SystemError ::decode ($ jsonError );
218- }
219-
220- foreach ($ json [Bridge::FILE_DIFFS ] as $ jsonFileDiff ) {
221- $ fileDiffs [] = FileDiff ::decode ($ jsonFileDiff );
222- }
223-
224- $ postFileCallback ($ json [Bridge::FILES_COUNT ]);
184+ $ postFileCallback ($ parallelResultCollector ->collectWorkerResult ($ json ));
225185
226186 $ systemErrorsCount += $ json [Bridge::SYSTEM_ERRORS_COUNT ];
227187 if ($ systemErrorsCount >= self ::SYSTEM_ERROR_LIMIT ) {
@@ -254,7 +214,7 @@ function (array $json) use (
254214 $ handleErrorCallable ,
255215
256216 // 3. callable on exit
257- function ($ exitCode , string $ stdErr ) use (& $ systemErrors , $ processIdentifier ): void {
217+ function ($ exitCode , string $ stdErr ) use ($ parallelResultCollector , $ processIdentifier ): void {
258218 $ this ->processPool ->tryQuitProcess ($ processIdentifier );
259219 if ($ exitCode === Command::SUCCESS ) {
260220 return ;
@@ -264,7 +224,9 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
264224 return ;
265225 }
266226
267- $ systemErrors [] = new SystemError ('Child process error: ' . $ stdErr );
227+ $ parallelResultCollector ->collectSystemError (new SystemError (
228+ 'Child process error: ' . $ stdErr
229+ ));
268230 }
269231 );
270232
@@ -283,17 +245,12 @@ function ($exitCode, string $stdErr) use (&$systemErrors, $processIdentifier): v
283245 $ streamSelectLoop ->run ();
284246
285247 if ($ reachedSystemErrorsCountLimit ) {
286- $ systemErrors [] = new SystemError (sprintf (
248+ $ parallelResultCollector -> collectSystemError ( new SystemError (sprintf (
287249 'Reached system errors count limit of %d, exiting... ' ,
288250 self ::SYSTEM_ERROR_LIMIT
289- ));
290- }
291-
292- $ mergedUsedSkips = [];
293- foreach ($ usedSkips as $ skip => $ paths ) {
294- $ mergedUsedSkips [$ skip ] = array_keys ($ paths );
251+ )));
295252 }
296253
297- return new ProcessResult ( $ systemErrors , $ fileDiffs , $ totalChanged , $ mergedUsedSkips );
254+ return $ parallelResultCollector -> createProcessResult ( );
298255 }
299256}
0 commit comments