Skip to content

Instantly share code, notes, and snippets.

@elazar
Created July 11, 2021 01:58
Show Gist options
  • Save elazar/b567a53ee6eacf48005f83000079ca5c to your computer and use it in GitHub Desktop.
Save elazar/b567a53ee6eacf48005f83000079ca5c to your computer and use it in GitHub Desktop.
Lint checking PHP code samples in README.md
<?php
$contents = file_get_contents(__DIR__ . '/README.md');
$start = 0;
while (true) {
$start = strpos($contents, '```php', $start);
if ($start === false) {
break;
}
$end = strpos($contents, '```', $start + 1);
$code = substr($contents, $start, $end - $start);
$file = tempnam(sys_get_temp_dir(), microtime(true) * 10000);
file_put_contents($file, $code);
$output = shell_exec("php -l $file");
if (strpos($output, 'No syntax errors detected') === false) {
$line = substr_count($contents, "\n", 0, $start) + 1;
echo $line . ':' . PHP_EOL . $output . PHP_EOL . PHP_EOL;
}
unlink($file);
$start = $end + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment