|
6 | 6 | * Usage: php bin/format |
7 | 7 | */ |
8 | 8 |
|
9 | | -function scan_dir(string $dir, callable $filter = null): array |
10 | | -{ |
11 | | - $files = array_filter(scandir($dir), function (string $file) { |
12 | | - return $file[0] !== '.'; |
13 | | - }); |
14 | | - array_walk($files, function (&$file) use ($dir) { |
15 | | - $file = "{$dir}/{$file}"; |
16 | | - }); |
17 | | - return array_values($filter ? array_filter($files, $filter) : $files); |
| 9 | +$directories = ['src', 'tests', 'sample']; |
| 10 | +$rootDirectoryPath = realpath(dirname(__DIR__)); |
| 11 | + |
| 12 | +foreach ($directories as $directory) { |
| 13 | + $directoryPath = $rootDirectoryPath . '/' . $directory; |
| 14 | + |
| 15 | + $directoryIterator = new RecursiveDirectoryIterator($directoryPath, RecursiveDirectoryIterator::SKIP_DOTS); |
| 16 | + $iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST); |
| 17 | + |
| 18 | + $phpFiles = findPhpFiles($iterator); |
| 19 | + |
| 20 | + foreach ($phpFiles as $file) { |
| 21 | + $formattedContent = formatFile($file); |
| 22 | + writeToFile($file, $formattedContent, $directory); |
| 23 | + } |
18 | 24 | } |
19 | 25 |
|
20 | | -function fix_tests_in_this_dir(string $dir, string $root = '') |
| 26 | +echo 'Formatting completed', PHP_EOL; |
| 27 | + |
| 28 | +/** |
| 29 | + * Returns an array of PHP file paths from the directory iterator. |
| 30 | + * |
| 31 | + * @param RecursiveIteratorIterator $iterator The iterator of the directory to search through. |
| 32 | + * @return array An array of PHP file paths. |
| 33 | + */ |
| 34 | +function findPhpFiles($iterator) |
21 | 35 | { |
22 | | - $files = scan_dir($dir); |
23 | | - foreach ($files as $file) { |
24 | | - if (pathinfo($file, PATHINFO_EXTENSION) === 'php') { |
25 | | - $requirement_level = (function () use ($root, $file) { |
26 | | - for ($l = 0; $l < 8; $l++) { |
27 | | - $file = dirname($file); |
28 | | - if ($file === $root) { |
29 | | - return $l; |
30 | | - } |
31 | | - } |
32 | | - return -1; |
33 | | - })(); |
34 | | - if ($requirement_level < 0) { |
35 | | - echo("Failed to get requirement level of file {$file}"); |
36 | | - } |
37 | | - $content = file_get_contents($file); |
38 | | - $changed = false; |
39 | | - // empty lines |
40 | | - $_content = trim($content) . "\n"; |
41 | | - if ($content !== $_content) { |
42 | | - echo "Format empty lines in {$file}", PHP_EOL; |
43 | | - $content = $_content; |
44 | | - $changed = true; |
45 | | - } |
46 | | - if ($changed) { |
47 | | - file_put_contents($file, $content); |
48 | | - } |
49 | | - } elseif (is_dir($file)) { |
50 | | - fix_tests_in_this_dir($file, $root); |
| 36 | + $phpFiles = []; |
| 37 | + foreach ($iterator as $file) { |
| 38 | + if ($file->getExtension() !== 'php' || $file->isDir()) { |
| 39 | + continue; |
51 | 40 | } |
| 41 | + $phpFiles[] = $file->getPathname(); |
52 | 42 | } |
| 43 | + return $phpFiles; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Returns the formatted content of a file. |
| 48 | + * |
| 49 | + * @param string $file The path to the file to format. |
| 50 | + * @return string The formatted content. |
| 51 | + */ |
| 52 | +function formatFile($file) |
| 53 | +{ |
| 54 | + $content = file_get_contents($file); |
| 55 | + return formatLineEndings($content); |
53 | 56 | } |
54 | 57 |
|
55 | | -$root = realpath(dirname(__DIR__)); |
56 | | -$dirs = ['src', 'tests', 'sample']; |
57 | | -foreach ($dirs as $dir) { |
58 | | - fix_tests_in_this_dir($root . '/' . $dir, $root); |
| 58 | +/** |
| 59 | + * Returns the content with formatted line endings. |
| 60 | + * |
| 61 | + * @param string $content The content to format. |
| 62 | + * @return string The content with formatted line endings. |
| 63 | + */ |
| 64 | +function formatLineEndings($content) |
| 65 | +{ |
| 66 | + return trim($content) . "\n"; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Returns the content with all occurrences of 'schema' replaced with 'scheme'. |
| 71 | + * |
| 72 | + * @param string $content The content to perform replacements on. |
| 73 | + * @return string The content with all occurrences of 'schema' replaced with 'scheme'. |
| 74 | + */ |
| 75 | +function replaceSchemaWithScheme($content) |
| 76 | +{ |
| 77 | + return str_replace(['schema', 'Schema'], ['scheme', 'Scheme'], $content); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Writes the provided content to a file if it differs from the original content of the file. |
| 82 | + * Also checks the directory and if it's not 'src', replaces 'schema' with 'scheme' in the content. |
| 83 | + * |
| 84 | + * @param string $file The path to the file to write. |
| 85 | + * @param string $content The content to write to the file. |
| 86 | + * @param string $directory The directory the file resides in. |
| 87 | + */ |
| 88 | +function writeToFile($file, $content, $directory) |
| 89 | +{ |
| 90 | + $originalContent = file_get_contents($file); |
| 91 | + $changed = false; |
| 92 | + if ($originalContent !== $content) { |
| 93 | + echo "Formatted empty lines in {$file}", PHP_EOL; |
| 94 | + $changed = true; |
| 95 | + } |
| 96 | + if ($directory != 'src') { |
| 97 | + $_content = replaceSchemaWithScheme($content); |
| 98 | + if ($content !== $_content) { |
| 99 | + echo "Use scheme instead of schema in {$file}", PHP_EOL; |
| 100 | + $content = $_content; |
| 101 | + $changed = true; |
| 102 | + } |
| 103 | + } |
| 104 | + if ($changed) { |
| 105 | + file_put_contents($file, $content); |
| 106 | + } |
59 | 107 | } |
60 | | -echo 'Format done', PHP_EOL; |
|
0 commit comments