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