Created
July 11, 2021 01:58
-
-
Save elazar/b567a53ee6eacf48005f83000079ca5c to your computer and use it in GitHub Desktop.
Lint checking PHP code samples in README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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