Lines 100.00% 10 / 10
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
SourceChangeTracker 100.00% 10 / 10 100.00% 3 / 3 6 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 findSettledChange 100.00% 7 / 7 100.00% 1 / 1 4
 markApplied 100.00% 2 / 2 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Reload;
6
7final class SourceChangeTracker
8{
9    private ?SourceFingerprint $pending = null;
10
11    public function __construct(private SourceFingerprint $applied) {}
12
13    // A change counts once two scans in a row agree on it, so a save that writes several files counts once.
14    public function findSettledChange(SourceFingerprint $current): ?SourceFingerprint
15    {
16        if ($current->matches($this->applied)) {
17            $this->pending = null;
18
19            return null;
20        }
21
22        if ($this->pending === null || !$current->matches($this->pending)) {
23            $this->pending = $current;
24
25            return null;
26        }
27
28        return $current;
29    }
30
31    public function markApplied(SourceFingerprint $fingerprint): void
32    {
33        $this->applied = $fingerprint;
34        $this->pending = null;
35    }
36}