4747use function http_build_query ;
4848use function ini_get ;
4949use function is_file ;
50+ use function microtime ;
51+ use function min ;
5052use function parse_url ;
5153use function sprintf ;
5254use function unlink ;
5961final class FixerApplication
6062{
6163
64+ /**
65+ * A burst of changes, like a git checkout or a coding standard fixer at work,
66+ * is analysed once it is over, not as soon as its first file changes.
67+ */
68+ private const CHANGES_QUIET_SECONDS = 0.5 ;
69+
70+ /** Changes that never stop for that long are analysed this long after the first one anyway. */
71+ private const CHANGES_WAIT_SECONDS_LIMIT = 2.0 ;
72+
6273 /** @var PromiseInterface<string>|null */
6374 private PromiseInterface |null $ processInProgress = null ;
6475
@@ -168,18 +179,20 @@ public function run(
168179 $ encoder ,
169180 );
170181
171- $ this ->monitorFileChanges ($ loop , function (FileMonitorResult $ changes ) use ($ loop , $ mainScript , $ projectConfigFile , $ input , $ encoder ): void {
182+ $ this ->monitorFileChanges ($ loop , static function (FileMonitorResult $ changes ) use ($ encoder ): void {
183+ if (count ($ changes ->getChangedFiles ()) === 0 ) {
184+ return ;
185+ }
186+
187+ $ encoder ->write (['action ' => 'changedFiles ' , 'data ' => [
188+ 'paths ' => $ changes ->getChangedFiles (),
189+ ]]);
190+ }, function () use ($ loop , $ mainScript , $ projectConfigFile , $ input , $ encoder ): void {
172191 if ($ this ->processInProgress !== null ) {
173192 $ this ->processInProgress ->cancel ();
174193 $ this ->processInProgress = null ;
175194 }
176195
177- if (count ($ changes ->getChangedFiles ()) > 0 ) {
178- $ encoder ->write (['action ' => 'changedFiles ' , 'data ' => [
179- 'paths ' => $ changes ->getChangedFiles (),
180- ]]);
181- }
182-
183196 $ this ->analyse (
184197 $ loop ,
185198 $ mainScript ,
@@ -394,16 +407,19 @@ private function writeInfoFile(string $infoPath, string $version, string $branch
394407 }
395408
396409 /**
397- * @param callable(FileMonitorResult): void $hasChangesCallback
410+ * @param callable(FileMonitorResult): void $hasChangesCallback called for every poll with changes, the code shown can follow them right away
411+ * @param callable(): void $changesSettledCallback called once the changes stop, to analyse them
398412 */
399- private function monitorFileChanges (LoopInterface $ loop , callable $ hasChangesCallback ): void
413+ private function monitorFileChanges (LoopInterface $ loop , callable $ hasChangesCallback, callable $ changesSettledCallback ): void
400414 {
401415 if ($ this ->fileMonitor === null ) {
402416 throw new ShouldNotHappenException ();
403417 }
404418 $ fileMonitor = $ this ->fileMonitor ;
405419 $ interval = $ fileMonitor ->getPollInterval ();
406- $ callback = function () use (&$ callback , $ loop , $ hasChangesCallback , $ fileMonitor , $ interval ): void {
420+ $ firstChangeAt = null ;
421+ $ lastChangeAt = null ;
422+ $ callback = function () use (&$ callback , &$ firstChangeAt , &$ lastChangeAt , $ loop , $ hasChangesCallback , $ changesSettledCallback , $ fileMonitor , $ interval ): void {
407423 if (!$ this ->fileMonitorActive ) {
408424 $ loop ->addTimer ($ interval , $ callback );
409425 return ;
@@ -413,12 +429,29 @@ private function monitorFileChanges(LoopInterface $loop, callable $hasChangesCal
413429 return ;
414430 }
415431 $ changes = $ fileMonitor ->getChanges ();
432+ $ now = microtime (true );
416433
417434 if ($ changes ->hasAnyChanges ()) {
435+ $ firstChangeAt ??= $ now ;
436+ $ lastChangeAt = $ now ;
418437 $ hasChangesCallback ($ changes );
419438 }
420439
421- $ loop ->addTimer ($ interval , $ callback );
440+ if ($ firstChangeAt === null || $ lastChangeAt === null ) {
441+ $ loop ->addTimer ($ interval , $ callback );
442+ return ;
443+ }
444+
445+ if ($ now - $ lastChangeAt >= self ::CHANGES_QUIET_SECONDS || $ now - $ firstChangeAt >= self ::CHANGES_WAIT_SECONDS_LIMIT ) {
446+ $ firstChangeAt = null ;
447+ $ lastChangeAt = null ;
448+ $ changesSettledCallback ();
449+ $ loop ->addTimer ($ interval , $ callback );
450+ return ;
451+ }
452+
453+ // the monitor may poll less often than this, the end of the changes is noticed by the next poll
454+ $ loop ->addTimer (min ($ interval , self ::CHANGES_QUIET_SECONDS ), $ callback );
422455 };
423456 $ loop ->addTimer ($ interval , $ callback );
424457 }
0 commit comments