Skip to content

Commit e41d432

Browse files
author
Dominik Liebler
committed
check references to PHP files in READMEs
1 parent e3fd663 commit e41d432

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ script:
2525
- vendor/bin/phpunit
2626
- vendor/bin/phpcs .
2727
- vendor/bin/psalm --show-info=false
28+
- ./check-refs-readmes

check-refs-readmes

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// checks if all the PHP have been referenced in a README.rst
5+
// and lists all PHP files that have not been referenced
6+
7+
function getFileIterator(string $regex): Generator {
8+
$directory = new RecursiveDirectoryIterator(__DIR__);
9+
$iterator = new RecursiveIteratorIterator($directory);
10+
$iterator = new RegexIterator($iterator, $regex, RecursiveRegexIterator::GET_MATCH);
11+
12+
foreach ($iterator as $list) {
13+
foreach ($list as $file) {
14+
yield $file;
15+
}
16+
}
17+
}
18+
19+
function getFiles($regex) {
20+
$files = [];
21+
22+
foreach (getFileIterator($regex) as $file) {
23+
$vendor = __DIR__ . '/vendor';
24+
25+
if (substr($file, 0, strlen($vendor)) != $vendor && $file != __FILE__) {
26+
$files[] = $file;
27+
}
28+
}
29+
30+
return $files;
31+
}
32+
33+
function getFilesFromReadmes() {
34+
$allFiles = [];
35+
$start = '.. literalinclude::';
36+
37+
foreach (getFiles('/^.+\.rst$/') as $rst) {
38+
$dirName = dirname($rst);
39+
40+
$lines = file($rst);
41+
$lines = array_filter($lines, function (string $line) use ($start): bool {
42+
return substr($line, 0, strlen($start)) == $start;
43+
});
44+
45+
$files = array_map(function (string $line) use ($dirName, $start): string {
46+
return trim($dirName . '/' . substr($line, strlen($start) + 1));
47+
}, $lines);
48+
49+
$allFiles = array_merge($allFiles, (array) $files);
50+
}
51+
52+
return $allFiles;
53+
}
54+
55+
$phpFiles = getFiles('/^.+\.php$/');
56+
$filesFromReadme = getFilesFromReadmes();
57+
$diff = array_diff($phpFiles, $filesFromReadme);
58+
59+
foreach ($diff as $file) {
60+
echo $file . PHP_EOL;
61+
}
62+
63+
if (count($diff) != 0) {
64+
exit(1);
65+
}

0 commit comments

Comments
 (0)