GitHub Actions Validator — PHP source
Check a workflow YAML before you push: on-shape, jobs, runs-on, steps with run or uses, and the needs graph including cycle detection — every issue reported at its path. Runs locally in your browser.
This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.
<?php
// gh-actions-validator — GitHub Actions workflow validator. PHP port
// (canonical TS: src/lib/gh-actions-validator.ts; Go twin: cli/gh-actions-validator).
// Minimal YAML-subset parser (block maps, lists, scalars) then the rules:
// on/jobs presence, runs-on (or reusable uses), steps run-xor-uses, needs
// known + acyclic. Issues print as "path — message"; "valid" when clean.
/** Minimal indented-block YAML: maps, "- " lists, scalars. */
function yaml_parse_subset(array $lines, int &$i, int $indent): array {
$node = ['map' => [], 'list' => [], 'scalar' => null];
while ($i < count($lines)) {
[$ind, $t] = $lines[$i];
if ($ind < $indent) break;
if (str_starts_with($t, '- ')) {
$v = trim(substr($t, 2), " \t\"'");
$node['list'][] = ['map' => [], 'list' => [], 'scalar' => $v];
$i++;
continue;
}
$colon = strpos($t, ':');
if ($colon === false) { $i++; continue; }
$key = trim(substr($t, 0, $colon));
$rest = trim(substr($t, $colon + 1));
if ($rest === '') {
$i++;
$node['map'][$key] = yaml_parse_subset($lines, $i, $indent + 2);
} else {
$i++;
$node['map'][$key] = ['map' => [], 'list' => [], 'scalar' => trim($rest, "\"'")];
}
}
return $node;
}
function parse_workflow(string $src): array {
$lines = [];
foreach (explode("\n", $src) as $raw) {
$t = trim($raw);
if ($t === '' || $t[0] === '#') continue;
$lines[] = [strlen($raw) - strlen(ltrim($raw)), $t];
}
$i = 0;
return yaml_parse_subset($lines, $i, 0);
}
function needs_of(array $job): array {
if (!isset($job['map']['needs'])) return [];
$n = $job['map']['needs'];
if ($n['list']) return array_map(fn($c) => $c['scalar'], $n['list']);
if ($n['scalar'] !== null) return [$n['scalar']];
return [];
}
function find_cycle(array $jobs): ?string {
$color = [];
$visit = function (string $id, array $stack) use (&$visit, &$color, $jobs): ?string {
if (($color[$id] ?? 0) === 2) return null;
if (($color[$id] ?? 0) === 1) {
$from = array_search($id, $stack, true);
return implode(' -> ', array_merge(array_slice($stack, $from), [$id]));
}
$color[$id] = 1;
$stack[] = $id;
foreach (needs_of($jobs[$id]) as $n) {
if (isset($jobs[$n])) {
if ($c = $visit($n, $stack)) return $c;
}
}
array_pop($stack);
$color[$id] = 2;
return null;
};
foreach (array_keys($jobs) as $id) {
if ($c = $visit($id, [])) return $c;
}
return null;
}
$src = stream_get_contents(STDIN);
$root = parse_workflow($src);
$issues = [];
if (!isset($root['map']['on'])) $issues[] = ['$', 'missing required key "on"'];
if (!isset($root['map']['jobs'])) {
$issues[] = ['$', 'missing required key "jobs"'];
} else {
$jobs = $root['map']['jobs']['map'];
if (!$jobs) $issues[] = ['$.jobs', 'jobs must not be empty'];
foreach ($jobs as $id => $job) {
$at = "\$.jobs.$id";
$reusable = isset($job['map']['uses']['scalar']);
if (!$reusable && !isset($job['map']['runs-on'])) {
$issues[] = ["$at.runs-on", 'job needs runs-on (or a reusable-workflow uses)'];
}
if (!$reusable && !isset($job['map']['steps'])) {
$issues[] = ["$at.steps", 'job needs steps'];
}
if (isset($job['map']['steps']['list'])) {
foreach ($job['map']['steps']['list'] as $k => $step) {
$hasRun = isset($step['map']['run']['scalar']);
$hasUses = isset($step['map']['uses']['scalar']);
if (!$hasRun && !$hasUses) $issues[] = ["$at.steps[$k]", 'step needs run or uses'];
if ($hasRun && $hasUses) $issues[] = ["$at.steps[$k]", 'step has both run and uses - pick one'];
}
}
foreach (needs_of($job) as $k => $n) {
if (!isset($jobs[$n])) $issues[] = ["$at.needs[$k]", "unknown job \"$n\""];
}
}
if ($cycle = find_cycle($jobs)) $issues[] = ['$.jobs', "needs cycle: $cycle"];
}
foreach ($issues as [$p, $m]) echo "$p — $m\n";
echo $issues ? '' : "valid\n";
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →