17
17
use Psalm \Internal \FileManipulation \FileManipulationBuffer ;
18
18
use Psalm \Internal \FileManipulation \FunctionDocblockManipulator ;
19
19
use Psalm \Internal \FileManipulation \PropertyDocblockManipulator ;
20
+ use Psalm \Internal \Fork \AnalyzerTask ;
20
21
use Psalm \Internal \Fork \InitAnalyzerTask ;
21
22
use Psalm \Internal \Fork \Pool ;
22
23
use Psalm \Internal \Fork \ShutdownAnalyzerTask ;
37
38
use function count ;
38
39
use function explode ;
39
40
use function implode ;
40
- use function intdiv ;
41
41
use function ksort ;
42
42
use function number_format ;
43
43
use function pathinfo ;
@@ -300,60 +300,22 @@ private function doAnalysis(ProjectAnalyzer $project_analyzer, int $pool_size):
300
300
301
301
$ codebase = $ project_analyzer ->getCodebase ();
302
302
303
- $ task_done_closure = $ this ->taskDoneClosure (...);
303
+ $ task_done_closure = $ this ->progress -> taskDone (...);
304
304
305
305
if ($ pool_size > 1 && count ($ this ->files_to_analyze ) > $ pool_size ) {
306
- $ shuffle_count = $ pool_size + 1 ;
307
-
308
- $ file_paths = array_values ($ this ->files_to_analyze );
309
-
310
- $ count = count ($ file_paths );
311
- /** @var int<0, max> */
312
- $ middle = intdiv ($ count , $ shuffle_count );
313
- $ remainder = $ count % $ shuffle_count ;
314
-
315
- $ new_file_paths = [];
316
-
317
- for ($ i = 0 ; $ i < $ shuffle_count ; $ i ++) {
318
- for ($ j = 0 ; $ j < $ middle ; $ j ++) {
319
- if ($ j * $ shuffle_count + $ i < $ count ) {
320
- $ new_file_paths [] = $ file_paths [$ j * $ shuffle_count + $ i ];
321
- }
322
- }
323
-
324
- if ($ remainder ) {
325
- $ new_file_paths [] = $ file_paths [$ middle * $ shuffle_count + $ remainder - 1 ];
326
- $ remainder --;
327
- }
328
- }
329
-
330
- $ process_file_paths = [];
331
-
332
- $ i = 0 ;
333
-
334
- foreach ($ new_file_paths as $ file_path ) {
335
- $ process_file_paths [$ i % $ pool_size ][] = $ file_path ;
336
- ++$ i ;
337
- }
338
-
339
306
// Run analysis one file at a time, splitting the set of
340
307
// files up among a given number of child processes.
341
308
$ pool = new Pool (
342
- $ this ->config ,
343
- $ process_file_paths ,
344
- InitAnalyzerTask::runStatic (...),
345
- fn (int $ _ , string $ file_path ) => self ::analysisWorker ($ this ->config , $ this ->progress , $ file_path ),
346
- ShutdownAnalyzerTask::getPoolData (...),
347
- $ task_done_closure ,
309
+ $ pool_size ,
310
+ $ project_analyzer ->progress ,
348
311
);
349
312
350
313
$ this ->progress ->debug ('Forking analysis ' . "\n" );
351
314
352
315
// Wait for all tasks to complete and collect the results.
353
- /**
354
- * @var array<int, WorkerData>
355
- */
356
- $ forked_pool_data = $ pool ->wait ();
316
+ $ pool ->runAll (new InitAnalyzerTask );
317
+ $ pool ->run ($ this ->files_to_analyze , AnalyzerTask::class, $ task_done_closure );
318
+ $ forked_pool_data = $ pool ->runAll (new ShutdownAnalyzerTask );
357
319
358
320
$ this ->progress ->debug ('Collecting forked analysis results ' . "\n" );
359
321
@@ -467,10 +429,7 @@ private function doAnalysis(ProjectAnalyzer $project_analyzer, int $pool_size):
467
429
}
468
430
} else {
469
431
foreach ($ this ->files_to_analyze as $ file_path => $ _ ) {
470
- self ::analysisWorker ($ this ->config , $ this ->progress , $ file_path );
471
-
472
- $ issues = IssueBuffer::getIssuesDataForFile ($ file_path );
473
- $ task_done_closure ($ issues );
432
+ $ task_done_closure (self ::analysisWorker ($ this ->config , $ this ->progress , $ file_path ));
474
433
}
475
434
}
476
435
}
@@ -1500,41 +1459,22 @@ public function isMethodAlreadyAnalyzed(string $file_path, string $method_id, bo
1500
1459
return isset ($ this ->analyzed_methods [$ file_path ][$ method_id ]);
1501
1460
}
1502
1461
1503
- /**
1504
- * @param list<IssueData> $issues
1505
- */
1506
- private function taskDoneClosure (array $ issues ): void
1507
- {
1508
- $ has_error = false ;
1509
- $ has_info = false ;
1510
-
1511
- foreach ($ issues as $ issue ) {
1512
- switch ($ issue ->severity ) {
1513
- case IssueData::SEVERITY_INFO :
1514
- $ has_info = true ;
1515
- break ;
1516
- default :
1517
- $ has_error = true ;
1518
- break ;
1519
- }
1520
- }
1521
-
1522
- $ this ->progress ->taskDone ($ has_error ? 2 : ($ has_info ? 1 : 0 ));
1523
- }
1524
-
1525
1462
/**
1526
1463
* @internal
1527
- * @return list<IssueData>
1528
1464
*/
1529
- public static function analysisWorker (Config $ config , Progress $ progress , string $ file_path ): array
1465
+ public static function analysisWorker (Config $ config , Progress $ progress , string $ file_path ): int
1530
1466
{
1531
1467
$ extension = pathinfo ($ file_path , PATHINFO_EXTENSION );
1532
1468
1533
1469
$ file_name = $ config ->shortenFileName ($ file_path );
1534
1470
1535
1471
$ filetype_analyzers = $ config ->getFiletypeAnalyzers ();
1536
1472
if (isset ($ filetype_analyzers [$ extension ])) {
1537
- $ file_analyzer = new $ filetype_analyzers [$ extension ](ProjectAnalyzer::getInstance (), $ file_path , $ file_name );
1473
+ $ file_analyzer = new $ filetype_analyzers [$ extension ](
1474
+ ProjectAnalyzer::getInstance (),
1475
+ $ file_path ,
1476
+ $ file_name
1477
+ );
1538
1478
} else {
1539
1479
$ file_analyzer = new FileAnalyzer (ProjectAnalyzer::getInstance (), $ file_path , $ file_name );
1540
1480
}
@@ -1546,6 +1486,20 @@ public static function analysisWorker(Config $config, Progress $progress, string
1546
1486
$ file_analyzer ->clearSourceBeforeDestruction ();
1547
1487
unset($ file_analyzer );
1548
1488
1549
- return IssueBuffer::getIssuesDataForFile ($ file_path );
1489
+ $ has_error = false ;
1490
+ $ has_info = false ;
1491
+
1492
+ foreach (IssueBuffer::getIssuesDataForFile ($ file_path ) as $ issue ) {
1493
+ switch ($ issue ->severity ) {
1494
+ case IssueData::SEVERITY_INFO :
1495
+ $ has_info = true ;
1496
+ break ;
1497
+ default :
1498
+ $ has_error = true ;
1499
+ break ;
1500
+ }
1501
+ }
1502
+
1503
+ return $ has_error ? 2 : ($ has_info ? 1 : 0 );
1550
1504
}
1551
1505
}
0 commit comments