chore: refactors exceptions

This commit is contained in:
nuno maduro
2026-08-07 15:26:55 +01:00
parent 42d9b777bf
commit 8c554a81e2
54 changed files with 145 additions and 94 deletions
+9 -3
View File
@@ -17,7 +17,7 @@ use Symfony\Component\Process\Process;
*/
final readonly class BaselineSync
{
private const string WORKFLOW_FILE = 'tia-baseline.yml';
private const string DEFAULT_WORKFLOW_FILE = 'tia-baseline.yml';
private const string ARTIFACT_NAME = 'pest-tia-baseline';
@@ -57,8 +57,14 @@ final readonly class BaselineSync
public function __construct(
private State $state,
private OutputInterface $output,
private WatchPatterns $watchPatterns,
) {}
private function workflowFile(): string
{
return $this->watchPatterns->baselineWorkflow() ?? self::DEFAULT_WORKFLOW_FILE;
}
private function renderBadge(string $type, string $content): void
{
View::render('components.badge', ['type' => $type, 'content' => $content]);
@@ -276,7 +282,7 @@ final readonly class BaselineSync
Panic::with(new BaselineFetchFailed(
sprintf('%s — %s', $contextPrefix, $diagnosis['message']),
'Verify workflow tia-baseline.yml, artifact pest-tia-baseline, and gh token scope.',
sprintf('Verify workflow [%s], artifact [%s], and gh token scope.', $this->workflowFile(), self::ARTIFACT_NAME),
$hasAnchor,
));
}
@@ -528,7 +534,7 @@ final readonly class BaselineSync
$process = new Process([
'gh', 'run', 'list',
'-R', $repo,
'--workflow', self::WORKFLOW_FILE,
'--workflow', $this->workflowFile(),
'--status', 'success',
'--limit', '1',
'--json', 'databaseId',
+5 -1
View File
@@ -51,12 +51,16 @@ final class Configuration
/**
* @return $this
*/
public function baselined(): self
public function baselined(?string $workflow = null): self
{
/** @var WatchPatterns $watchPatterns */
$watchPatterns = Container::getInstance()->get(WatchPatterns::class);
$watchPatterns->markBaselined();
if ($workflow !== null) {
$watchPatterns->setBaselineWorkflow($workflow);
}
return $this;
}
+12 -6
View File
@@ -27,10 +27,12 @@ final class CoverageCollector
}
try {
$lineCoverage = PhpUnitCodeCoverage::instance()
$data = PhpUnitCodeCoverage::instance()
->codeCoverage()
->getData()
->lineCoverage();
->getData();
$lineCoverage = $data->lineCoverage();
$idByIndex = $data->testIds();
} catch (Throwable) {
return [];
}
@@ -46,12 +48,16 @@ final class CoverageCollector
continue;
}
foreach ($hits as $id) {
$testIds[$id] = true;
foreach (array_keys($hits) as $index) {
if (! isset($idByIndex[$index])) {
continue;
}
$testIds[$index] = $idByIndex[$index];
}
}
foreach (array_keys($testIds) as $testId) {
foreach ($testIds as $testId) {
$testFile = $this->testIdToFile($testId);
if ($testFile === null) {
+29 -9
View File
@@ -100,19 +100,32 @@ final class CoverageMerger
}
$cachedData = $cached->getData();
$staleIndexes = [];
foreach ($cachedData->testIds() as $index => $id) {
if (in_array($id, $currentIds, true)) {
$staleIndexes[$index] = true;
}
}
if ($staleIndexes === []) {
return;
}
$lineCoverage = $cachedData->lineCoverage();
foreach ($lineCoverage as $file => $lines) {
foreach ($lines as $line => $ids) {
if ($ids === null) {
foreach ($lines as $line => $hits) {
if ($hits === null) {
continue;
}
if ($ids === []) {
if ($hits === []) {
continue;
}
$filtered = array_values(array_diff($ids, $currentIds));
$filtered = array_diff_key($hits, $staleIndexes);
if ($filtered !== $ids) {
if ($filtered !== $hits) {
$lineCoverage[$file][$line] = $filtered;
}
}
@@ -126,21 +139,28 @@ final class CoverageMerger
*/
private static function collectTestIds(CodeCoverage $coverage): array
{
$data = $coverage->getData();
$idByIndex = $data->testIds();
$ids = [];
foreach ($coverage->getData()->lineCoverage() as $lines) {
foreach ($data->lineCoverage() as $lines) {
foreach ($lines as $hits) {
if ($hits === null) {
continue;
}
foreach ($hits as $id) {
$ids[$id] = true;
foreach (array_keys($hits) as $index) {
if (! isset($idByIndex[$index])) {
continue;
}
$ids[$index] = $idByIndex[$index];
}
}
}
return array_keys($ids);
return array_values($ids);
}
private static function state(): State
+15
View File
@@ -46,6 +46,8 @@ final class WatchPatterns
private ?string $defaultBranch = null;
private ?string $baselineWorkflow = null;
public function useDefaults(string $projectRoot): void
{
$testPath = TestSuite::getInstance()->testPath;
@@ -189,6 +191,18 @@ final class WatchPatterns
return $this->defaultBranch;
}
public function setBaselineWorkflow(string $workflow): void
{
$workflow = trim($workflow);
$this->baselineWorkflow = $workflow === '' ? null : $workflow;
}
public function baselineWorkflow(): ?string
{
return $this->baselineWorkflow;
}
public function reset(): void
{
$this->patterns = [];
@@ -198,6 +212,7 @@ final class WatchPatterns
$this->filtered = false;
$this->baselined = false;
$this->defaultBranch = null;
$this->baselineWorkflow = null;
}
private function keyMatches(string $key, string $file): bool