|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| - declare(strict_types=1); |
4 |
| - |
5 |
| - if ($argc == 1) { |
6 |
| - $mode = ''; |
7 |
| - $dir = '.'; |
8 |
| - } elseif ($argc == 2) { |
9 |
| - $mode = $argv[1]; |
10 |
| - $dir = '.'; |
11 |
| - } elseif ($argc == 3) { |
12 |
| - $mode = $argv[1]; |
13 |
| - $dir = $argv[2]; |
14 |
| - } else { |
15 |
| - die('JSON Checker cannot be run with this set of parameters!'); |
16 |
| - } |
17 |
| - |
18 |
| - if (!in_array($mode, ['', 'fix'])) { |
19 |
| - die('Unsupported mode "' . $mode . '"!'); |
20 |
| - } |
21 |
| - |
22 |
| - $start = microtime(true); |
23 |
| - $invalidFiles = jsonStyleCheck($dir, $mode); |
24 |
| - $duration = microtime(true) - $start; |
25 |
| - |
26 |
| - foreach ($invalidFiles as $invalidFile) { |
27 |
| - echo $invalidFile . PHP_EOL; |
28 |
| - } |
29 |
| - |
30 |
| - if (!empty($invalidFiles)) { |
31 |
| - echo PHP_EOL; |
32 |
| - } |
33 |
| - |
34 |
| - echo 'Checked all files in ' . number_format($duration, 3) . ' seconds, ' . number_format(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB memory used' . PHP_EOL; |
35 |
| - |
36 |
| - if (!empty($invalidFiles)) { |
37 |
| - exit(1); |
38 |
| - } |
39 |
| - |
40 |
| - function jsonStyleCheck(string $dir, string $mode) |
41 |
| - { |
42 |
| - $ignore = ['./.vscode', './.idea', './.git', './libs/vendor']; |
43 |
| - $invalidFiles = []; |
44 |
| - $files = scandir($dir); |
45 |
| - foreach ($files as $file) { |
46 |
| - if ($file != '.' && $file != '..' && !in_array($dir, $ignore)) { |
47 |
| - if (is_dir($dir . '/' . $file)) { |
48 |
| - $invalidFiles = array_merge($invalidFiles, jsonStyleCheck($dir . '/' . $file, $mode)); |
49 |
| - } else { |
50 |
| - if (fnmatch('*.json', $dir . '/' . $file)) { |
51 |
| - $invalidFile = checkContentInFile($dir . '/' . $file, $mode); |
52 |
| - if ($invalidFile !== false) { |
53 |
| - $invalidFiles[] = $invalidFile; |
54 |
| - } |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +if ($argc == 1) { |
| 6 | + $mode = ''; |
| 7 | + $dir = '.'; |
| 8 | +} elseif ($argc == 2) { |
| 9 | + $mode = $argv[1]; |
| 10 | + $dir = '.'; |
| 11 | +} elseif ($argc == 3) { |
| 12 | + $mode = $argv[1]; |
| 13 | + $dir = $argv[2]; |
| 14 | +} else { |
| 15 | + die('JSON Checker cannot be run with this set of parameters!'); |
| 16 | +} |
| 17 | + |
| 18 | +if (!in_array($mode, ['', 'fix'])) { |
| 19 | + die('Unsupported mode "' . $mode . '"!'); |
| 20 | +} |
| 21 | + |
| 22 | +$start = microtime(true); |
| 23 | +$invalidFiles = jsonStyleCheck($dir, $mode); |
| 24 | +$duration = microtime(true) - $start; |
| 25 | + |
| 26 | +foreach ($invalidFiles as $invalidFile) { |
| 27 | + echo $invalidFile . PHP_EOL; |
| 28 | +} |
| 29 | + |
| 30 | +if (!empty($invalidFiles)) { |
| 31 | + echo PHP_EOL; |
| 32 | +} |
| 33 | + |
| 34 | +echo 'Checked all files in ' . number_format($duration, 3) . ' seconds, ' . number_format(memory_get_peak_usage() / 1024 / 1024, 3) . ' MB memory used' . PHP_EOL; |
| 35 | + |
| 36 | +if (!empty($invalidFiles)) { |
| 37 | + exit(1); |
| 38 | +} |
| 39 | + |
| 40 | +function jsonStyleCheck(string $dir, string $mode) |
| 41 | +{ |
| 42 | + $ignore = ['./.vscode', './.idea', './.git', './libs/vendor']; |
| 43 | + $invalidFiles = []; |
| 44 | + $files = scandir($dir); |
| 45 | + foreach ($files as $file) { |
| 46 | + if ($file != '.' && $file != '..' && !in_array($dir, $ignore)) { |
| 47 | + if (is_dir($dir . '/' . $file)) { |
| 48 | + $invalidFiles = array_merge($invalidFiles, jsonStyleCheck($dir . '/' . $file, $mode)); |
| 49 | + } else { |
| 50 | + if (fnmatch('*.json', $dir . '/' . $file)) { |
| 51 | + $invalidFile = checkContentInFile($dir . '/' . $file, $mode); |
| 52 | + if ($invalidFile !== false) { |
| 53 | + $invalidFiles[] = $invalidFile; |
55 | 54 | }
|
56 | 55 | }
|
57 | 56 | }
|
58 | 57 | }
|
59 |
| - return $invalidFiles; |
60 | 58 | }
|
| 59 | + return $invalidFiles; |
| 60 | +} |
61 | 61 |
|
62 |
| - function checkContentInFile(string $dir, string $mode) |
63 |
| - { |
64 |
| - $fileOriginal = file_get_contents($dir); |
65 |
| - |
66 |
| - // Normalize line endings |
67 |
| - $fileOriginal = str_replace("\r\n", "\n", $fileOriginal); |
68 |
| - $fileOriginal = str_replace("\r", "\n", $fileOriginal); |
| 62 | +function checkContentInFile(string $dir, string $mode) |
| 63 | +{ |
| 64 | + $fileOriginal = file_get_contents($dir); |
69 | 65 |
|
70 |
| - // Ignore line break at the end of the file |
71 |
| - $fileOriginal = rtrim($fileOriginal, "\n"); |
| 66 | + // Normalize line endings |
| 67 | + $fileOriginal = str_replace("\r\n", "\n", $fileOriginal); |
| 68 | + $fileOriginal = str_replace("\r", "\n", $fileOriginal); |
72 | 69 |
|
73 |
| - // Reformat JSON using PHP |
74 |
| - $fileCompare = json_encode(json_decode($fileOriginal), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION); |
| 70 | + // Ignore line break at the end of the file |
| 71 | + $fileOriginal = rtrim($fileOriginal, "\n"); |
75 | 72 |
|
76 |
| - if ($fileOriginal == $fileCompare) { |
77 |
| - return false; |
78 |
| - } |
| 73 | + // Reformat JSON using PHP |
| 74 | + $fileCompare = json_encode(json_decode($fileOriginal), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION); |
79 | 75 |
|
80 |
| - if ($mode == 'fix') { |
81 |
| - file_put_contents($dir, $fileCompare); |
82 |
| - } |
| 76 | + if ($fileOriginal == $fileCompare) { |
| 77 | + return false; |
| 78 | + } |
83 | 79 |
|
84 |
| - return $dir; |
| 80 | + if ($mode == 'fix') { |
| 81 | + file_put_contents($dir, $fileCompare); |
85 | 82 | }
|
| 83 | + |
| 84 | + return $dir; |
| 85 | +} |
0 commit comments